Sub topic 15
Field Selectors Compared with Label Selectors
This chapter assumes you can read Pod manifests, run basic kubectl commands, and already know how equality-based label selectors work. Field selectors look similar on the command line, but they filter on a different kind of data.
What a field selector filters on
A label selector filters on labels, which you set and control. A field selector filters on the value of a field in the object's own definition or status. Kubernetes fills in many of those fields itself, so you often can't change them without changing the object.
Use the --field-selector flag with kubectl get:
kubectl get pods --field-selector status.phase=Running
kubectl get pods --field-selector spec.nodeName=worker-1
kubectl get pods --field-selector status.phase!=Running,metadata.namespace=default
kubectl get pods -A --field-selector metadata.namespace!=kube-systemField selectors support only =, ==, and !=. Separate several conditions with commas, and all of them must match. There is no in, notin, or existence test.
Which fields you can use
Every resource type supports metadata.name and metadata.namespace. Beyond those, each type supports its own short list. Pods support status.phase, spec.nodeName, spec.restartPolicy, spec.schedulerName, spec.serviceAccountName, and status.podIP, among others. If you use a field the type doesn't support, the API server returns an error such as field label not supported. It does not return an empty list.
Comparing the two
| Aspect | Label selector | Field selector |
|---|---|---|
| Flag | `-l` or `--selector` | `--field-selector` |
| Data source | Labels you attach | Fields in the object itself |
| Operators | `=`, `==`, `!=`, `in`, `notin`, exists | `=`, `==`, `!=` only |
| Available fields | Any key you choose | A fixed list per resource type |
| Use in manifests | Services, Deployments, and others | Not available |
| Main use | Grouping objects | Querying current state |
The last two rows matter most. Services and workloads can't use field selectors, so a Service can't select "all Pods on node worker-1." Field selectors are a query tool for clients such as kubectl and controllers that call the API.
Combining both
You can pass both flags in one command. An object must satisfy both filters to appear:
kubectl get pods -l app=web --field-selector status.phase=Running,spec.nodeName=worker-1This lists running web Pods on one node. It is a handy way to find which replicas of an application are on a node you plan to drain.
Common mistakes
- Expecting set-based syntax.
--field-selector 'status.phase in (Running,Pending)'fails. Run two queries or filter the output instead. - Using an unsupported field.
kubectl get pods --field-selector spec.containers[0].image=nginxfails because only a fixed set of fields is indexed. - Forgetting the namespace. Without
-nor-A, the query covers only the current namespace.
Exercise
You want to list all Pods labeled tier=frontend that are scheduled on the node worker-2. Which command does this, and why can't you use a field selector alone to find Pods by tier?
Show answer
Run kubectl get pods -l tier=frontend --field-selector spec.nodeName=worker-2. The tier value is a label, and field selectors only match supported fields of the object, so tier can only be matched with a label selector.
Exercise
Why does kubectl get pods --field-selector 'status.phase in (Running,Pending)' fail, and how would you get both kinds of Pods?
Show answer
Field selectors support only =, ==, and !=, not set-based operators. Run one query for each phase, or use --field-selector status.phase!=Succeeded,status.phase!=Failed to exclude the other phases.