Why kubectl returns pods with 'Terminating' status when I query for just running pods?

11/17/2020

enter image description here

I have a k3s cluster with some pods running. I want to get all pods with status of Running containing grafana in its name.

From the docs, looks like I can use field selector flag to achieve it (was introduced in v1.9 release). But when I give it a try, it didn't work.

I know I can force delete the pod with Terminating status to get what I want. What I want to know here is, why my command didn't work as expected? Did i miss something?

Btw, the Terminating pod was there for some time. I believe it's stuck. But again, that's not the interest of this question.

-- Zulhilmi Zainudin
k3s
kubectl
kubernetes

1 Answer

11/17/2020

If you take a look into k8s documentation on pod-lifecycle you can see that status.phase can have only 5 different values and none of them is Terminating. This means that termination state in not reflected in this field and therefore filtering by the phase field is useless.

Terminating status is reflected under .status.containerStatuses.state (container states in docs) although this field label doesn't seem to be supported by the filter.


So what can you do?

The easiest thing you can do is to use grep twice:

kubectl get pods | grep grafana | grep Running
-- Matt
Source: StackOverflow