Difficulty: Advanced
What is Pod anti-affinity, how do you configure it, and how does it help with high availability?
Pod anti-affinity tells the scheduler to keep certain Pods apart, usually replicas of the same app, so they do not land on the same node or zone. If all replicas sit on one node and that node fails, the whole service goes down; spreading them across failure domains means one failure only takes out part of the capacity.
Anti-affinity lives under spec.affinity.podAntiAffinity and has three key parts:
app label as this Podkubernetes.io/hostname means "not on the same node"; topology.kubernetes.io/zone means "not in the same zone".requiredDuringSchedulingIgnoredDuringExecution is a strict rule. If it cannot be satisfied, the Pod stays Pending.preferredDuringSchedulingIgnoredDuringExecution is a scored preference with a weight from 1 to 100. The scheduler tries to honour it but will co-locate Pods if needed.IgnoredDuringExecution means the rule is only checked when scheduling. Pods already running are not moved if conditions change later.
A required per-node rule with 5 replicas needs at least 5 eligible nodes. If you scale to 6 on a 5-node cluster, one Pod stays Pending, and during a rolling update the surge Pod may also be blocked. Common practice:
| Aspect | Pod anti-affinity | topologySpreadConstraints |
|---|---|---|
| Model | At most one matching Pod per domain (hard) or best effort | Keep counts balanced within maxSkew |
| Many replicas | Breaks once replicas exceed domains (required) | Spreads evenly, for example 4-3-3 over 3 zones |
| Behaviour when impossible | Pending or ignored | DoNotSchedule or ScheduleAnyway |
For "spread 12 replicas evenly across 3 zones", topology spread constraints are the better tool. Many teams use both: spread across zones and prefer distinct nodes within a zone.
ap-south-1, a typical setup is 3 or more replicas, zone spreading, preferred per-node anti-affinity and a PDB with minAvailable: 2.apiVersion: apps/v1
kind: Deployment
metadata:
name: payments-api
spec:
replicas: 3
selector:
matchLabels: { app: payments-api }
template:
metadata:
labels: { app: payments-api }
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels: { app: payments-api }
topologyKey: kubernetes.io/hostname # never 2 on one node
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchLabels: { app: payments-api }
topologyKey: topology.kubernetes.io/zone # try different zones
containers:
- name: api
image: ghcr.io/acme/payments-api:3.2.1
Each replica must be on a different node and the scheduler prefers different zones. Note that the preferred form wraps the term in podAffinityTerm.
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels: { app: payments-api }
Zone counts never differ by more than one, which scales cleanly to many replicas, unlike required anti-affinity.
Pod Anti-Affinity, topologyKey, High Availability, Topology Spread Constraints, Failure Domains