Anti-Affinity for High Availability
What you'll learn: How to use anti-affinity rules to spread replicas across zones and nodes, eliminating single points of failure.
The Core Problem
When running multiple replicas of an application for high availability, you don't want them all landing on the same node or availability zone. If that node crashes or that zone goes offline, all your replicas fail simultaneously—defeating the purpose of redundancy.
How Anti-Affinity Solves This
Pod anti-affinity tells Kubernetes: "Keep these pods away from each other." By using topology keys like kubernetes.io/hostname (node-level) or topology.kubernetes.io/zone (zone-level), you instruct the scheduler to distribute replicas across different failure domains.
Think of it like this: You wouldn't store all backup copies of critical documents in the same building. Anti-affinity is your insurance policy that spreads risk across different "buildings" (nodes) or even different "cities" (zones).
Practical Application
For a three-replica deployment, you'd set required pod anti-affinity with topology.kubernetes.io/zone as the key. This forces each replica onto a different availability zone. If one zone experiences an outage, two replicas remain operational elsewhere.
You can also use preferred anti-affinity when you want best-effort spreading without being rigid—useful when you have more replicas than available zones.
When to Use It
- Multi-replica stateless services requiring maximum uptime
- Databases with replication where primary and replicas should never colocate
- Any critical workload where zone or node failure must not cause total outage
Key Takeaway: Anti-affinity rules spread pods across failure domains (nodes/zones) to ensure that infrastructure failures affect only a subset of replicas, maintaining service availability during outages.