内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等; 概览
额外准备
源码下载
开发K8S环境内的应用:DemoApplication
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven./POM/4.0.0" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://maven./POM/4.0.0 https://maven./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 .
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"
至此,SpringBoot应用通过K8S官方java客户端,成功获取了自身所在K8S环境的信息,通过前文和本章,咱们对K8S官方java客户端已经有了基本的认识,接下来的实战会开启这个客户端更丰富的能力; 你不孤单,欣宸原创一路相伴 |
|
来自: python_lover > 《待分类》