博客
关于我
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/

    你可能感兴趣的文章
    python判断汉字数目
    查看>>
    python判断文件是空的,如果是空的,就删除
    查看>>
    python判断密码是否正确_python密码判断是否符合要求的方法
    查看>>
    python判断字符串包含中文_Python 判断字符串是否包含中文
    查看>>
    python删除第一行_Python 乱码指北:一行删掉根目录
    查看>>
    Python删除列表元素的三种方法
    查看>>
    python初步学习-python数据类型-集合(set)
    查看>>
    python列表生成字典_Python中将字典转换为列表的方法
    查看>>
    python列表对应元素合并为列表及判断一个列表是几维
    查看>>
    python列表去重复后按照顺序_从包含不可共元素的Python列表中删除重复元素,同时保留顺序?...
    查看>>
    python列表前几个_python之列表
    查看>>
    python列表元组
    查看>>
    Python列表/元组/字典和集合使用
    查看>>
    python列表 行列选择_python_pandas_dataframe_行列选择_切片操作
    查看>>
    python列表
    查看>>
    python列出当前目录、子目录和文件的脚本
    查看>>
    python+flask计算机毕业设计骨科门诊患者档案管理系统(程序+开题+论文)
    查看>>
    python+flask计算机毕业设计高校体测管理系统的设计与实现(程序+开题+论文)
    查看>>
    Python切片对象和__getitem__
    查看>>
    python+flask计算机毕业设计高校体育竞赛成绩管理系统的设计与实现(程序+开题+论文)
    查看>>