Soft vs. Hard Affinity

Difficulty: Advanced

Question

What is the difference between requiredDuringSchedulingIgnoredDuringExecution and preferredDuringSchedulingIgnoredDuringExecution in Kubernetes affinity rules?

Answer

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.

Reading the long names

The names split into two parts:

  • DuringScheduling: when the scheduler decides where to place a new Pod. required means filter, preferred means score.
  • DuringExecution: what happens after the Pod is running. 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.

How the scheduler applies them

The scheduler runs in two phases for each Pod:

  1. Filtering: nodes that fail any hard constraint (resources, taints, nodeSelector, required affinity) are removed.
  2. Scoring: the remaining nodes are scored by plugins. Each matching preferred term adds its 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.

Matching rules inside the spec

  • nodeSelectorTerms in a required rule are ORed: a node matching any term passes.
  • matchExpressions within one term are ANDed: all must be true.
  • Operators are In, NotIn, Exists, DoesNotExist, Gt and Lt. NotIn and DoesNotExist give you node anti-affinity.
  • Preferred rules are a list of {weight, preference}, so you can rank options, e.g. weight 80 for spot-free nodes and weight 20 for a particular zone.

When to use which

SituationChoice
GPU workload that cannot run without a GPURequired (gpu=nvidia-a100)
Data must stay in a compliant regionRequired (topology.kubernetes.io/region=ap-south-1)
Prefer cheaper or newer node pool but fine elsewherePreferred
Spread across zones as a nice-to-havePreferred anti-affinity, or topology spread constraints

Gotchas

  • Overusing required rules causes Pods stuck in 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.
  • Preferred is not a guarantee. If you truly need isolation (for example, PCI workloads at a payments company), use required rules plus taints and tolerations.
  • Required pod affinity and anti-affinity are expensive for the scheduler in large clusters; the docs warn against them at very large scale.
  • Changing a node's labels does not move running Pods. You must restart or reschedule them yourself.
Hard vs soft affinity. Required rules filter nodes; preferred rules only change node scores.
required...IgnoredDuringExecutionpreferred...IgnoredDuringExecution
Scheduler phaseFilteringScoring
No matching nodePod stays PendingPod scheduled on best other node
WeightsNone, all-or-nothing1 to 100 per term
Labels change laterRunning Pod staysRunning Pod stays

Code examples

Hard and soft node affinity together

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.

Key points

  • Required = filter phase, Pod stays Pending if nothing matches.
  • Preferred = scoring phase with weights 1 to 100, never blocks scheduling.
  • IgnoredDuringExecution means running Pods are not evicted when labels change.
  • nodeSelectorTerms are ORed; matchExpressions inside a term are ANDed.
  • The same required/preferred split applies to podAffinity and podAntiAffinity.

Common follow-up questions

  • How does node affinity compare with nodeSelector and with taints and tolerations?
  • What would RequiredDuringExecution mean and why is it hard to implement?
  • How would you debug a Pod stuck in Pending because of affinity?
  • When would you use topologySpreadConstraints instead of preferred anti-affinity?

Interview tips

  • Explain the name by splitting it into scheduling-time and execution-time behaviour; it makes the answer memorable.
  • Mention filtering vs scoring to show you know how the scheduler works internally.
  • Give a realistic example of each, such as GPU nodes (hard) and spot avoidance (soft).

Concepts covered

Node Affinity, requiredDuringSchedulingIgnoredDuringExecution, preferredDuringSchedulingIgnoredDuringExecution, Scheduler Scoring, matchExpressions

More Kubernetes & Orchestration questions