Alert for Pods not Running in Kubernetes

Luca Berton
3 min readOct 30, 2024

To get alerts or insights on pods that meet specific conditions like a high number of restarts, not being in a “Running” state, or being stuck in the “Init” state in a Kubernetes cluster, you can use kubectl commands combined with filtering.

Here are different ways to gather this information:

1. Pods with High Number of Restarts:

To list pods with more than, say, 3 restarts:

kubectl get pods --all-namespaces --field-selector=status.containerStatuses.restartCount>3

If you want to list all pods with any restarts, you can use:

kubectl get pods --all-namespaces --sort-by='.status.containerStatuses[0].restartCount'

This will give you a list of pods sorted by the number of restarts.

You can further refine this using jsonpath to display the number of restarts explicitly:

kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\t"}{.status.containerStatuses[0].restartCount}{"\n"}{end}' | awk '$3 > 3'

2. Pods Not in a “Running” State:

To get all pods that are not in the Running state:

kubectl get pods…

--

--

Luca Berton
Luca Berton

Written by Luca Berton

I help creative Automation DevOps, Cloud Engineer, System Administrator, and IT Professional to succeed with Ansible Technology to automate more things everyday