通过kubernetes python API获取Readynes探测器的状态

2024-10-03 06:30:17 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我跑

kubectl -n=mynamespace describe pod mypodname 

例如,我得到以下输出:

  Type     Reason     Age                      From                  Message
  ----     ------     ----                     ----                  -------
  Warning  Unhealthy  43s (x30458 over 3d17h)  kubelet, myhostname  Readiness probe failed: HTTP probe failed with statuscode: 404

现在,我如何通过python库https://github.com/kubernetes-client/python获得这个

我可以获取POD信息,例如,对所有名称空间使用list_POD_,并获取一个POD对象https://github.com/kubernetes-client/python/blob/6d64cf67d38337a6fdde1908bdadf047b7118731/kubernetes/docs/V1Pod.md

但是这只会告诉我(在podobject.status.conditions属性中)容器还没有准备好,但是我没有得到像上面404状态码那样的详细信息

有什么想法吗

谢谢大家!


Tags: httpsgithubcomclienttypekubernetesprobepod
1条回答
网友
1楼 · 发布于 2024-10-03 06:30:17

1)首先,您可以在这里找到所有方法:CoreV1Api.md

2)您感兴趣的零件:

[**read_namespaced_pod**](CoreV1Api.md#read_namespaced_pod) | **GET** /api/v1/namespaces/{namespace}/pods/{name} | 
[**read_namespaced_pod_log**](CoreV1Api.md#read_namespaced_pod_log) | **GET** /api/v1/namespaces/{namespace}/pods/{name}/log | 
[**read_namespaced_pod_status**](CoreV1Api.md#read_namespaced_pod_status) | **GET** /api/v1/namespaces/{namespace}/pods/{name}/status | 
[**read_namespaced_pod_template**](CoreV1Api.md#read_namespaced_pod_template) | **GET** /api/v1/namespaces/{namespace}/podtemplates/{name} | 

3)你的正确方法是read_namespaced_pod

4)您的代码取自How to get log and describe of pods in kubernetes by python client的@Prafull Ladha

from kubernetes.client.rest import ApiException
from kubernetes import client, config

config.load_kube_config()
pod_name = "counter"
try:
    api_instance = client.CoreV1Api()
    api_response = api_instance.read_namespaced_pod(name=pod_name, namespace='default')
    print(api_response)

相关问题 更多 >