如何通过python客户端获取Kubernetes中Pod的日志和描述



我想通过python客户端在kubernetes中获取我的pod的日志和描述。在 kubernetes 集群中,我们可以使用

kubectl logs <NAME_OF_POD>
kubectl describe pods <NAME_OF_pod>

但是我想在 kubernetes 的 python 客户端中使用这些命令,我该怎么办?

您可以使用以下代码读取 Pod 的日志:

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_log(name=pod_name, namespace='default')
print(api_response)
except ApiException as e:
print('Found exception in reading the logs')

上面的代码非常适合获取 pod 的日志。

为了得到kubectl describe pod的输出,所有提供的信息都在read_namespaced_pod函数中。它包含您需要的所有信息,您可以以所需的任何方式使用该信息。您可以编辑上面的代码并使用read_namespaced_pod代替read_namespaced_pod_log来获取信息。

由于 Kubernetes 使用的是 REST API,这为您提供了通过 python 调用日志和对象描述的所有可能性。

首先,您需要找出您的授权机制。您可以根据集群找到它。

kubectl config view --raw

然后,您可以使用此身份验证方法创建 api 调用。例如,在下面的示例中,我使用了基本身份验证来获取 pod 日志。

import json
import requests
from requests.auth import HTTPBasicAuth
user='admin'
password='password'
url='https://cluster-api-url/api/v1/namespaces/default/pods/nginx-ingress-controller-7bbcbdcf7f-dgr57/log'
requests.packages.urllib3.disable_warnings()
resp = requests.get(url, auth=HTTPBasicAuth(user, password), verify=False, json=False)
print(resp.text)

要轻松获取 url,请键入带有"--v=8"参数的命令。例如,获取用于描述 pod 的 url

kubectl describe pod nginx-ingress-controller-7bbcbdcf7f-dgr57 --v=8

并检查以上部分实际输出

I0514 12:31:42.376972  216066 round_trippers.go:383] GET https://cluster-api-url/api/v1/namespaces/default/events?fieldSelector=involvedObject.namespace%3Ddefault%2CinvolvedObject.uid%3D1ad92455-7589-11e9-8dc1-02a3436401b6%2CinvolvedObject.name%3Dnginx-ingress-controller-7bbcbdcf7f-dgr57
I0514 12:31:42.377026  216066 round_trippers.go:390] Request Headers:
I0514 12:31:42.377057  216066 round_trippers.go:393]     Accept: application/json, */*
I0514 12:31:42.377074  216066 round_trippers.go:393]     Authorization: Basic YWRtaW46elRoYUJoZDBUYm1FbGpzbjRtYXZ2N1hqRWlvRkJlQmo=
I0514 12:31:42.377090  216066 round_trippers.go:393]     User-Agent: kubectl/v1.12.0 (linux/amd64) kubernetes/0ed3388

GET https://<URL>部分复制 URL 并在 python 脚本中随url进行更改,然后你就可以了。

希望对你有帮助

这些答案都没有返回命名空间 pod 的事件,这是在运行kubectl describe时默认给出的。 要获取给定 Pod 的命名空间事件,请运行:

from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
out = kube.core_api.list_namespaced_event(namespace, field_selector=f'involvedObject.name={pod_name}')

其中namespace是感兴趣的命名空间,pod_name是感兴趣的 Pod。

在生成 Pod 并为用户提供有关 Pod 当前状况的合理状态报告以及调试 pod 的状态(如果它无法超出"挂起"时)时,我需要它。

你需要探索官方 https://github.com/kubernetes-client/python K8s Python客户端。

但是我找不到任何符合您要求的特定文档。我认为,下面的链接是起点,

https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md

尝试对对象执行dir并查看可用的方法。例如,下面来自自述文件的代码

from kubernetes import client, config
# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
print("%st%st%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) 

dir(v1)dir(ret)并查看方法/变量等。或者list*方法可能会为您提供您在kubectl describe pod <name>中看到的详细信息

最新更新