How can I view pods with kubectl and filter based on having a status of ImagePullBackOff?

7/26/2019

I'd like to do a kubectl get pods and filter where the pod is in a status of ImagePullBackOff.

I've tried kubectl get pods --field-selector=status.phase=waiting and kubectl get pods --field-selector=status.phase=ImagePullBackOff but that returns no results.

I've had a look at the JSON output with -o json:

...
            {
                "image": "zzzzzzzzzzzzzzzz",
                "imageID": "",
                "lastState": {},
                "name": "nginx",
                "ready": false,
                "restartCount": 0,
                "state": {
                    "waiting": {
                        "message": "Back-off pulling image \"zzzzzzzzzzzzzzzz\"",
                        "reason": "ImagePullBackOff"
                    }
                }
            }
...

If I try target that value:

kubectl get pods --field-selector=state.waiting=ImagePullBackOff 
Error from server (BadRequest): Unable to find "/v1, Resource=pods" that match label selector "", field selector "state.waiting=ImagePullBackOff": field label not supported: state.waiting
-- Chris Stryczynski
kubectl
kubernetes

3 Answers

7/26/2019

You can use command below:

kubectl get pods --all-namespaces -o custom-columns=NAMESPACE:metadata.namespace,POD:metadata.name,PodIP:status.podIP,STATE:status.containerStatuses[*].state.waiting.reason | grep ImagePullBackOff

BTW: your command kubectl get pods --field-selector=state.waiting=ImagePullBackOff fails because there is no state.waiting selector in kubernetes. Thats why you see field label not supported: state.waiting error.

As per official documentation and Field Selectors docs:

A Pod’s status field is a PodStatus object, which has a phase field.

Here are the possible values for phase:

  • Pending
  • Running
  • Succeeded
  • Failed
  • Unknown

So use custom-columns output.

-- VKR
Source: StackOverflow

7/26/2019

Using json output and piping through jq:

kubectl get pod -o=json | jq '.items[]|select(any( .status.containerStatuses[]; .state.waiting.reason=="ImagePullBackOff"))|.metadata.name'

Last chunk |.metadata.name means it'll list pod names instead of the entire structures.

-- Egor Stambakio
Source: StackOverflow

7/26/2019

As you can see in the offical doc for kubernetes,

Supported field selectors vary by Kubernetes resource type. All resource types support the metadata.name and metadata.namespace fields. Using unsupported field selectors produces an error.

Ref: https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/#supported-fields

You can try this:

kubectl get pod --all-namespaces | grep "ImagePullBackOff" | awk '{print $2 " -n " $1}' | xargs kubectl get pod -o json

Or:

kubectl get pod -o jsonpath='{.items[?(@.status.containerStatuses[*].state.waiting.reason=="ImagePullBackOff")].metadata.name}'
-- Shudipta Sharma
Source: StackOverflow