What if your apps could choose their best neighbors automatically to run faster and safer?
Why Pod affinity and anti-affinity in Kubernetes? - Purpose & Use Cases
Imagine you have many small apps running on different servers, and you want some apps to be close together for speed, while others must stay apart to avoid crashing all at once.
Manually placing apps on servers is slow and confusing. You might put two apps that need to be close far apart, or accidentally put all copies of one app on the same server, risking failure if that server goes down.
Pod affinity and anti-affinity let Kubernetes automatically place apps where they should be--either close together or separated--without you having to manage each server manually.
Schedule pods by hand on specific nodes using labels and guessworkaffinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: 'app'
operator: In
values: ['frontend']
topologyKey: 'kubernetes.io/hostname'It enables reliable, automatic app placement that improves performance and availability without extra manual work.
Running a web app where frontend pods stay on the same server for fast communication, while database pods spread out to avoid all failing together.
Manual pod placement is slow and error-prone.
Pod affinity and anti-affinity automate smart pod placement.
This improves app speed and reliability effortlessly.
Practice
podAffinity in Kubernetes?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 DQuick Check:
podAffinity = place pods close [OK]
- Confusing affinity with anti-affinity
- Thinking affinity limits pods per node
- Mixing affinity with pod restart policies
Solution
Step 1: Identify required pod anti-affinity syntax
Required pod anti-affinity usespodAntiAffinitywithrequiredDuringSchedulingIgnoredDuringExecutionand a label selector plus topology key.Step 2: Match syntax with options
"podAntiAffinity": {"requiredDuringSchedulingIgnoredDuringExecution": [{"labelSelector": {"matchLabels": {"app": "web"}}, "topologyKey": "kubernetes.io/hostname"}]} correctly usespodAntiAffinitywithrequiredDuringSchedulingIgnoredDuringExecutionand proper labelSelector and topologyKey.Final Answer:
Correct required pod anti-affinity syntax -> Option AQuick Check:
Required anti-affinity uses podAntiAffinity + requiredDuringScheduling [OK]
- Using podAffinity instead of podAntiAffinity
- Confusing required and preferred rules
- Wrong topologyKey values
app: frontend is scheduled?affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: backend
topologyKey: kubernetes.io/hostnameSolution
Step 1: Understand required podAffinity behavior
Required podAffinity means the pod must be placed on a node where pods with matching labels already run, here app=backend.Step 2: Apply to given pod spec
The pod with label app=frontend requires a node with pods labeled app=backend, so it will schedule only on such nodes.Final Answer:
The pod will be scheduled on a node that already runs a pod with label app=backend -> Option CQuick Check:
Required podAffinity = schedule near matching pods [OK]
- Thinking podAffinity prevents scheduling near pods
- Confusing required and preferred affinity
- Assuming scheduling fails without matching pods
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: database
topologyKey: ""
What is the likely cause?
Solution
Step 1: Check topologyKey usage
ThetopologyKeymust be a valid node label key likekubernetes.io/hostname. Empty string disables the rule.Step 2: Understand scheduler behavior
With emptytopologyKey, the scheduler ignores the anti-affinity rule, allowing pods on same node.Final Answer:
Empty topologyKey causes the rule to be ignored -> Option AQuick Check:
Valid topologyKey needed for anti-affinity [OK]
- Leaving topologyKey empty
- Misplacing anti-affinity under preferred instead of required
- Assuming required rules are ignored
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?Solution
Step 1: Prevent pods on same node strictly
UsepodAntiAffinitywithrequiredDuringSchedulingIgnoredDuringExecutionandtopologyKey: kubernetes.io/hostnameto ensure no two pods run on the same node.Step 2: Prefer pods in same zone
AddpodAffinitywithpreferredDuringSchedulingIgnoredDuringExecutionandtopologyKey: failure-domain.beta.kubernetes.io/zoneto encourage scheduling pods in the same zone but not strictly.Step 3: Combine both rules
UsepodAntiAffinitywithrequiredDuringSchedulingIgnoredDuringExecutiononapp: cacheandtopologyKey: kubernetes.io/hostname, pluspodAffinitywithpreferredDuringSchedulingIgnoredDuringExecutionontopologyKey: failure-domain.beta.kubernetes.io/zonecombines 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 BQuick Check:
Strict anti-affinity + preferred affinity = UsepodAntiAffinitywithrequiredDuringSchedulingIgnoredDuringExecutiononapp: cacheandtopologyKey: kubernetes.io/hostname, pluspodAffinitywithpreferredDuringSchedulingIgnoredDuringExecutionontopologyKey: failure-domain.beta.kubernetes.io/zone[OK]
- Using affinity instead of anti-affinity to separate pods
- Using preferred instead of required for strict separation
- Ignoring zone preference
Ready to prove it?
Pass the test and this pattern moves unknown
