使用Kubernetes Java Client创建shared informer控制器监听Pod事件示例咨询
前置依赖
首先在项目中引入Kubernetes Java Client官方依赖,Maven配置示例如下:
<dependency> <groupId>io.kubernetes</groupId> <artifactId>client-java</artifactId> <version>19.0.0</version> <!-- 可替换为最新稳定版本 --> </dependency>
完整实现代码
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.V1Pod; import io.kubernetes.client.util.ClientBuilder; import io.kubernetes.client.informer.SharedInformer; import io.kubernetes.client.informer.SharedInformerFactory; import io.kubernetes.client.informer.ResourceEventHandler; import java.util.concurrent.TimeUnit; public class PodEventMonitor { // 替换为你需要监听的目标命名空间 private static final String TARGET_NAMESPACE = "your-target-namespace"; // 全量资源同步周期,单位毫秒,设置为0则关闭全量同步 private static final long RESYNC_PERIOD = TimeUnit.MINUTES.toMillis(10); public static void main(String[] args) throws Exception { // 初始化Kubernetes客户端,本地运行默认读取~/.kube/config,集群内运行自动读取ServiceAccount凭证 ApiClient apiClient = ClientBuilder.defaultClient(); Configuration.setDefaultApiClient(apiClient); CoreV1Api coreV1Api = new CoreV1Api(apiClient); // 创建SharedInformer工厂实例 SharedInformerFactory informerFactory = new SharedInformerFactory(); // 注册Pod资源的Informer,指定监听的命名空间 SharedInformer<V1Pod> podInformer = informerFactory.sharedIndexInformerFor( V1Pod.class, callParams -> coreV1Api.listNamespacedPodCall( TARGET_NAMESPACE, callParams.getPretty(), callParams.getAllowWatchBookmarks(), callParams.get_continue(), callParams.getFieldSelector(), callParams.getLabelSelector(), callParams.getLimit(), callParams.getResourceVersion(), callParams.getResourceVersionMatch(), callParams.getTimeoutSeconds(), callParams.getWatch(), null ), RESYNC_PERIOD, TARGET_NAMESPACE ); // 添加Pod事件处理回调 podInformer.addEventHandler(new ResourceEventHandler<V1Pod>() { @Override public void onAdd(V1Pod newPod) { // 处理Pod创建事件,替换为自身业务逻辑 System.out.printf("监听到Pod创建:%s/%s,当前状态:%s%n", newPod.getMetadata().getNamespace(), newPod.getMetadata().getName(), newPod.getStatus().getPhase()); } @Override public void onUpdate(V1Pod oldPod, V1Pod newPod) { // 处理Pod更新事件,替换为自身业务逻辑,可对比oldPod和newPod的字段差异做定向处理 System.out.printf("监听到Pod更新:%s/%s,旧状态:%s,新状态:%s%n", newPod.getMetadata().getNamespace(), newPod.getMetadata().getName(), oldPod.getStatus().getPhase(), newPod.getStatus().getPhase()); } @Override public void onDelete(V1Pod deletedPod, boolean deletedFinalStateUnknown) { // 处理Pod删除事件,替换为自身业务逻辑 System.out.printf("监听到Pod删除:%s/%s,状态是否未知:%b%n", deletedPod.getMetadata().getNamespace(), deletedPod.getMetadata().getName(), deletedFinalStateUnknown); } }); // 启动所有已注册的Informer informerFactory.startAllRegisteredInformers(); // 阻塞主线程保持监听状态 Thread.currentThread().join(); } }
注意事项
- 如果需要监听集群所有命名空间的Pod,将
TARGET_NAMESPACE变量设置为空字符串即可- 事件回调中不要执行耗时同步逻辑,避免阻塞Informer的事件队列,耗时操作建议提交到异步线程池处理
- 程序在集群内运行时,需要为运行程序的ServiceAccount配置对应命名空间Pod的
list、watch权限- 不需要全量同步Pod资源的场景可以将
RESYNC_PERIOD设置为0,降低资源消耗
内容的提问来源于stack exchange,提问作者coder123
相关产品推荐
相关产品推荐

