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

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


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)


To add to @rico's answer,

for pod in api_response.items:    status = pod.status.phase        container_status = pod.status.container_statuses[0]    if container_status.started is False or container_status.ready is False:        waiting_state = container_status.state.waiting        if waiting_state.message is not None and 'Error' in waiting_state.message:            status = waiting_state.reason    print(pod.metadata.name + " " + status)

You can also loop on the api call 'api_instance.list_namespaced_pod' to see the statuses changing online like in kubectl -n $NAMESPACE get pods -w