2.05 Node Affinity


- Node Affinity provides advanced control over pod placement compared to NodeSelector.
- Defined under
spec > affinity > nodeAffinity. - Uses matchExpressions with
key,operator, andvalues. - Example:
In,NotIn,Exists. - Example rules:
- Place pod on
LargeorMediumnodes. - Avoid scheduling on
Smallnodes. - Match nodes where label
sizeexists.
Node Affinity Types

- Two stages in pod lifecycle:
- DuringScheduling β pod creation.
-
DuringExecution β pod already running.
-
Available Types:
requiredDuringSchedulingIgnoredDuringExecutionβ Pod scheduled only if matching node exists.-
preferredDuringSchedulingIgnoredDuringExecutionβ Tries matching node, falls back to any node. -
Planned Type:
requiredDuringSchedulingRequiredDuringExecutionβ Will enforce rules even after pod is running.
Behavior Summary
- Required: Pod wonβt be scheduled without a matching node.
- Preferred: Scheduler tries to match, but runs pod elsewhere if needed.
- Ignored During Execution: Running pods are not affected if labels change later.
