A field selector filters Kubernetes API objects by matching specified field values. It helps narrow kubectl results to objects with a particular property, such as Warning events or pods assigned to one node. section 4.3.1 section 7.4
Use field selectors with kubectl to find specific objects:
- List warning events with kubectl get ev --field-selector type=Warning. section 4.3.1
- List pods scheduled on a node with kubectl get pods --field-selector spec.nodeName=kind-worker. section 7.4.1
- Find pods that are not running with kubectl get pods --field-selector status.phase!=Running; add -A to search all namespaces. section 7.4.1
Supported fields depend on the object kind. metadata.name and metadata.namespace are always supported. Use kubectl explain to inspect object fields, then verify whether a chosen field is supported in selectors. section 7.4 section 7.4.1
$ kubectl get pods --field-selector spec.nodeName=kind-worker NAME READY STATUS RESTARTS AGE kiada-front-end 2/2 Running 0 15m kiada-002 2/2 Running 0 3h quote-002 2/2 Running 0 3h
Shows the command output for selecting pods assigned to a specific node. section 7.4.1
$ kubectl get pods --field-selector status.phase!=Running NAME READY STATUS RESTARTS AGE kiada-front-end-affinity 0/2 Pending 0 41m
Shows the command output for selecting pods whose phase is not Running. section 7.4.1
