Get multiple time restarted pod list in kubernate

Viewed 1026

From the below pods, how can we get a list of pods which have been restarted more than 2 times. How can we get in a single line query ?

xx-5f6df977d7-4gtxj                 3/3     Running            0          6d21h
xx-5f6df977d7-4rvtg                 3/3     Running            0          6d21h
pkz-ms-profile-df9fdc4f-2nqvw       1/1     Running            0          76d
push-green-95455c5c-fmkr7           3/3     Running            3          15d
spice-blue-77b7869847-6md6w         2/2     Running            0          19d
bang-blue-55845b9c68-ht5s5             1/3     Running            2          8m50s
mum-blue-6f544cd567-m6lws                  2/2     Running            3          76d
2 Answers

Use:

kubectl get pods | awk '{if($4>2)print$1}'

Use -n "NameSpace" if required to fetch pods on the basis of a namespace. For example:

kubectl get pods -n kube-system | awk '{if($4>2)print$1}'

where $1, $4 : Depends on which column pod name is present , column on which filter is to be done respectively

Note: awk will work in linux whereas

Actually is not possible to use field selector to get this result, as mentioned in this github open issue.

You can use kubectl with the option -o jsonpath to get the container name that whas restart more than 2 times. Example:

kubectl get pods -o jsonpath='{.items[*].status.containerStatuses[?(@.restartCount>=2)].name}'
Related