Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to get resource usage of Pods in a Kubernetes Cluster with kubernetes python client

i have linux command to get the resource usage of the pods in particular namespace what is the equivalent python command for it

$ 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
like image 618
Pradeep Padmanaban C Avatar asked Sep 17 '25 05:09

Pradeep Padmanaban C


1 Answers

Thanks for the input the following command worked fine

>>> from kubernetes import client, config
>>> 
>>> config.load_kube_config()
>>> api = client.CustomObjectsApi()
>>> 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")
...
like image 145
Pradeep Padmanaban C Avatar answered Sep 19 '25 19:09

Pradeep Padmanaban C