Node Affinity, Pod Affinity & Pod Anti-Affinity
Express soft/hard placement rules for co-location and spreading across nodes and zones.
In plain words
Affinity is a seating plan: node rules choose the venue, pod affinity groups collaborators, and anti-affinity separates rivals. The simple Kubernetes rule is: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
Think of it like…
Affinity is a seating plan: node rules choose the venue, pod affinity groups collaborators, and anti-affinity separates rivals.
Where you’ve seen it
A cluster operator validates a small change and runs kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labels. Expected evidence: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods. If the run instead shows this symptom, stop and restore the last known state: Hard affinity and topology-key skew can leave new replicas Pending after a zone or pool disappears, while preferred rules do not guarantee locality.
Words you’ll hear
- Node Affinity
- For this lesson, Node Affinity is the controlling idea: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
- Pod Affinity
- Pod Affinity is the checkpoint; run
kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labelsand inspect the owner chain. Hard affinity and topology-key skew can leave new replicas Pending after a zone or pool disappears, while preferred rules do not guarantee locality. - Anti-Affinity
- Anti-Affinity is the outcome to preserve; the cost is explicit: Placement precision improves cache or failure-domain locality, but large affinity matches consume scheduler work and can fragment scarce capacity.
- Verification evidence
- The evidence command is
kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labels. It connects the declaration to this behavior: Express soft/hard placement rules for co-location and spreading across nodes and zones.
Visual diagram
Theory & deep dive
How Node Affinity reaches the intended state
Express soft/hard placement rules for co-location and spreading across nodes and zones. Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
The concrete checkpoint is kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labels; compare the Node Affinity owner, Pod Affinity state, and Anti-Affinity outcome instead of treating YAML acceptance as proof.
Run kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labels after the change and correlate the result with Events and the owning object. Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
Pod Affinity: failure boundary and production cost
Hard affinity and topology-key skew can leave new replicas Pending after a zone or pool disappears, while preferred rules do not guarantee locality. The prerequisite contract is concrete: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
Placement precision improves cache or failure-domain locality, but large affinity matches consume scheduler work and can fragment scarce capacity. A safe design limits blast radius, records the last good configuration, and uses kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labels as a deterministic rollback checkpoint.
Capture the current state with kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labels, then correct the controlling manifest rather than patching a child object; the relevant divergence signal is specific: Hard affinity and topology-key skew can leave new replicas Pending after a zone or pool disappears, while preferred rules do not guarantee locality.
How it works, step by step
Step 1 of 41. Model the cluster contract
Start from a minimal API, context, or node condition and make this contract explicit: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
- Goal
- Express soft/hard placement rules for co-location and spreading across nodes and zones.
- Owner
- Node Affinity
When to use it & trade-offs
What it is
Express soft/hard placement rules for co-location and spreading across nodes and zones. Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
Why it matters: Hard affinity and topology-key skew can leave new replicas Pending after a zone or pool disappears, while preferred rules do not guarantee locality. The invariant at risk is specific: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods. The quickest fact check before changing the design is kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labels.
When to reach for it
- Use Node Affinity to enforce this invariant: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
- Use Pod Affinity evidence from
kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labelsin reviews and incident timelines. Express soft/hard placement rules for co-location and spreading across nodes and zones. - Choose the platform-level abstraction route only when this cost is justified by governance or scale: Placement precision improves cache or failure-domain locality, but large affinity matches consume scheduler work and can fragment scarce capacity.
When to avoid or be careful
- Avoid assuming Node Affinity is complete after YAML submission; the required behavior is concrete: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
- Avoid treating the next symptom as random; check the owner and state with
kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labelsfirst: Hard affinity and topology-key skew can leave new replicas Pending after a zone or pool disappears, while preferred rules do not guarantee locality. - Avoid adopting a heavier platform wrapper before measuring its cost: Placement precision improves cache or failure-domain locality, but large affinity matches consume scheduler work and can fragment scarce capacity.
Native, manual, and managed paths for Node Affinity
kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labels.Strengths: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
Watch out: Placement precision improves cache or failure-domain locality, but large affinity matches consume scheduler work and can fragment scarce capacity. Recovery must also recognize this signal: Hard affinity and topology-key skew can leave new replicas Pending after a zone or pool disappears, while preferred rules do not guarantee locality.
kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labels; the expected invariant is: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.Strengths: kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labels gives immediate feedback without first building a platform layer.
Watch out: A manual change can hide drift, and unlike Node Affinity, it does not make this symptom self-documenting: Hard affinity and topology-key skew can leave new replicas Pending after a zone or pool disappears, while preferred rules do not guarantee locality.
kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labels.Strengths: Platform controls can wrap this exact behavior in policy or dashboards: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
Watch out: The added platform ownership buys value at this cost: Placement precision improves cache or failure-domain locality, but large affinity matches consume scheduler work and can fragment scarce capacity.
Interview signals & numbers to know
When this topic comes up
- Interview anchor: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
- Trace Node Affinity to Anti-Affinity through Pod Affinity; the invariant is: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
- Recognize the symptom: Hard affinity and topology-key skew can leave new replicas Pending after a zone or pool disappears, while preferred rules do not guarantee locality.
- Justify the cost: Placement precision improves cache or failure-domain locality, but large affinity matches consume scheduler work and can fragment scarce capacity.
- Verify with kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labels instead of only inspecting YAML.
Benchmarks & rules of thumb
- 2
- rule strengths: required and preferred
- 1-100
- valid weight range for preferred node-affinity terms
- 1
- topologyKey defines the placement domain for pod-affinity rules
Common mistakes & follow-ups
Common interview mistakes
- Debugging only the child object misses the Node Affinity owner contract: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods.
- Ignoring this telltale condition leads to unrelated changes: Hard affinity and topology-key skew can leave new replicas Pending after a zone or pool disappears, while preferred rules do not guarantee locality.
- Repeating
kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labelswithout an owner for remediation wastes retry cost. Placement precision improves cache or failure-domain locality, but large affinity matches consume scheduler work and can fragment scarce capacity.
Likely interviewer follow-ups
- How would API output and runtime evidence prove this behavior: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods?
- Which owner object, logs, and Events would you inspect for this symptom-Hard affinity and topology-key skew can leave new replicas Pending after a zone or pool disappears, while preferred rules do not guarantee locality-?
- At what threshold would this cost become unacceptable: Placement precision improves cache or failure-domain locality, but large affinity matches consume scheduler work and can fragment scarce capacity?
kubectl get pod api-0 -o jsonpath='{.spec.affinity}'; kubectl get nodes --show-labels, name the failure, then justify the cost. Invariant: Node affinity matches node labels with required or preferred rules, pod affinity co-locates related Pods, and anti-affinity spreads competing Pods. Failure: Hard affinity and topology-key skew can leave new replicas Pending after a zone or pool disappears, while preferred rules do not guarantee locality. Cost: Placement precision improves cache or failure-domain locality, but large affinity matches consume scheduler work and can fragment scarce capacity.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Node Affinity, Pod Affinity & Pod Anti-Affinity with generative AI interview Q&A and hands-on practice.