How do you list pods with high restart counts using the Python client?
BeginnerConceptKubernetes, Docker & SSH clients
Answer
v1 = client.CoreV1Api()
for pod in v1.list_pod_for_all_namespaces(watch=False, limit=500).items:
for cs in pod.status.container_statuses or []:
if cs.restart_count > 5:
reason = cs.last_state.terminated.reason if cs.last_state.terminated else "-"
print(pod.metadata.namespace, pod.metadata.name, cs.name, cs.restart_count, reason)
Note container_statuses can be None for pending pods. For large clusters, paginate with limit and _continue=resp.metadata._continue, and use label_selector/field_selector to filter on the server.
More on Kubernetes, Docker & SSH clients
How does the official Kubernetes Python client authenticate?BeginnerHow do you patch or scale a Deployment with the Python client?IntermediateHow does `watch.Watch()` work, and what is a `410 Gone`?AdvancedHow do you apply a YAML manifest from Python?AdvancedHow do you use the Docker SDK for Python?BeginnerHow do you clean up dangling Docker images and stopped containers with Python?Intermediate