Bird
0
0

Which affinity configuration achieves this?

hard📝 Best Practice Q15 of 15
Kubernetes - Scheduling
You want to deploy multiple replicas of a pod with label app: cache so that no two pods run on the same node, but you want the scheduler to try to keep pods in the same zone if possible. Which affinity configuration achieves this?
AUse <code>podAffinity</code> with <code>requiredDuringSchedulingIgnoredDuringExecution</code> on <code>app: cache</code> and <code>topologyKey: kubernetes.io/hostname</code> only
BUse <code>podAntiAffinity</code> with <code>requiredDuringSchedulingIgnoredDuringExecution</code> on <code>app: cache</code> and <code>topologyKey: kubernetes.io/hostname</code>, plus <code>podAffinity</code> with <code>preferredDuringSchedulingIgnoredDuringExecution</code> on <code>topologyKey: failure-domain.beta.kubernetes.io/zone</code>
CUse <code>podAntiAffinity</code> with <code>preferredDuringSchedulingIgnoredDuringExecution</code> on <code>app: cache</code> and <code>topologyKey: kubernetes.io/hostname</code> only
DUse <code>podAffinity</code> with <code>preferredDuringSchedulingIgnoredDuringExecution</code> on <code>app: cache</code> and <code>topologyKey: failure-domain.beta.kubernetes.io/zone</code> only
Step-by-Step Solution
Solution:
  1. Step 1: Prevent pods on same node strictly

    Use podAntiAffinity with requiredDuringSchedulingIgnoredDuringExecution and topologyKey: kubernetes.io/hostname to ensure no two pods run on the same node.
  2. Step 2: Prefer pods in same zone

    Add podAffinity with preferredDuringSchedulingIgnoredDuringExecution and topologyKey: failure-domain.beta.kubernetes.io/zone to encourage scheduling pods in the same zone but not strictly.
  3. Step 3: Combine both rules

    Use podAntiAffinity with requiredDuringSchedulingIgnoredDuringExecution on app: cache and topologyKey: kubernetes.io/hostname, plus podAffinity with preferredDuringSchedulingIgnoredDuringExecution on topologyKey: failure-domain.beta.kubernetes.io/zone combines strict anti-affinity on node and preferred affinity on zone, matching the requirement.
  4. Final Answer:

    Use required podAntiAffinity on hostname and preferred podAffinity on zone -> Option B
  5. Quick Check:

    Strict anti-affinity + preferred affinity = Use podAntiAffinity with requiredDuringSchedulingIgnoredDuringExecution on app: cache and topologyKey: kubernetes.io/hostname, plus podAffinity with preferredDuringSchedulingIgnoredDuringExecution on topologyKey: failure-domain.beta.kubernetes.io/zone [OK]
Quick Trick: Use required anti-affinity on node + preferred affinity on zone [OK]
Common Mistakes:
  • Using affinity instead of anti-affinity to separate pods
  • Using preferred instead of required for strict separation
  • Ignoring zone preference

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kubernetes Quizzes