Find Top 10 Restarting Kubernetes Containers
Identify the most unstable containers across all namespaces in a Kubernetes cluster by sorting their restart counts.
Setup
- → kubectl configured with cluster access
- → jq
Cost per run
Free
The one-liner
$ kubectl get pods -A -o json | jq -r '.items[] | select(.status.containerStatuses != null) | .metadata.namespace as $ns | .metadata.name as $pod | .status.containerStatuses[] | "\(.restartCount)\t\($ns)\t\($pod)\t\(.name)"' | sort -rn | head -n 10What each stage does
- [01] kubectl
kubectl get pods -A -o jsonFetches all pods across all namespaces and outputs the raw data in JSON format. - [02] jq
jq -r '.items[] | select(.status.containerStatuses != null)'Filters out pods that haven't scheduled or don't have container status information yet. - [03] jq
.metadata.namespace as $ns | .metadata.name as $podSaves the namespace and pod name into variables to include them alongside the container data. - [04] jq
.status.containerStatuses[] | "\(.restartCount)\t\($ns)\t\($pod)\t\(.name)"Iterates over each container, formatting a tab-separated string with restarts, namespace, pod, and container name. - [05] sort
sort -rnSorts the output numerically in reverse order so the highest restart counts appear first. - [06] head
head -n 10Limits the output to the top 10 worst offending containers.
Expected output (sample)
342 kube-system calico-node-z9q2w calico-node 128 monitoring prometheus-adapter-5b5f5c prometheus-adapter 45 default flaky-app-deployment-848 backend 12 ingress-nginx ingress-nginx-controller controller 3 kube-system coredns-5d78c9869d-x2j coredns
Caveats & tips
- Fetching all pods as JSON in very large clusters (10,000+ pods) can consume significant memory and API server bandwidth, potentially causing timeouts.
- Requires cluster-wide read permissions (`get pods` at the cluster scope) which may be restricted by RBAC policies.