Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Kubernetes Client equivalent of kubectl get pods

I'm trying to use the Python Kubernetes Client to get the run-time of each container for all my pods. Using the K8s CLI this information is available by using kubectl describe pods.

I can get the output for a single pod by using

api_response = api_instance.read_namespaced_pod(name='pod-name',namespace='namespace-name')

However, I need this information for all the pods.

What is the equivalent of kubectl get podsfor Python K8s library? I'm thinking I can use this to create a list of pods and use the mentioned command above to loop through them by their pod-name and get the required information.

like image 343
aideen Avatar asked Oct 15 '25 14:10

aideen


2 Answers

From the docs you can try this api_response = api_instance.list_namespaced_pod(namespace='namespace-name')

like image 101
Arghya Sadhu Avatar answered Oct 17 '25 04:10

Arghya Sadhu


You are right about using this command to get all pod names:

api_response = api_instance.read_namespaced_pod(name='pod-name',namespace='namespace-name')

But to get specifically the name of those, use something like this:

for i in api_response.items:
    print("%s" %(i.metadata.name))

That should work for you :)

like image 24
Maria Clara Bezerra Avatar answered Oct 17 '25 02:10

Maria Clara Bezerra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!