Node affinity and pod affinity are Kubernetes scheduling features that influence where pods run in a cluster. They allow operators to control placement based on node labels or the presence of other pods. These mechanisms help align workloads with infrastructure capabilities, performance goals, and architectural requirements.
How It Works
Node affinity uses labels assigned to nodes, such as region, instance type, or hardware capability. A pod specification defines rules that either require or prefer scheduling on nodes with matching labels. Required rules must be satisfied for scheduling to occur. Preferred rules act as soft constraints, guiding the scheduler toward better placement without enforcing it strictly.
Pod affinity and anti-affinity extend this logic to relationships between pods. Pod affinity attracts a pod to nodes where specific other pods already run. Pod anti-affinity does the opposite, ensuring separation across nodes, zones, or other topology domains. These rules rely on label selectors and topology keys such as kubernetes.io/hostname or topology.kubernetes.io/zone.
The scheduler evaluates these constraints during placement. Hard constraints filter eligible nodes first. Soft constraints then influence scoring and ranking before final selection. This layered approach allows precise yet flexible control over workload distribution.
Why It Matters
These controls support performance optimization, compliance, and resilience. For example, you can place latency-sensitive services on SSD-backed nodes, co-locate tightly coupled microservices to reduce network overhead, or spread replicas across zones for high availability.
They also improve cost and resource efficiency. Teams can target specialized nodes such as GPU instances only when required and prevent noisy neighbors from impacting critical services. This structured placement reduces operational surprises and aligns infrastructure behavior with architectural intent.
Key Takeaway
Node and pod affinity give you fine-grained, policy-driven control over workload placement to optimize performance, resilience, and resource utilization in Kubernetes clusters.