Return Homezq2599
CnBlogsHomeNew PostContactAdminSubscription订阅Posts - 133 Articles - 0 Comments - 21
Kubernetes官方java客户端之四:内部应用
欢迎访问我的GitHub
https://github.com/zq2599/blog_demos

内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;

概览
本文是《Kubernetes官方java客户端》系列的第四篇,以下提到的java客户端都是指client-jar.jar;

前文《Kubernetes官方java客户端之三:外部应用》中,咱们开发了一个名为OutsideclusterApplication的SpringBoot应用,该应用并未部署在K8S环境,而是远程访问K8S环境内部的API Server,整体结构如下:
在这里插入图片描述

除了前文中部署在外部的方式,还有一种常见场景:使用java客户端的应用自身也部署在K8S环境中,如下图所示,名为DemoApplication的SpringBoot应用部署在K8S环境内,调用java客户端库的API对K8S进行各种操作,整体结构如下:
在这里插入图片描述

本文的内容就是开发上图中名为DemoApplication的应用,并且部署在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/blog_demos):
名称 链接 备注
项目主页 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协议
这个git项目中有多个文件夹,本章的应用在kubernetesclient文件夹下,如下图红框所示:
在这里插入图片描述
开发K8S环境内的应用:DemoApplication
打开《Kubernetes官方java客户端:准备》中创建的的kubernetesclient工程,在里面创建子工程,名为helloworld,这是个SpringBoot工程,pom.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

<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>
编写java代码,创建DemoApplication.java,这里为了简单起见,将引导类和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那段代码里对V1PodList执行序列化的是Gson,并且hello方法返回的也不是V1PodList实例,而是新做的一个List实例,这样做是因为jackson对V1PodList做序列化会导致异常,这里要避免jackson参与序列化操作;
应用的代码已经写完,接下来是镜像制作用到的Dockerfile文件,该文件和刚才创建的pom.xml文件在同一个目录下(即子工程helloworld的文件夹下),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镜像的操作我仅在macOS和Linux操作系统下执行成功,在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所在目录执行命令:kubectl apply -f helloworld.yaml
在这里插入图片描述

我这边,上图中的Pod所在宿主机IP地址是192.168.50.135,因此用浏览器访问http://192.168.50.135:30100/hello,如下图,可见当前K8S环境下所有Pod名称都返回了:
在这里插入图片描述

至此,SpringBoot应用通过K8S官方java客户端,成功获取了自身所在K8S环境的信息,通过前文和本章,咱们对K8S官方java客户端已经有了基本的认识,接下来的实战会开启这个客户端更丰富的能力;

你不孤单,欣宸原创一路相伴
Java系列
Spring系列
Docker系列
kubernetes系列
数据库+中间件系列
DevOps系列
欢迎关注公众号:程序员欣宸
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界…
https://github.com/zq2599/blog_demos

好文要顶 关注我 收藏该文
程序员欣宸
关注 - 4
粉丝 - 17
+加关注
00
« 上一篇: Kubernetes官方java客户端之三:外部应用
posted @ 2021-01-06 08:41 程序员欣宸 Views(26) Comments(0) Edit 收藏
刷新评论刷新页面返回顶部
登录后才能发表评论,立即 登录 或 注册, 访问 网站首页
【推荐】News: 大型组态、工控、仿真、CADGIS 50万行VC++源码免费下载
【推荐】有你助力,更好为你——博客园用户消费观调查,附带小惊喜!
【推荐】AWS携手博客园为开发者送福利,注册立享12个月免费套餐
【推荐】七牛云新老用户同享 1 分钱抢 CDN 1TB流量大礼包!
【推荐】了不起的开发者,挡不住的华为,园子里的品牌专区
【推荐】未知数的距离,毫秒间的传递,声网与你实时互动
【推荐】新一代 NoSQL 数据库,Aerospike专区新鲜入驻

相关博文:
· kubernetes之部署java项目
· Kubernetes
· kubernetes
· Kubernetes Ingress
· Kubernetes Nacos
» 更多推荐…

最新 IT 新闻:
· 产能利用率仅5.3%,柔宇上市是假扩张、真圈钱?
· 体检异常率99%,这届打工人的身体有多差?
· 虾米音乐停止运营:8年前被阿里收购 曾一起走过洪荒岁月
· 那个想去网易云的女孩倒在了拼多多
· 蔚来首款旗舰轿车专利图曝光!续航有望超900km
» 更多新闻…
昵称: 程序员欣宸
园龄: 8年
粉丝: 17
关注: 4
+加关注
< 2021年1月 >
日 一 二 三 四 五 六
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6
Search

My Tags
CI(1)
DevOps(1)
GitLab(1)
java(1)
springboot(1)
PostArchives
2021/1(3)
2020/12(13)
2020/11(23)
2020/10(27)
2020/9(6)
2020/6(7)
2019/11(5)
2019/10(6)
2019/9(41)
2019/8(2)
Recent Comments

  1. Re:群晖DS218+部署GitLab
    大佬,麻烦问下内存是哪个牌子的吗?可以私信连接吗?

–a403828237
2. Re:Flink SQL Client综合实战
老师您好,我是开源中国的内容志愿者李艳,想跟您聊聊内容合作,能否加下您的微信或qq给您详细说明下

–爱吃甜品的女孩
3. Re:K8S的Kafka监控(Prometheus+Grafana)
666呀!

–monkey’s
4. Re:K8S的Kafka监控(Prometheus+Grafana)
@通用C#系统架构 谢谢您的反馈…
–程序员欣宸
5. Re:K8S的Kafka监控(Prometheus+Grafana)
非常好的文章,整套能搭建起来很棒。

–通用C#系统架构
Top Posts

  1. 树莓派4B安装64位Linux(不用显示器键盘鼠标)(12417)
  2. 设置IntelliJ IDEA支持lambda表达式(7458)
  3. Jenkins流水线(pipeline)实战之:从部署到体验(2626)
  4. 树莓派4B安装docker-compose(64位Linux)(2150)
  5. 让docker中的mysql启动时自动执行sql(2139)
    推荐排行榜
  6. Jenkins把GitHub项目做成Docker镜像(3)
  7. GitLab CI构建SpringBoot-2.3应用(2)
  8. springboot的jar为何能独立运行(2)
  9. 设置非root账号不用sudo直接执行docker命令(2)
  10. 群晖DS218+部署mysql(2)
    Copyright © 2021 程序员欣宸
    Powered by .NET 5.0 on Kubernetes
Logo

开源、云原生的融合云平台

更多推荐