【问题标题】:Listening for pod fail in kubernetes java client?在 kubernetes java 客户端中监听 pod 失败?
【发布时间】:2020-10-08 12:41:21
【问题描述】:

我是 kubernetes 的新手,我需要编写一个程序来监听 kubernetes 的变化,例如新的 pod 创建或 pod 删除。对于这些,我已经有了解决方案,但我不知道如何监听 pod 的失败,有人可以帮忙吗?我用的是kubernetes java官方客户端,但是我也可以切换到另一种语言来解决问题,谢谢

【问题讨论】:

  • 您的集群是自行管理的还是由某个云提供商管理的?一个好的起点应该是 kubernetes auditing。请更详细地描述您的集群以及您想要监控的确切内容。

标签: kubernetes kubernetes-pod


【解决方案1】:

您可以查看 pod 状态,这里有一些示例到 https://github.com/kubernetes-client/java

你可以通过这个例子来获取pod状态:

import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.Config;

import java.io.IOException;

public class Example {
    public static void main(String[] args) throws IOException, ApiException{
        ApiClient client = Config.defaultClient();
        Configuration.setDefaultApiClient(client);

        CoreV1Api api = new CoreV1Api();
        V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
        for (V1Pod item : list.getItems()) {
            System.out.println(item.getMetadata().getName());
            System.out.println(item.getStatus());
        }
    }
}

你也可以观看:

import com.google.gson.reflect.TypeToken;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Namespace;
import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.Watch;

import java.io.IOException;

public class WatchExample {
    public static void main(String[] args) throws IOException, ApiException{
        ApiClient client = Config.defaultClient();
        Configuration.setDefaultApiClient(client);

        CoreV1Api api = new CoreV1Api();

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

        for (Watch.Response<V1Namespace> item : watch) {
            System.out.printf("%s : %s%n", item.type, item.object.getMetadata().getName());
        }
    }
}

【讨论】:

    【解决方案2】:

    正如用户 Ruben 之前提到的,我们可以实现一个手表。为了更进一步,我还提供了一个关于如何运行健康检查的示例。以下代码块只是 MODIFIED 情况下 pod 状态的 watch 事件。

    try (Watch<V1Pod> watch = Watch.createWatch(
            client,
            coreV1Api.listNamespacedPodCall(namespace, null, null, null, null,
                    labels, null, null, null, null, Boolean.TRUE, null),
            new TypeToken<Watch.Response<V1Pod>>() {
            }.getType())) {
    
        for (Watch.Response<V1Pod> eventPod : watch) {
            V1Pod pod = eventPod.object;
            switch (eventPod.type) {
                case "ADDED":
                    System.out.println("Pod: " + pod.getMetadata().getName() + " ADDED\n");
                    break;
                case "MODIFIED":
                    System.out.println("Pod: " + pod.getMetadata().getName() + " has been MODIFIED\n");
                    // Check to see the current status of the pod
                    if (!pod.getStatus().getPhase().equals("Running")){
                        System.out.println("Pod: " + pod.getMetadata.getName() + " is now " + pod.getStatus());
                    }
                    break;
                case "DELETED":
                    System.out.println("Pod: " + pod.getMetadata().getName() + " has been DELETED\n");
                default:
                    System.out.println("Unknown Event Occurred: " + eventPod.type + "\n");
            }
        }
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
    

    Kubernetes defines the different statuses,您可以将其作为运行状况检查条件的一部分来实现。

    【讨论】:

      猜你喜欢
      • 2016-11-07
      • 2022-01-02
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      相关资源
      最近更新 更多