zl程序教程

您现在的位置是:首页 >  后端

当前栏目

K8S Java Client 17.0 已经发布,新功能试用一览

JAVAk8s 功能 发布 已经 Client 一览 试用
2023-06-13 09:17:34 时间

Kubernetes Java Client[1] 17.0.0 的 发布[2] 提供了对 Kubernetes 1.25 的[3]支持[4],提供了动态检索信息的能力,例如用于监控目的,并允许更改和删除 Kubernetes 集群中的对象。Kubernetes 客户端可以用作命令行 Kubernetes 工具kubectl [argument]的替代品。

添加如下 Maven 依赖后即可使用 Kubernetes Java Client:

<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,示例如下[5]

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() 方法通过指定方法的参数提供了许多配置选项:

public V1PodList listPodForAllNamespaces(
    Boolean allowWatchBookmarks,
    String _continue,
    String fieldSelector,
    String labelSelector,
    Integer limit,
    String pretty,
    String resourceVersion,
    String resourceVersionMatch,
    Integer timeoutSeconds,
    Boolean watch)

除了检索信息之外,还可以更改项目,甚至删除项,例如 从一个 namespace 中移除 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 命令显示来自正在运行的容器的日志,类似于以下 API 调用:

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 命令。例如,要观察 namespace 中的变化并打印它们:

Watch<V1Namespace> watch =
    Watch.createWatch(
        client,
        api.listNamespaceCall(null, null, null, null, null, 5, null, null,
            null, Boolean.TRUE, null),
        new TypeToken<Watch.Response<V1Namespace>>() {}.getType());

try {
    for (Watch.Response<V1Namespace> reponse : watch) {
    System.out.printf("Response type:" + response.type + " name: " +
        response.object.getMetadata().getName());
    }
} finally {
 watch.close();
}

一些高级用例需要 client-java-extended 添加以下 Maven 依赖项后才能使用的模块:

<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());
}

更多信息和示例可以在文档[6]中找到。

参考资料

[1]

Kubernetes Java Client: https://github.com/kubernetes-client/java

[2]

发布: https://github.com/kubernetes-client/java/releases/tag/v17.0.0

[3]

Kubernetes 1.25 的: https://kubernetes.io/

[4]

支持: https://github.com/kubernetes-client/java/wiki/2.-Versioning-and-Compatibility

[5]

示例如下: https://raw.githubusercontent.com/kubernetes-client/java/master/kubernetes/src/main/java/io/kubernetes/client/openapi/apis/CoreV1Api.java

[6]

文档: https://github.com/kubernetes-client/java/wiki

- END -