You want to schedule pods so that each pod runs on a different node but all pods remain within the same availability zone. Which affinity and topologyKey configuration achieves this?
hard📝 Workflow Q8 of 15
Kubernetes - Scheduling
You want to schedule pods so that each pod runs on a different node but all pods remain within the same availability zone. Which affinity and topologyKey configuration achieves this?
AUse <code>podAntiAffinity</code> with <code>preferredDuringSchedulingIgnoredDuringExecution</code> and <code>topologyKey: failure-domain.beta.kubernetes.io/region</code>.
BUse <code>podAffinity</code> with <code>requiredDuringSchedulingIgnoredDuringExecution</code> and <code>topologyKey: failure-domain.beta.kubernetes.io/zone</code>.
CUse <code>podAntiAffinity</code> with <code>requiredDuringSchedulingIgnoredDuringExecution</code> and <code>topologyKey: kubernetes.io/hostname</code>, combined with <code>nodeAffinity</code> restricting nodes to the desired zone label.
DUse <code>podAffinity</code> with <code>preferredDuringSchedulingIgnoredDuringExecution</code> and <code>topologyKey: kubernetes.io/hostname</code>.
Step-by-Step Solution
Solution:
Step 1: Ensure pods run on different nodes
Using podAntiAffinity with requiredDuringSchedulingIgnoredDuringExecution and topologyKey: kubernetes.io/hostname prevents pods from scheduling on the same node.
Step 2: Restrict pods to the same zone
Use nodeAffinity to limit scheduling to nodes labeled with the desired availability zone.
Step 3: Combine both rules
This combination ensures pods are spread across nodes but remain within the same zone.
Final Answer:
Use podAntiAffinity with requiredDuringSchedulingIgnoredDuringExecution and topologyKey: kubernetes.io/hostname, combined with nodeAffinity restricting nodes to the desired zone label. correctly achieves the requirement.
Quick Check:
Anti-affinity by hostname + nodeAffinity by zone [OK]
Quick Trick:Combine podAntiAffinity by hostname with nodeAffinity by zone [OK]
Common Mistakes:
Using podAffinity instead of podAntiAffinity to spread pods
Using preferred instead of required for strict spreading
Using region instead of zone for topologyKey
Master "Scheduling" in Kubernetes
9 interactive learning modes - each teaches the same concept differently