文章目录
1、版本选择
spring-cloud-kubernetes同时支持了k8s官方的java client sdk,以及fabric8的client sdk。直接选用它引入的版本(如下)就OK,避免一些兼容性坑,适合本地虚拟机的k8s v1.19.16版本应该正好OK。
pom
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>19.0.1</version>
</dependency>
pom
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>6.13.4</version>
</dependency>
也可以直接使用以下pom配置锁定版本
pom
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>-->
</dependencies>
</dependencyManagement>
IDEA运行配置使用远程Java环境,也就是k8s master节点进行执行,因为创建client对象默认需要加载
~/.kube/config
认证信息以访问k8s apiserver
2、k8s官方Java sdk测试
官方sdk监听pod测试
java
package com.example.myk8s.demo;
import com.google.gson.reflect.TypeToken;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.Watch;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class k8sOfficePodWatcher {
@SneakyThrows
public static void main(String[] args) {
Configuration.setDefaultApiClient(Config.defaultClient());
CoreV1Api api = new CoreV1Api();
Watch<V1Pod> watch = Watch.createWatch(
api.getApiClient(),
api.listNamespacedPodCall("default", null, null, null, null, null, null, null, null, null, null, true, null),
new TypeToken<Watch.Response<V1Pod>>() {
}.getType()
);
watch.forEach(event -> {
System.out.println(event.type);
switch (event.type) {
case "ADDED" -> log.info("Pod created: {}", event.object.getMetadata().getName());
case "MODIFIED" -> log.info("Pod modified: {}", event.object.getMetadata().getName());
case "DELETED" -> log.info("Pod deleted: {}", event.object.getMetadata().getName());
default -> log.info("Unhandled event type: {}", event.type);
}
});
}
}
3、fabric8 sdk测试
fabric8 sdk监听pod测试
java
package com.example.myk8s.demo;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.*;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class k8sFabric8PodWatcher {
public static void main(String[] args) {
Config config = new ConfigBuilder().build();
KubernetesClient client = new KubernetesClientBuilder().withConfig(config).build();
client.pods().inNamespace("default").watch(new Watcher<Pod>() {
@Override
public void eventReceived(Action action, Pod pod) {
switch (action) {
case ADDED -> log.info("Pod created: {}", pod.getMetadata().getName());
case MODIFIED -> log.info("Pod modified: {}", pod.getMetadata().getName());
case DELETED -> log.info("Pod deleted: {}", pod.getMetadata().getName());
default -> log.info("Unhandled event type: {} for pod {}", action, pod.getMetadata().getName());
}
}
@Override
public void onClose(WatcherException cause) {
if (cause != null) {
log.error("Watch closed due to an error: {}", cause.getMessage());
} else {
log.info("Watch closed normally.");
}
}
});
}
}
4、总结
1、官方sdk提供Java,go,python等多种语言版本,fabric8仅Java
2、官方sdk api稍难用一点,fabric8有更友好的api,比如基于fabric8 的创建出来的client对象可以直接操作各种资源,且大量使用builder模式构建参数对象,基本不用看文档就能玩。
3、fabric8内置了更多的高级功能,比如模拟框架、更简单的配置选项以及更好的错误处理机制,支持自定义资源定义,并且更容易集成到 CI/CD 流程中。