欢送拜访我的GitHub
https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,波及Java、Docker、Kubernetes、DevOPS等;
概览
- 本文是《Kubernetes官网java客户端》系列的第四篇,以下提到的java客户端都是指client-jar.jar;
- 前文《Kubernetes官网java客户端之三:内部利用》中,咱们开发了一个名为<font color=”blue”>OutsideclusterApplication</font>的SpringBoot利用,该利用并未部署在K8S环境,而是近程拜访K8S环境外部的API Server,整体构造如下:
- 除了前文中部署在内部的形式,还有一种常见场景:应用java客户端的利用本身也部署在K8S环境中,如下图所示,名为<font color=”blue”>DemoApplication的SpringBoot</font>利用部署在K8S环境内,调用java客户端库的API对K8S进行各种操作,整体构造如下:
- 本文的内容就是开发上图中名为<font color=”blue”>DemoApplication</font>的利用,并且部署在K8S环境中进行验证;
额定筹备
- 前文《Kubernetes官网java客户端之三:内部利用》的实战是一次惯例的SpringBoot利用开发,本文的实战和前文略有不同,您须要对以下知识点有所理解:
- SpringBoot制作成docker镜像,首选官网举荐的形式,参考《体验SpringBoot(2.3)利用制作Docker镜像(官网计划)》、《详解SpringBoot(2.3)利用制作Docker镜像(官网计划)》
- SpringBoot利用在K8S环境下的探针技术,参考《把握SpringBoot-2.3的容器探针:根底篇》、《把握SpringBoot-2.3的容器探针:深刻篇》、《把握SpringBoot-2.3的容器探针:实战篇》
源码下载
- 如果您不想编码,能够在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环境内的利用:DemoApplication
- 关上《Kubernetes官网java客户端:筹备》中创立的的<font color=”blue”>kubernetesclient</font>工程,在外面创立子工程,名为<font color=”red”>helloworld</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>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>
- 编写java代码,创立<font color=”blue”>DemoApplication.java</font>,这里为了简略起见,将疏导类和web controller的代码都写在DemoApplication类中:
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());
}
}
- 还记得《Kubernetes官网java客户端之二:序列化和反序列化问题》提到的序列化问题吗?上述代码中,log.info那段代码里对<font color=”blue”>V1PodList</font>执行序列化的是Gson,并且hello办法返回的也不是V1PodList实例,而是新做的一个List实例,这样做是因为jackson对V1PodList做序列化会导致异样,这里要防止jackson参加序列化操作;
- 利用的代码曾经写完,接下来是镜像制作用到的Dockerfile文件,该文件和方才创立的pom.xml文件在同一个目录下(即子工程<font color=”blue”>helloworld</font>的文件夹下),Dockerfile文件内容如下:
# 指定根底镜像,这是分阶段构建的后期阶段
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"]
- 在子工程pom.xml文件所在目录执行以下命令实现编译构建:
mvn clean package -U -DskipTests
- 接下来要制作镜像文件了,请确保以后电脑曾经装置并运行了docker,另外构建docker镜像的操作<font color=”red”>我仅在macOS和Linux操作系统下执行胜利</font>,在Windows环境是否胜利请自行尝试;
- 在Dockerfile所在目录执行以下命令即可创立docker镜像文件:
docker build -t 192.168.50.43:5888/common/helloworld:1.0-SNAPSHOT .
- 上述命令执行胜利后,镜像文件还只是在本机的docker仓库中,请搁置到K8S环境能够拜访的中央,我这里是在内网部署了镜像仓库Harbor,执行以下命令即可从本地仓库推送到Harbor(可能须要先登录,与Harbor的设置无关):
- 镜像筹备实现,接下来就是在K8S环境部署了,在K8S环境创立名为helloworld.yaml的文件,内容如下,可见deployment和service都配置好了,另外请留神serviceAccountName属性的值为kubernates-client-service-account,此serviceAccountName是在《Kubernetes官网java客户端之一:筹备》一文中创立好的RBAC资源,令咱们开发的helloworld利用有权限申请API Server:
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所在目录执行命令:<font color=”blue”>kubectl apply -f helloworld.yaml</font>
- 我这边,上图中的Pod所在宿主机IP地址是<font color=”blue”>192.168.50.135</font>,因而用浏览器拜访<font color=”blue”>http://192.168.50.135:30100/hello</font>,如下图,可见以后K8S环境下所有Pod名称都返回了:
至此,SpringBoot利用通过K8S官网java客户端,胜利获取了本身所在K8S环境的信息,通过前文和本章,咱们对K8S官网java客户端曾经有了根本的意识,接下来的实战会开启这个客户端更丰盛的能力;
你不孤独,欣宸原创一路相伴
- Java系列
- Spring系列
- Docker系列
- kubernetes系列
- 数据库+中间件系列
- DevOps系列
欢送关注公众号:程序员欣宸
微信搜寻「程序员欣宸」,我是欣宸,期待与您一起畅游Java世界…
https://github.com/zq2599/blog_demos
发表回复