KubeDojo

Node Affinity and Pod Affinity/Anti-Affinity

AK
by Alexis Kinsella··16 min read
Node Affinity and Pod Affinity/Anti-Affinity

You scaled your StatefulSet to five replicas and two of them landed on the same node. The node went down and took 40% of your capacity with it. Or you rolled out a new Deployment version and the scheduler deadlocked because new Pods had affinity to old Pods that were being terminated. nodeSelector does not solve these problems. You need rules that express "spread across failure domains" and "co-locate with my cache, but never stack two of me on the same host."

Kubernetes gives you three complementary mechanisms for this: node affinity constrains Pods to labeled nodes with soft or hard rules, pod affinity and anti-affinity control co-location and spreading relative to other Pods, and topology spread constraints distribute replicas proportionally across zones, nodes, or any custom topology domain. Together with taints and tolerations (covered in cka-taints-tolerations) and resource requests (covered in cka-resource-management), they form the complete scheduling toolkit.

Sign in to access this lesson

Create a free account or sign in to enroll in the CKA — Certified Kubernetes Administrator course and access all 63 lessons.

CKA — Certified Kubernetes Administrator

63 lessons

Browse the full course curriculum →