Difficulty: Advanced
What is the difference between requiredDuringSchedulingIgnoredDuringExecution and preferredDuringSchedulingIgnoredDuringExecution in Kubernetes affinity rules?
requiredDuringSchedulingIgnoredDuringExecution is a hard rule: the scheduler will only place the Pod on a node that satisfies it, and if no node matches, the Pod stays Pending. preferredDuringSchedulingIgnoredDuringExecution is a soft rule: the scheduler adds a weighted score to matching nodes and prefers them, but still schedules the Pod elsewhere if none match. In both cases IgnoredDuringExecution means a Pod that is already running is not evicted if node labels change later.
The names split into two parts:
required means filter, preferred means score.Ignored means Kubernetes does not re-check the rule. A RequiredDuringExecution variant has been discussed for years but is not implemented.The same two variants exist for nodeAffinity, podAffinity and podAntiAffinity.
The scheduler runs in two phases for each Pod:
nodeSelector, required affinity) are removed.weight (1 to 100) to the node's affinity score, which is combined with other scores such as resource balance.So required terms decide where a Pod can go, and preferred terms influence where it most likely will go.
nodeSelectorTerms in a required rule are ORed: a node matching any term passes.matchExpressions within one term are ANDed: all must be true.In, NotIn, Exists, DoesNotExist, Gt and Lt. NotIn and DoesNotExist give you node anti-affinity.{weight, preference}, so you can rank options, e.g. weight 80 for spot-free nodes and weight 20 for a particular zone.| Situation | Choice |
|---|---|
| GPU workload that cannot run without a GPU | Required (gpu=nvidia-a100) |
| Data must stay in a compliant region | Required (topology.kubernetes.io/region=ap-south-1) |
| Prefer cheaper or newer node pool but fine elsewhere | Preferred |
| Spread across zones as a nice-to-have | Preferred anti-affinity, or topology spread constraints |
Pending with events like 0/6 nodes are available: 6 node(s) didn't match Pod's node affinity/selector. During node pool migrations this can block deploys.| required...IgnoredDuringExecution | preferred...IgnoredDuringExecution | |
|---|---|---|
| Scheduler phase | Filtering | Scoring |
| No matching node | Pod stays Pending | Pod scheduled on best other node |
| Weights | None, all-or-nothing | 1 to 100 per term |
| Labels change later | Running Pod stays | Running Pod stays |
apiVersion: apps/v1
kind: Deployment
metadata:
name: ml-inference
spec:
replicas: 3
selector:
matchLabels: { app: ml-inference }
template:
metadata:
labels: { app: ml-inference }
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: accelerator # hard: must have a GPU
operator: In
values: ["nvidia-l4", "nvidia-a100"]
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 80 # soft: prefer on-demand nodes
preference:
matchExpressions:
- key: node-lifecycle
operator: NotIn
values: ["spot"]
containers:
- name: server
image: registry.example.com/inference:2.1.0
The Pod can only land on GPU nodes, and among those the scheduler prefers non-spot nodes. If only spot GPU nodes have room, it still schedules there.
Node Affinity, requiredDuringSchedulingIgnoredDuringExecution, preferredDuringSchedulingIgnoredDuringExecution, Scheduler Scoring, matchExpressions