Kubernetes Java Client 17.0.0 发布总结
主要观点
Kubernetes Java Client 17.0.0 版本发布,支持 Kubernetes 1.25,提供了动态检索信息、修改和删除 Kubernetes 集群中项目的能力。该客户端可以作为命令行工具 kubectl 的替代方案。
关键信息
- 支持 Kubernetes 1.25:该版本增加了对 Kubernetes 1.25 的支持。
- 动态检索信息:可用于监控等目的。
- 修改和删除项目:允许在 Kubernetes 集群中修改和删除项目,如 Pod。
重要细节
依赖配置
Maven 依赖:
<dependency> <groupId>io.kubernetes</groupId> <artifactId>client-java</artifactId> <version>17.0.0</version> </dependency>Gradle 依赖:
compile 'io.kubernetes:client-java:15.0.1'
核心功能
CoreV1API:提供了大量方法,例如检索所有 Pod:
ApiClient apiClient = Config.defaultClient(); Configuration.setDefaultApiClient(apiClient); CoreV1Api api = new CoreV1Api(); V1PodList podList = api.listPodForAllNamespaces( null, null, null, null, null, null, null, null, null, null); for (V1Pod pod : podList.getItems()) { System.out.println("Pod name: " + pod.getMetadata().getName()); }- 方法参数配置:
listPodForAllNamespaces()方法提供了多种配置选项。 修改和删除项目:例如删除命名空间中的 Pod:
Call call = deleteNamespacedPodCall( String name, String namespace, String pretty, String dryRun, Integer gracePeriodSeconds, Boolean orphanDependents, String propagationPolicy, V1DeleteOptions body, final ApiCallback _callback)
日志和事件监控
日志检索:类似于
kubectl logs命令:PodLogs logs = new PodLogs(); V1Pod pod = api.listNamespacedPod( "default", "false", null, null, null, null, null, null, null, null, null) .getItems() .get(0); InputStream inputStream = logs.streamNamespacedPodLog(pod);- 事件监控:通过设置
watch参数为Boolean.TRUE来监控事件,类似于kubectl get <resource> -w命令。
高级功能
client-java-extended 模块:用于高级用例,如分页请求:
<dependency> <groupId>io.kubernetes</groupId> <artifactId>client-java-extended</artifactId> <version>17.0.0</version> </dependency>Gradle 依赖:
implementation 'io.kubernetes:client-java-extended:17.0.0'分页请求:减少服务器负载和网络流量,例如每次检索五个命名空间:
Pager<V1Namespace, V1NamespaceList> pager = new Pager<>((Pager.PagerParams param) -> { try { return api.listNamespaceCall(null, null, param.getContinueToken(), null, null, param.getLimit(), null, null, 1, null, null); } catch (Exception e) { // Handle exception } }, client, 5, V1NamespaceList.class); for (V1Namespace namespace : pager) { System.out.println("Namespace name: " + namespace.getMetadata().getName()); }
文档和示例
更多信息和示例可在 文档 中找到。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用。你还可以使用@来通知其他用户。