https://github.com/zq2599/blog_demosjava
内容:全部原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;node
本文是《Kubernetes官方java客户端》系列的第四篇,如下提到的java客户端都是指client-jar.jar;git
前文《Kubernetes官方java客户端之三:外部应用》中,我们开发了一个名为OutsideclusterApplication的SpringBoot应用,该应用并未部署在K8S环境,而是远程访问K8S环境内部的API Server,总体结构以下:
程序员
除了前文中部署在外部的方式,还有一种常见场景:使用java客户端的应用自身也部署在K8S环境中,以下图所示,名为DemoApplication的SpringBoot应用部署在K8S环境内,调用java客户端库的API对K8S进行各类操做,总体结构以下:
github
本文的内容就是开发上图中名为DemoApplication的应用,而且部署在K8S环境中进行验证;web
名称 | 连接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.bolingcavalry</groupId> <artifactId>kubernetesclient</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <groupId>com.bolingcavalry</groupId> <artifactId>helloworld</artifactId> <version>0.0.1-SNAPSHOT</version> <name>helloworld</name> <description>Demo project for Spring Boot</description> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>io.kubernetes</groupId> <artifactId>client-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.0.RELEASE</version> <!--该配置会在jar中增长layer描述文件,以及提取layer的工具--> <configuration> <layers> <enabled>true</enabled> </layers> </configuration> </plugin> </plugins> </build> </project>
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 @Slf4j public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @RequestMapping(value = "/hello") public List<String> hello() throws Exception { ApiClient client = Config.defaultClient(); Configuration.setDefaultApiClient(client); CoreV1Api api = new CoreV1Api(); // 调用客户端API取得全部pod信息 V1PodList v1PodList = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null); // 使用Gson将集合对象序列化成JSON,在日志中打印出来 log.info("pod info \n{}", new Gson().toJson(v1PodList)); return v1PodList .getItems() .stream() .map(value -> value.getMetadata().getNamespace() + ":" + value.getMetadata().getName()) .collect(Collectors.toList()); } }
# 指定基础镜像,这是分阶段构建的前期阶段 FROM openjdk:8u212-jdk-stretch as builder # 执行工做目录 WORKDIR application # 配置参数 ARG JAR_FILE=target/*.jar # 将编译构建获得的jar文件复制到镜像空间中 COPY ${JAR_FILE} application.jar # 经过工具spring-boot-jarmode-layertools从application.jar中提取拆分后的构建结果 RUN java -Djarmode=layertools -jar application.jar extract # 正式构建镜像 FROM openjdk:8u212-jdk-stretch WORKDIR application # 前一阶段从jar中提取除了多个文件,这里分别执行COPY命令复制到镜像空间中,每次COPY都是一个layer COPY --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"]
mvn clean package -U -DskipTests
docker build -t 192.168.50.43:5888/common/helloworld:1.0-SNAPSHOT .
上述命令执行成功后,镜像文件还只是在本机的docker仓库中,请放置到K8S环境能够访问的地方,我这里是在内网部署了镜像仓库Harbor,执行如下命令便可从本地仓库推送到Harbor(可能须要先登陆,与Harbor的设置有关):
spring
镜像准备完成,接下来就是在K8S环境部署了,在K8S环境建立名为helloworld.yaml的文件,内容以下,可见deployment和service都配置好了,另外请注意serviceAccountName属性的值为kubernates-client-service-account,此serviceAccountName是在《Kubernetes官方java客户端之一:准备》一文中建立好的RBAC资源,令我们开发的helloworld应用有权限请求API Server:docker
apiVersion: v1 kind: Service metadata: name: helloworld namespace : kubernetesclient spec: type: NodePort ports: - port: 8080 nodePort: 30100 selector: name: helloworld --- apiVersion: extensions/v1beta1 kind: Deployment metadata: namespace : kubernetesclient name: helloworld spec: 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"
helloworld.yaml所在目录执行命令:kubectl apply -f helloworld.yaml
shell
我这边,上图中的Pod所在宿主机IP地址是192.168.50.135,所以用浏览器访问http://192.168.50.135:30100/hello,以下图,可见当前K8S环境下全部Pod名称都返回了:
数据库
至此,SpringBoot应用经过K8S官方java客户端,成功获取了自身所在K8S环境的信息,经过前文和本章,我们对K8S官方java客户端已经有了基本的认识,接下来的实战会开启这个客户端更丰富的能力;
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
https://github.com/zq2599/blog_demos