DevOps Glossary

Kubernetes Pod Affinity and Anti-Affinity

Kubernetes Pod Affinity and Anti-Affinity are scheduling rules that place pods together or apart using labels and topology.

Kubernetes Pod Affinity and Anti-Affinity are scheduling rules that tell Kubernetes where to place pods based on labels on other pods already running in the cluster. Pod affinity places related pods near each other, such as on the same node, rack, zone, or region, while pod anti-affinity keeps pods apart to reduce shared failure risk. These rules use label selectors and a topology key, such as kubernetes.io/hostname for node-level placement or topology.kubernetes.io/zone for zone-level placement, and can be set as required rules or preferred rules. A common example is placing an application pod close to a cache pod for lower latency, while spreading replicas of the same service across different nodes for better availability. Affinity and anti-affinity are useful in Kubernetes platforms including Azure Kubernetes Service, but they should be used carefully because strict rules can leave pods pending if the cluster does not have enough matching capacity.