Sub topic 5
Hard requirements with requiredDuringSchedulingIgnoredDuringExecution
This chapter assumes you can write and apply pod manifests, know what node labels are, and have used nodeSelector. It covers the required form of node affinity, which sets a hard rule for where a pod may run.
What the field name says
The field name is long, but it has two halves:
requiredDuringSchedulingmeans the rule must be satisfied when the scheduler places the pod. If no node matches, the pod stays Pending.IgnoredDuringExecutionmeans the rule is not checked again after the pod is running. Changing a node's labels later does not evict the pod. Chapter 9 covers this in more detail.
The field structure
The rule lives under spec.affinity.nodeAffinity. The nesting is deep, and most mistakes come from getting it wrong:
requiredDuringSchedulingIgnoredDuringExecutionholds one object.- That object has a list called
nodeSelectorTerms. - Each term has a list called
matchExpressions. - Each expression has a
key, anoperator, and usually a list ofvalues.
A worked example
This pod may run only on nodes whose zone label is zone-a or zone-b.
apiVersion: v1
kind: Pod
metadata:
name: zone-restricted
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: topology.kubernetes.io/zone
operator: In
values:
- zone-a
- zone-b
containers:
- name: nginx
image: nginx:1.27The In operator passes when the node's label value equals any entry in values. A node in zone-c, or a node with no zone label, is filtered out.
Apply it and check where it landed:
kubectl apply -f zone-restricted.yaml
kubectl get pod zone-restricted -o wide
kubectl get nodes -L topology.kubernetes.io/zoneWhy use it instead of nodeSelector
nodeSelector can only say "label equals value" and every pair must match. A required affinity rule can express "one of several values" with In, exclusion with NotIn, and presence checks with Exists and DoesNotExist. The next chapter covers the operators.
If you only need a single exact label match, nodeSelector is shorter and equally strict.
Several expressions in one term
All expressions inside a single term must hold at the same time. The following rule needs an integer label greater than 1 and no maintenance label.
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: example.com/gpu-count
operator: Gt
values:
- "1"
- key: example.com/maintenance
operator: DoesNotExistNote that the value for Gt is a quoted string, even though it represents a number.
Common errors
| Symptom | Likely cause |
|---|---|
| Pod stays Pending with a "didn't match Pod's node affinity/selector" event | No node carries the required label, or the value is misspelled |
| The API server rejects the manifest | A wrong field name or wrong nesting, such as `matchExpressions` placed directly under the required field |
| Rule matches nothing although the label looks right | Label key or value differs by case or by a stray character |
| Pod keeps running after you remove the label | Expected behavior, since the rule is ignored during execution |
Exercise
A pod uses a required node affinity rule with the In operator on the key disktype and the values ssd and nvme. No node has a disktype label. What happens to the pod, and what happens if you add disktype=ssd to a node while the pod is Pending?
Show answer
The pod stays Pending because the scheduler finds no node that satisfies the rule. Once a node is labeled disktype=ssd, that node passes the filter and the scheduler can place the pod there on a later scheduling attempt.
Exercise
A pod is running on a node labeled disktype=ssd under a required rule for that label. You remove the label from the node. Is the pod evicted, and why?
Show answer
No. The rule ends in IgnoredDuringExecution, so it is evaluated only at scheduling time. The running pod stays where it is.