博客
关于我
Kubernetes官方java客户端之四:内部应用
阅读量:412 次
发布时间:2019-03-06

本文共 4097 字,大约阅读时间需要 13 分钟。

开发Kubernetes环境内的Spring Boot应用

概述

本文将介绍如何开发一个完全部署在Kubernetes环境中的Spring Boot应用,该应用将使用Kubernetes官方Java客户端对Kubernetes API进行操作。

环境准备

为了顺利完成本文的实战,我们需要具备以下基础知识和工具:

  • Spring Boot的基本知识
  • Docker镜像制作
  • Kubernetes环境的探针技术
  • 项目结构

    项目的代码结构如下:

    kubernetesclient/├── pom.xml└── helloworld/    ├── DemoApplication.java    └── Dockerfile

    代码编写

    1. 创建Spring Boot应用

    以下是应用的主要代码:

    package com.bolingcavalry.demo;import com.google.gson.Gson;import io.kubernetes.client.openapi.ApiClient;import io.kubernetes.client.openapi.Configuration;import io.kubernetes.client.openapi.apis.CoreV1Api;import io.kubernetes.client.openapi.models.V1PodList;import io.kubernetes.client.util.Config;import lombok.extern.slf4j.Slf4j;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.stream.Collectors;@SpringBootApplication@RestController@Slf4jpublic class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }    @RequestMapping(value = "/hello")    public List
    hello() throws Exception { ApiClient client = Config.defaultClient(); Configuration.setDefaultApiClient(client); CoreV1Api api = new CoreV1Api(); V1PodList v1PodList = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null); log.info("pod info \n{}", new Gson().toJson(v1PodList)); return v1PodList .getItems() .stream() .map(value -> value.getMetadata().getNamespace() + ":" + value.getMetadata().getName()) .collect(Collectors.toList()); }}

    2. Docker镜像构建

    以下是用于制作Docker镜像的Dockerfile:

    # 基础镜像FROM openjdk:8u212-jdk-stretch AS builderWORKDIR applicationARG JAR_FILE=target/*.jar# 提取构建镜像RUN java -Djarmode=layertools -jar ${JAR_FILE} extract# 正式构建镜像FROM openjdk:8u212-jdk-stretchWORKDIR applicationCOPY --from=builder application/dependencies/ .COPY --from=builder application/spring-boot-loader/ .COPY --from=builder application/snapshot-dependencies/ .COPY --from=builder application/application/ .ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

    镜像构建与部署

    1. 在本地编译并构建镜像

    执行以下命令:

    mvn clean package -U -DskipTests

    2. 制作Docker镜像

    确保已安装Docker,执行以下命令:

    docker build -t 192.168.50.43:5888/common/helloworld:1.0-SNAPSHOT .

    3. 推送镜像到Harbor

    将镜像推送到Harbor仓库:

    docker push 192.168.50.43:5888/common/helloworld:1.0-SNAPSHOT

    Kubernetes部署

    1. 创建Deployment和Service

    以下是Kubernetes配置文件:

    apiVersion: v1kind: Servicemetadata:  name: helloworld  namespace: kubernetesclientspec:  type: NodePort  ports:  - port: 8080    nodePort: 30100  selector:    name: helloworldapiVersion: extensions/v1beta1kind: Deploymentmetadata:  namespace: kubernetesclient  name: helloworldspec:  replicas: 1  template:    metadata:      labels:        name: helloworld    spec:      serviceAccountName: kubernates-client-service-account      containers:        - name: helloworld          image: 192.168.50.43:5888/common/helloworld:1.0-SNAPSHOT          tty: true          livenessProbe:            httpGet:              path: /actuator/health/liveness              port: 8080            initialDelaySeconds: 5            failureThreshold: 10            timeoutSeconds: 10            periodSeconds: 5          readinessProbe:            httpGet:              path: /actuator/health/readiness              port: 8080            initialDelaySeconds: 5            timeoutSeconds: 10            periodSeconds: 5          ports:            - containerPort: 8080          resources:            requests:              memory: 512Mi              cpu: 100m            limits:              memory: 1Gi              cpu: 1000m

    2. 部署应用

    执行以下命令:

    kubectl apply -f helloworld.yaml

    测试与验证

    1. 查看Pod信息

    运行以下命令查看Pod信息:

    kubectl get pods

    2. 访问应用

    通过浏览器访问:

    http://192.168.50.135:30100/hello

    总结

    通过本文的实践,我们成功地将一个Spring Boot应用部署到了Kubernetes环境中,并使用Kubernetes官方Java客户端对API进行操作。这为后续学习Kubernetes客户端的更深入功能打下了基础。

    转载地址:http://uqtkz.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现basic graphs基本图算法(附完整源码)
    查看>>
    Objective-C实现BCC校验计算(附完整源码)
    查看>>
    Objective-C实现bead sort珠排序算法(附完整源码)
    查看>>
    Objective-C实现BeadSort珠排序算法(附完整源码)
    查看>>
    Objective-C实现bellman ford贝尔曼福特算法(附完整源码)
    查看>>
    Objective-C实现bellman-ford贝尔曼-福特算法(附完整源码)
    查看>>
    Objective-C实现bellman-ford贝尔曼-福特算法(附完整源码)
    查看>>
    Objective-C实现BellmanFord贝尔曼-福特算法(附完整源码)
    查看>>
    Objective-C实现bezier curve贝塞尔曲线算法(附完整源码)
    查看>>
    Objective-C实现bfs 最短路径算法(附完整源码)
    查看>>
    Objective-C实现BF算法 (附完整源码)
    查看>>
    Objective-C实现Bilateral Filter双边滤波器算法(附完整源码)
    查看>>
    Objective-C实现binary exponentiation二进制幂运算算法(附完整源码)
    查看>>
    Objective-C实现binary search二分查找算法(附完整源码)
    查看>>
    Objective-C实现binary tree mirror二叉树镜像算法(附完整源码)
    查看>>
    Objective-C实现binary tree traversal二叉树遍历算法(附完整源码)
    查看>>
    Objective-C实现BinarySearchTreeNode树算法(附完整源码)
    查看>>
    Objective-C实现binomial coefficient二项式系数算法(附完整源码)
    查看>>
    Objective-C实现bisection二分法算法(附完整源码)
    查看>>
    Objective-C实现bisection二等分算法(附完整源码)
    查看>>