Pod affinity lets you tell Kubernetes to schedule pods close to other pods based on labels. It helps pods run together on the same or nearby nodes.
Click to reveal answer
beginner
What does pod anti-affinity do?
Pod anti-affinity tells Kubernetes to avoid placing pods near certain other pods. It helps spread pods apart for better availability.
Click to reveal answer
intermediate
Which Kubernetes field is used to specify pod affinity rules in a pod spec?
The field is affinity.podAffinity inside the pod spec's spec.affinity section.
Click to reveal answer
intermediate
How do you express that a pod should not be scheduled on the same node as pods with label app=frontend?
Use pod anti-affinity with a rule matching label app=frontend and set topologyKey to kubernetes.io/hostname to avoid the same node.
Click to reveal answer
advanced
What is the difference between requiredDuringSchedulingIgnoredDuringExecution and preferredDuringSchedulingIgnoredDuringExecution in pod affinity?
requiredDuringSchedulingIgnoredDuringExecution means the rule must be met to schedule the pod. preferredDuringSchedulingIgnoredDuringExecution means the rule is a soft preference but not mandatory.
Click to reveal answer
What does pod affinity help you do?
APrevent pods from running on any node
BSchedule pods close to other pods with specific labels
CDelete pods automatically
DScale pods horizontally
✗ Incorrect
Pod affinity helps schedule pods near other pods that match certain labels.
Which field defines pod anti-affinity rules?
Aspec.affinity.podAntiAffinity
Bspec.containers.antiAffinity
Cmetadata.labels.antiAffinity
Dspec.nodeSelector
✗ Incorrect
Pod anti-affinity rules are set under spec.affinity.podAntiAffinity.
What does setting topologyKey: kubernetes.io/hostname in pod anti-affinity do?
AIgnores node labels
BSchedules pods only on master nodes
CForces pods to run on different clusters
DAvoids scheduling pods on the same node
✗ Incorrect
This key means pods should not be scheduled on the same node (hostname).
Which affinity type is a soft preference, not mandatory?
ApreferredDuringSchedulingIgnoredDuringExecution
BrequiredDuringSchedulingIgnoredDuringExecution
CnodeAffinity
DpodSelector
✗ Incorrect
Preferred affinity is a soft rule, while required affinity is mandatory.
Pod anti-affinity is useful to:
ARun all pods on the same node
BIncrease pod CPU limits
CSpread pods across nodes for high availability
DAutomatically update pods
✗ Incorrect
Anti-affinity helps spread pods to avoid single points of failure.
Explain how pod affinity and anti-affinity affect pod scheduling in Kubernetes.
Think about how pods can be grouped or separated on nodes.
You got /4 concepts.
Describe the difference between required and preferred rules in pod affinity or anti-affinity.
Consider how strict Kubernetes is when placing pods.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of podAffinity in Kubernetes?
easy
A. To automatically restart pods on failure
B. To prevent pods from running on the same node
C. To limit the number of pods per node
D. To schedule pods close to other pods with matching labels
Solution
Step 1: Understand podAffinity concept
Pod affinity is used to place pods close to other pods that have specific labels, improving communication or resource sharing.
Step 2: Compare options with podAffinity purpose
Only To schedule pods close to other pods with matching labels describes placing pods close to others with matching labels, which matches podAffinity's goal.
Final Answer:
To schedule pods close to other pods with matching labels -> Option D
Quick Check:
podAffinity = place pods close [OK]
Hint: Affinity means placing pods near each other [OK]
Common Mistakes:
Confusing affinity with anti-affinity
Thinking affinity limits pods per node
Mixing affinity with pod restart policies
2. Which of the following is the correct syntax to specify a required pod anti-affinity rule in a pod spec?
easy
A. "podAntiAffinity": {"requiredDuringSchedulingIgnoredDuringExecution": [{"labelSelector": {"matchLabels": {"app": "web"}}, "topologyKey": "kubernetes.io/hostname"}]}
A. Empty topologyKey causes the rule to be ignored
B. Label selector is incorrect syntax
C. Required rules are always ignored by scheduler
D. podAntiAffinity must be under preferredDuringSchedulingIgnoredDuringExecution
Solution
Step 1: Check topologyKey usage
The topologyKey must be a valid node label key like kubernetes.io/hostname. Empty string disables the rule.
Step 2: Understand scheduler behavior
With empty topologyKey, the scheduler ignores the anti-affinity rule, allowing pods on same node.
Final Answer:
Empty topologyKey causes the rule to be ignored -> Option A
Quick Check:
Valid topologyKey needed for anti-affinity [OK]
Hint: topologyKey cannot be empty for anti-affinity rules [OK]
Common Mistakes:
Leaving topologyKey empty
Misplacing anti-affinity under preferred instead of required
Assuming required rules are ignored
5. 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?
hard
A. Use podAffinity with requiredDuringSchedulingIgnoredDuringExecution on app: cache and topologyKey: kubernetes.io/hostname only
B. Use podAntiAffinity with requiredDuringSchedulingIgnoredDuringExecution on app: cache and topologyKey: kubernetes.io/hostname, plus podAffinity with preferredDuringSchedulingIgnoredDuringExecution on topologyKey: failure-domain.beta.kubernetes.io/zone
C. Use podAntiAffinity with preferredDuringSchedulingIgnoredDuringExecution on app: cache and topologyKey: kubernetes.io/hostname only
D. Use podAffinity with preferredDuringSchedulingIgnoredDuringExecution on app: cache and topologyKey: failure-domain.beta.kubernetes.io/zone only
Solution
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.
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.
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.
Final Answer:
Use required podAntiAffinity on hostname and preferred podAffinity on zone -> Option B
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]
Hint: 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
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.