如何通过python客户端获取kubernetes中的pod的日志和描述 [英] How to get log and describe of pods in kubernetes by python client

查看:156
本文介绍了如何通过python客户端获取kubernetes中的pod的日志和描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get log and describe of my pod in kubernetes by python client. In kubernetes cluster we can use

kubectl logs <NAME_OF_POD>
kubectl describe pods <NAME_OF_pod>

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

But I want these command in python client of kubernetes.what should I do?

推荐答案

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

You can read the logs of a pod using the following code:

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的日志.

The above code works perfectly fine for getting the logs of pod.

要获取 kubectl describe pod 的输出,提供的所有信息均在 read_namespaced_pod 函数中.它具有您需要的所有信息,您可以以任何需要的方式使用该信息.您可以编辑以上代码,并使用 read_namespaced_pod 代替 read_namespaced_pod_log 来获取信息.

To get the output of kubectl describe pod, all the information provided is in read_namespaced_pod function. It has all the information you require, and you can use that information in whatever way you require. You can edit the above code and use read_namespaced_pod in place of read_namespaced_pod_log to get the info.

这篇关于如何通过python客户端获取kubernetes中的pod的日志和描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆