[英]I need to get number of Pods in a Kubernetes Cluster with kubernetes python client
[英]I need to get resource usage of Pods in a Kubernetes Cluster with kubernetes python client
我有 linux 命令来获取特定命名空间中 pod 的资源使用情况 等效的 python 命令是什么
$ kubectl top pod
NAME CPU(cores) MEMORY(bytes)
nginx-deployment-7fd6966748-57mt5 0m 2Mi
nginx-deployment-7fd6966748-jpbjl 0m 2Mi
nginx-deployment-7fd6966748-snrx4 0m 2Mi
没有单一的命令。
Kubernetes 指标在 metrics.k8s.io 上公开。 您必须编写程序来查询 API 并获得结果。
例如对于节点。
此 API 在 /apis/metrics.k8s.io/ 端点下可用
kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes" | jq 用于节点指标
感谢您的输入,以下命令运行良好
>>> from kubernetes import client, config
>>>
>>> config.load_kube_config()
>>> resource = api.list_namespaced_custom_object(group="metrics.k8s.io",version="v1beta1", namespace="default", plural="pods")
>>> for pod in resource["items"]:
... print(pod['containers'], "\n")
...
您也可以使用 python 运行 kubectl 命令。
from subprocess import call
call('kubectl describe pod pod_name_here -n default', shell=True)
因此,您可以使用 Python Kubernetes 库过滤任何内容,还可以使用 kubectl 命令来简化您的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.