Chris Stryczynski
Chris Stryczynski

Reputation: 34091

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

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

Upvotes: 18

Views: 31749

Answers (3)

Egor Stambakio
Egor Stambakio

Reputation: 18156

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.

Upvotes: 19

Shudipta Sharma
Shudipta Sharma

Reputation: 5892

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}'

Upvotes: 4

Vit
Vit

Reputation: 8491

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.

Upvotes: 11

Related Questions