https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;
名称 | 链接 | 备注 |
---|---|---|
项目主页 | 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>fluent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fluent</name>
<description>Demo project for fluent style</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>
public static void main(String[] args) {
SpringApplication.run(FluentStyleApplication.class, args);
}
private final static String NAMESPACE = "fluent";
/**
* 默认的全局设置
* @return
* @throws Exception
*/
@PostConstruct
private void setDefaultApiClient() 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();
// 会打印和API Server之间请求响应的详细内容,生产环境慎用
client.setDebugging(true);
// 创建操作类
Configuration.setDefaultApiClient(client);
}
@RequestMapping(value = "/fluent/createnamespace")
public V1Namespace createnamespace() throws Exception {
V1Namespace v1Namespace = new V1NamespaceBuilder()
.withNewMetadata()
.withName(NAMESPACE)
.addToLabels("label1", "aaa")
.addToLabels("label2", "bbb")
.endMetadata()
.build();
return new CoreV1Api().createNamespace(v1Namespace, null, null, null);
}
为了更清晰的展现fluent style效果,将上述代码与创建namespace的yaml文件内容放在一起对比,如下图所示,可见对照着yaml文件就能将代码写出来:
接下来是创建service的代码,为了便于和yaml对应起来,代码中特意加了缩进:
@RequestMapping(value = "/fluent/createservice")
public V1Service createservice() throws Exception {
V1Service v1Service = new V1ServiceBuilder()
// meta设置
.withNewMetadata()
.withName("nginx")
.endMetadata()
// spec设置
.withNewSpec()
.withType("NodePort")
.addToPorts(new V1ServicePort().port(80).nodePort(30103))
.addToSelector("name", "nginx")
.endSpec()
.build();
return new CoreV1Api().createNamespacedService(NAMESPACE, v1Service, null, null, null);
}
@RequestMapping(value = "/fluent/createdeployment")
public ExtensionsV1beta1Deployment createdeployment() throws Exception {
ExtensionsV1beta1Deployment v1Deployment = new ExtensionsV1beta1DeploymentBuilder()
// meta设置
.withNewMetadata()
.withName("nginx")
.endMetadata()
// spec设置
.withNewSpec()
.withReplicas(1)
// spec的templat
.withNewTemplate()
// template的meta
.withNewMetadata()
.addToLabels("name", "nginx")
.endMetadata()
// template的spec
.withNewSpec()
.addNewContainer()
.withName("nginx")
.withImage("nginx:1.18.0")
.addToPorts(new V1ContainerPort().containerPort(80))
.endContainer()
.endSpec()
.endTemplate()
.endSpec()
.build();
return new ExtensionsV1beta1Api().createNamespacedDeployment(NAMESPACE, v1Deployment, null, null, null);
}
@RequestMapping(value = "/fluent/clear")
public String clear() throws Exception {
// 删除deployment
try {
new ExtensionsV1beta1Api().deleteNamespacedDeployment("nginx", NAMESPACE, null, null, null, null, null, null);
} catch (Exception e)
{
log.error("delete deployment error", e);
}
CoreV1Api coreV1Api = new CoreV1Api();
// 删除service
coreV1Api.deleteNamespacedService("nginx", NAMESPACE, null, null, null, null, null, null);
// 删除namespace
try {
coreV1Api.deleteNamespace(NAMESPACE, null, null, null, null, null, null);
} catch (Exception e)
{
log.error("delete namespace error", e);
}
return "clear finish, " + new Date();
}
将fluent工程直接在IEDA环境启动;
浏览器访问:http://localhost:8080/fluent/createnamespace ,页面会展示API Server返回的完整namespace信息:
浏览器访问:http://localhost:8080/fluent/createservice ,页面会展示API Server返回的完整service信息:
浏览器访问:http://localhost:8080/fluent/createdeployment ,页面会展示API Server返回的完整deployment信息:
验证前面几个接口创建的服务是否可用,我这里kubernetes的IP地址是192.168.50.135,因此访问:http://192.168.50.135:30103 ,可以正常显示nginx首页:
SSH登录kubernetes服务器查看,通过kubernetes的java客户端创建的资源都正常:
验证完成后,浏览器访问:http://localhost:8080/fluent/clear ,即可清理掉前面三个接口创建的资源;
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
https://github.com/zq2599/blog_demos
Kubernetes官方java客户端之八:fluent style
原文:https://www.cnblogs.com/bolingcavalry/p/14275339.html