Kubernetes: How do I get the status and restarts of a container using python?

10/5/2018

I would like to get the container status and restarts of a pod, using python kubernetes. I am currently using list_namespaced_pod to get the pods and the container status, which I interpret as the status of the pod. This works, but is far from an ideal solution. I am looking for something that gives me similar information to kubectl -n $NAMESPACE get pods.

-- A_test_user
kubernetes
python
restart
status

1 Answer

10/5/2018

You can do the following for example, to get all the pods and their status:

api_response = api_instance.list_namespaced_pod(namespace, pretty=pretty, timeout_seconds=timeout_seconds, watch=watch)

for i in api_response.items:
    print(i.metadata.name + " " + i.status.phase)
-- Rico
Source: StackOverflow