欢送拜访我的GitHub
https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,波及Java、Docker、Kubernetes、DevOPS等;
概览
- 以下提到的<font color="blue">java客户端</font>都是指<font color="red">client-jar.jar</font>;
- 本文是《Kubernetes官网java客户端》系列的第三篇,《Kubernetes官网java客户端:筹备》一文中咱们为实战做好了筹备工作,从本文开始进入实战阶段;
- 本文的指标是开发名为<font color="blue">OutsideclusterApplication的SpringBoot</font>利用,该利用没有部署在K8S环境,应用的config文件是手动从K8S环境复制过去的,java客户端通过此config文件,可能近程拜访到K8S上的API Server,实现所有客户端性能,整体部署状况如下图:
- 介绍结束,开始编码;
源码下载
- 如果您不想编码,能够在GitHub下载所有源码,地址和链接信息如下表所示(https://github.com/zq2599/blo...:
名称 | 链接 | 备注 |
---|---|---|
我的项目主页 | https://github.com/zq2599/blo... | 该我的项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blo... | 该我的项目源码的仓库地址,https协定 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该我的项目源码的仓库地址,ssh协定 |
- 这个git我的项目中有多个文件夹,本章的利用在<font color="blue">kubernetesclient</font>文件夹下,如下图红框所示:
部署在K8S之外的利用:OutsideclusterApplication
名为<font color="blue">OutsideclusterApplication</font>的利用并未部署在K8S环境,该利用可能拜访到K8S环境的要害,就是将K8S环境的config文件复制一份,而后放在OutsideclusterApplication可能拜访到的地位:
- 登录K8S环境,在~/.kube目录下找到config文件,复制此文件到OutsideclusterApplication运行的机器上(我这里寄存的门路是/Users/zhaoqin/temp/202007/05/,和前面的代码中统一);
- 关上《Kubernetes官网java客户端:筹备》中创立的的<font color="blue">kubernetesclient</font>工程,在外面创立子工程,名为<font color="blue">OutsideclusterApplication</font>,这是个SpringBoot工程,pom.xml内容如下:
<?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>outsidecluster</artifactId> <version>0.0.1-SNAPSHOT</version> <name>outsidecluster</name> <description>Demo project for Spring Boot</description> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </exclusion> </exclusions> </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> </plugin> </plugins> </build></project>
- 上述pom.xml中,须要留神的是在依赖spring-boot-starter-web的时候,应用exclusion语法排除了spring-boot-starter-json的依赖,这样做是为了将jackson的依赖全副去掉(spring-boot-starter-json依赖了jackson),如此一来整个classpath上面就没有了jackson库,此时SpringBoot框架就会应用gson作为序列化和反序列化工具(client-java.jar依赖了gson库);(这个问题在《Kubernetes官网java客户端之二:序列化和反序列化问题》一文有具体介绍)
- 新增OutsideclusterApplication.java,简略起见,该类即是疏导类又是Controller:
package com.bolingcavalry.outsidecluster;import com.google.gson.GsonBuilder;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.ClientBuilder;import io.kubernetes.client.util.KubeConfig;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.io.FileReader;@SpringBootApplication@RestController@Slf4jpublic class OutsideclusterApplication { public static void main(String[] args) { SpringApplication.run(OutsideclusterApplication.class, args); } @RequestMapping(value = "/hello") public V1PodList hello() throws Exception { // 寄存K8S的config文件的全门路 String kubeConfigPath = "/Users/zhaoqin/temp/202007/05/config"; // 以config作为入参创立的client对象,能够拜访到K8S的API Server ApiClient client = ClientBuilder .kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))) .build(); Configuration.setDefaultApiClient(client); CoreV1Api api = new CoreV1Api(); // 调用客户端API获得所有pod信息 V1PodList v1PodList = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null); // 应用jackson将汇合对象序列化成JSON,在日志中打印进去 log.info("pod info \n{}", new GsonBuilder().setPrettyPrinting().create().toJson(v1PodList)); return v1PodList; }}
- 运行上述代码,在浏览器拜访http://localhost:8080/hello ,即可获得K8S所有pod的详情,如下所示(为了让返回数据更加参差好看,我用的是Firefox浏览器):
- 查看控制台,可见日志也将详情打印进去:
- 至此,咱们的第一个应用K8S官网java客户端的利用就实现了,接下来的实战会尝试将利用部署在K8S环境内,在K8S外部进行各项操作;
你不孤独,欣宸原创一路相伴
- Java系列
- Spring系列
- Docker系列
- kubernetes系列
- 数据库+中间件系列
- DevOps系列
欢送关注公众号:程序员欣宸
微信搜寻「程序员欣宸」,我是欣宸,期待与您一起畅游Java世界...
https://github.com/zq2599/blog_demos