Sub topic 4
Why Node Affinity Exists and What It Adds
This chapter assumes you can write and apply pod manifests, know what a node label is, and have used nodeSelector at least once. If any of that is new, review the previous chapters on node labels and nodeSelector first.
The limit of nodeSelector
nodeSelector is a map of label keys and values. A pod is eligible for a node only if the node carries every pair in the map. That is simple and predictable, but it can express only one thing: "this label equals this value", joined by AND.
Real placement rules are often looser or softer than that:
- "Run in zone-a or zone-b." A map can hold only one value per key.
- "Run on any node that is not in maintenance." A map cannot say "not" or "absent".
- "Prefer SSD nodes, but run elsewhere if none fit." A map is always a hard requirement.
Node affinity was added to cover these cases. It is a richer way to express the same idea, which is to attract a pod to nodes based on node labels.
What node affinity adds
Node affinity lives under spec.affinity.nodeAffinity. Compared with nodeSelector, it adds three things.
- Operators. Each rule is a match expression with a key, an operator, and optional values. The operators are
In,NotIn,Exists,DoesNotExist,Gt, andLt. A later chapter covers choosing among them. - Hard and soft rules. A required rule filters nodes out. A preferred rule only adds to a node's score, so the pod can still land elsewhere.
- Weights. Each preferred rule carries a weight from 1 to 100, so you can rank several preferences.
The two rule types have long names that encode their behavior:
| Field | Behavior at scheduling time | Behavior once the pod runs |
|---|---|---|
| `requiredDuringSchedulingIgnoredDuringExecution` | Hard filter. The pod stays Pending if no node matches. | Ignored. |
| `preferredDuringSchedulingIgnoredDuringExecution` | Soft preference. Matching nodes score higher. | Ignored. |
"IgnoredDuringExecution" means that if a node's labels change after the pod starts, Kubernetes does not evict the pod.
Same rule, two forms
This nodeSelector pod runs only on nodes labeled disktype=ssd:
apiVersion: v1
kind: Pod
metadata:
name: nginx-ssd
spec:
nodeSelector:
disktype: ssd
containers:
- name: nginx
image: nginx:1.27The equivalent node affinity form is more verbose but means the same thing:
apiVersion: v1
kind: Pod
metadata:
name: nginx-ssd-affinity
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: nginx
image: nginx:1.27The verbosity pays off once the rule is something nodeSelector cannot say. The next example allows two zones, which a selector map cannot do:
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.27Common mistakes
- Treating
nodeSelectoras obsolete. It is still supported and is the clearest choice for a single equality match. Use affinity when you need operators, alternatives, or preferences. - Expecting eviction. Changing a node's label after scheduling does not move a running pod.
- Expecting a preference to be a guarantee. A preferred rule never leaves a pod Pending. Use a required rule for hard needs.
Exercise
A pod must run in zone-a or zone-b, and should prefer nodes labeled disktype=ssd but still run on other nodes if none are available. Which fields express each part, and why can nodeSelector not do it?
Show answer
Use a required rule (requiredDuringSchedulingIgnoredDuringExecution) with the In operator and values zone-a and zone-b for the zone restriction. Use a preferred rule (preferredDuringSchedulingIgnoredDuringExecution) with a weight and disktype In [ssd] for the SSD preference. nodeSelector cannot do it because it holds one value per key, so it cannot list two zones, and every entry in it is a hard requirement, so it cannot express a preference.