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.waitingYou 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 ImagePullBackOffBTW: 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:
So use custom-columns output.
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.
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 jsonOr:
kubectl get pod -o jsonpath='{.items[?(@.status.containerStatuses[*].state.waiting.reason=="ImagePullBackOff")].metadata.name}'