Field selectors let you select Kubernetes resources based on the value of one or more resource fields. Here are some example field selector queries:
kubectl command selects all Pods for which the value of the
status.phase field is
$ kubectl get pods --field-selector status.phase=Running
Field selectors are essentially resource filters. By default, no selectors/filters are applied, meaning that all resources of the specified type are selected. This makes the following
$ kubectl get pods $ kubectl get pods --field-selector ""
Supported field selectors vary by Kubernetes resource type. All resource types support the
metadata.namespace fields. Using unsupported field selectors produces an error. For example:
$ kubectl get ingress --field-selector foo.bar=baz Error from server (BadRequest): Unable to find "ingresses" that match label selector "", field selector "foo.bar=baz": "foo.bar" is not a known field selector: only "metadata.name", "metadata.namespace"
You can use the
!= operators with field selectors (
== mean the same thing). This
kubectl command, for example, selects all Kubernetes Services that aren’t in the
$ kubectl get services --field-selector metadata.namespace!=default
As with label and other selectors, field selectors can be chained together as a comma-separated list. This
kubectl command selects all Pods for which the
status.phase does not equal
Running and the
spec.restartPolicy field equals
$ kubectl get pods --field-selector=status.phase!=Running,spec.restartPolicy=Always
You use field selectors across multiple resource types. This
kubectl command selects all Statefulsets and Services that are not in the
$ kubectl get statefulsets,services --field-selector metadata.namespace!=default
Was this page helpful?
Thanks for the feedback. If you have a specific, answerable question about how to use Kubernetes, ask it on Stack Overflow. Open an issue in the GitHub repo if you want to report a problem or suggest an improvement.