Kubernetes: How do I get all pods in a namespace using the python api?

9/14/2018

I am using python to access my cluster. The only thing I came across, that is close is list_namespaced_pod, which does not give me the actual names of the pods.

-- A_test_user
kubernetes
namespaces
python

1 Answer

1/7/2019

As stated in the comments, you can access all information in the metadata of each pod in the list of pod items returned by the API call.

Here is an example:

def get_pods():  

    v1 = client.CoreV1Api()
    pod_list = v1.list_namespaced_pod("example")
    for pod in pod_list.items:
        print("%s\t%s\t%s" % (pod.metadata.name,
                              pod.status.phase,
                              pod.status.pod_ip))
-- Baily
Source: StackOverflow