Sub topic 10
Selecting nodes by field with matchFields
This chapter assumes you can write required node affinity rules and know that a nodeSelectorTerms list holds terms made of expressions. Here you will see the second way to write a term: matchFields.
Labels versus fields
matchExpressions tests node labels. matchFields tests fields of the Node object itself. The only field you can rely on is metadata.name, the node's name. Use it when you want to target a node by name and don't want to add a label first.
A term can hold both lists:
nodeSelectorTerms:
- matchExpressions: [] # tests labels
matchFields: [] # tests fieldsPinning a pod to a named node
This pod can run only on the node named worker-1.
apiVersion: v1
kind: Pod
metadata:
name: pinned-pod
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchFields:
- key: metadata.name
operator: In
values:
- worker-1
containers:
- name: nginx
image: nginx:1.27The scheduler still runs its normal filters. If worker-1 lacks free CPU or memory, or has a taint the pod doesn't tolerate, the pod stays Pending. It does not move to another node.
Check where the pod landed:
kubectl get pod pinned-pod -o wideLimits of matchFields
- The key must be
metadata.name. Other fields are rejected or never match. - The operator must be
InorNotIn.Exists,GtandLtare for label expressions. - Each expression takes a single value. The API server rejects a longer
valueslist.
Matching several nodes
Because each expression takes one value, you list several nodes as separate terms. Terms are ORed, so the pod may run on a node that satisfies any one of them.
nodeSelectorTerms:
- matchFields:
- key: metadata.name
operator: In
values:
- worker-1
- matchFields:
- key: metadata.name
operator: In
values:
- worker-2To exclude a node, use NotIn:
nodeSelectorTerms:
- matchFields:
- key: metadata.name
operator: NotIn
values:
- worker-3Combining fields and labels
Inside one term, matchExpressions and matchFields are ANDed. The pod below needs a node named worker-1 that also carries disktype=ssd.
nodeSelectorTerms:
- matchFields:
- key: metadata.name
operator: In
values:
- worker-1
matchExpressions:
- key: disktype
operator: In
values:
- ssdA common mistake is to put the field test and the label test in separate terms. That makes them alternatives, so the pod can match either a node named worker-1 or any SSD node.
When to use it
Pinning pods to one named node is rarely right for ordinary workloads. It removes the scheduler's freedom, and it breaks if the node is replaced under a new name. A label such as disktype=ssd survives node replacement. The kubernetes.io/hostname label looks similar, but it can differ from the node name on some platforms, so metadata.name is the exact match.
The main user of matchFields is Kubernetes itself. The DaemonSet controller adds a metadata.name term to each pod it creates, so each pod targets one specific node.
Exercise
A pod has two entries in nodeSelectorTerms. The first uses matchFields to require metadata.name In worker-1. The second uses matchExpressions to require disktype In ssd. On which nodes can the pod run?
Show answer
On worker-1, or on any node labeled disktype=ssd. Terms are ORed, so a node needs to satisfy only one. To require both conditions, put the matchFields and matchExpressions entries in the same term.