What is node affinity and pod affinity/anti-affinity?
Learn how Kubernetes node affinity, pod affinity, and anti-affinity control scheduling to place, co-locate, and spread pods for high availability.
Expected Interview Answer
Node affinity constrains which nodes a pod can be scheduled onto based on node labels, while pod affinity/anti-affinity constrains scheduling based on the labels of pods already running on a node or topology domain.
Node affinity is the expressive successor to nodeSelector and supports required (requiredDuringSchedulingIgnoredDuringExecution) and preferred (preferredDuringSchedulingIgnoredDuringExecution) rules. Pod affinity co-locates pods that should run together (e.g., app and cache) within a topology key like a hostname or zone, while pod anti-affinity spreads pods apart for high availability. Both are evaluated by the scheduler at placement time and use label selectors plus a topologyKey.
- Places workloads on nodes with the right hardware (GPU, SSD, zone)
- Co-locates tightly coupled pods to cut network latency
- Spreads replicas across nodes or zones for high availability
- More expressive than a plain nodeSelector
- Supports both hard requirements and soft preferences
AI Mentor Explanation
Node affinity is like a captain insisting a fast bowler only plays on green seaming pitches — the pitch (node) must have the right label. Pod affinity is pairing a wicketkeeper right behind a specific bowler so they operate as a unit, and anti-affinity is spreading your fielders across the boundary so two aren't guarding the same gap, keeping the whole field covered.
Step-by-Step Explanation
Step 1
Label the nodes
Attach labels to nodes (e.g., disktype=ssd, topology.kubernetes.io/zone=us-east-1a) that describe hardware or location.
Step 2
Add nodeAffinity
Under spec.affinity.nodeAffinity, use required or preferred rules with matchExpressions against those node labels.
Step 3
Add podAffinity to co-locate
Use spec.affinity.podAffinity with a labelSelector matching target pods and a topologyKey like kubernetes.io/hostname.
Step 4
Add podAntiAffinity to spread
Use podAntiAffinity with the same pod's own labels to keep replicas on separate nodes or zones.
Step 5
Choose required vs preferred
Pick requiredDuringScheduling for hard constraints or preferredDuringScheduling with a weight for soft ones.
What Interviewer Expects
- Difference between node affinity and pod affinity
- Meaning of required vs preferred rules
- Role of topologyKey in pod affinity
- When to use anti-affinity for high availability
- How affinity differs from nodeSelector and taints/tolerations
Common Mistakes
- Confusing pod anti-affinity with taints and tolerations
- Forgetting the topologyKey, which is mandatory for pod affinity
- Assuming affinity rules are re-evaluated after scheduling (they are IgnoredDuringExecution)
- Using required rules so strict that pods become unschedulable
- Mixing up node labels with pod labels in the selectors
Best Answer (HR Friendly)
“Affinity rules tell Kubernetes where to place application copies. Node affinity picks the right kind of machine, while pod affinity keeps related apps together and anti-affinity keeps copies apart so a single machine failure doesn't take everything down.”
Code Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchLabels:
app: web
topologyKey: kubernetes.io/hostname
containers:
- name: web
image: nginx:1.27Follow-up Questions
- How does topologyKey change the granularity of pod anti-affinity?
- What is the difference between affinity and pod topology spread constraints?
- Why are affinity rules IgnoredDuringExecution and what does that imply?
- How do taints and tolerations complement affinity?
- When would you prefer nodeSelector over node affinity?
MCQ Practice
1. Which field is mandatory in a pod affinity/anti-affinity term?
topologyKey defines the domain (host, zone) over which the affinity or anti-affinity is evaluated and is required.
2. What does requiredDuringSchedulingIgnoredDuringExecution mean?
The rule must be satisfied to schedule the pod, but changing node labels later will not evict a running pod.
3. To spread replicas of one Deployment across nodes, you use:
Pod anti-affinity matching the pod's own label keeps replicas on separate nodes for high availability.
Flash Cards
Node affinity vs nodeSelector — Node affinity is the expressive successor to nodeSelector, supporting operators, required/preferred rules, and weights.
Pod affinity — Co-locates pods with matching labels within a topologyKey domain (e.g., same host or zone).
Pod anti-affinity — Keeps pods apart across a topology domain, commonly to spread replicas for HA.
topologyKey — The node label defining the domain over which pod (anti-)affinity applies, such as kubernetes.io/hostname.
IgnoredDuringExecution — Affinity is checked only at scheduling time; later label changes do not evict running pods.