Interview's MateKubernetes
Interview's Mate

Kubernetes

K8s · Workloads · GitOps · Cloud

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Kubernetes topics
Lesson 18/ 160
01. Kubernetes Fundamentals & Architecture

Node Affinity, Pod Affinity & Pod Anti-Affinity

Express soft/hard placement rules for co-location and spreading across nodes and zones.

AdvancedNode Affinity · Pod Affinity · Anti-Affinity3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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-labels and 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

Loading interactive 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.

Verify Pod Affinity, not only object creation

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.

Key takeaway: Node Affinity is implemented only when this invariant remains observable: 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.

Recover from Pod Affinity drift

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.

Key takeaway: Accept the trade-off consciously: Placement precision improves cache or failure-domain locality, but large affinity matches consume scheduler work and can fragment scarce capacity.

How it works, step by step

Step 1 of 4
Operator intent

1. 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.

Node Affinity contract → owned state
Key numbers at this step
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-labels in 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-labels first: 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

Native cluster primitive
Best for: Express soft/hard placement rules for co-location and spreading across nodes and zones. A concrete incident check is 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.

One-off administrative action
Best for: Quickly testing the Pod Affinity assumption with 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.

Platform-level abstraction
Best for: Teams that need Anti-Affinity integrated with guardrails; the evidence path is 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-labels without 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?
Interview tip: Interview sequence: state the invariant, run 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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Node Architecture: Taints, Tolerations & nodeSelector
Next topic
Topology Spread Constraints for High Availability