7.15 Kubernetes Scheduling Label Usage
Kubernetes uses scheduling labels to assign pods to nodes, enabling efficient resource management and workload distribution across the cluster.
Kubernetes Scheduling Label Usage is the way the scheduler consults labels on Pods themselves, rather than on nodes, to make placement decisions relative to other already-running Pods, encompassing Pod affinity and anti-affinity for co-location or separation preferences, and topology spread constraints for balanced distribution, extending label-driven scheduling beyond the node-targeting mechanisms of nodeSelector and node affinity.
Pod Affinity for Co-Location
Selecting Pods to Schedule Near
Pod affinity, expressed through spec.affinity.podAffinity, uses a label selector to identify a set of existing Pods, and schedules the new Pod onto a node within the same topology domain, defined by a specified topologyKey, as at least one Pod matching that selector, useful for co-locating tightly coupled workloads that benefit from network locality.
The Role of topologyKey
The topologyKey field determines the granularity of "nearness" being expressed, commonly a node's hostname label for same-node co-location, or a zone label for same-availability-zone co-location, meaning the same podAffinity selector can express very different placement outcomes depending on which topology dimension it references.
Pod Anti-Affinity for Separation
Selecting Pods to Schedule Away From
Pod anti-affinity works as the inverse of Pod affinity, using a label selector to identify existing Pods and steering the new Pod away from nodes within the same topology domain as any matching Pod, commonly used to spread replicas of the same application across different nodes or zones to improve fault tolerance.
Common Use for Replica Spreading
A frequent pattern uses anti-affinity with a selector matching a Deployment's own Pods and a hostname-based topologyKey, ensuring no two replicas of the same application land on the same physical node, reducing the impact of a single node failure on that application's overall availability.
Required Versus Preferred Terms
Hard and Soft Variants for Both Affinity Types
Both Pod affinity and anti-affinity support requiredDuringSchedulingIgnoredDuringExecution and preferredDuringSchedulingIgnoredDuringExecution variants, mirroring node affinity's distinction between a hard constraint the scheduler must satisfy and a soft preference it attempts to honor when feasible, giving workloads graduated control over how strictly a co-location or separation preference is enforced.
IgnoredDuringExecution Semantics
The "IgnoredDuringExecution" portion of these term names reflects that once a Pod is scheduled and running, subsequent label changes on either the Pod itself or the Pods it was matched against do not retroactively cause the already-placed Pod to be rescheduled or evicted, even if the original affinity condition would no longer hold.
Topology Spread Constraints
A Purpose-Built Alternative to Anti-Affinity Spreading
Topology spread constraints, expressed through spec.topologySpreadConstraints, provide a more direct and fine-tuned mechanism specifically for even distribution, using a labelSelector to identify the Pod group being spread and a topologyKey to define the spreading domain, alongside a maxSkew value controlling how uneven the distribution is allowed to become.
Advantages Over Anti-Affinity for Spreading Use Cases
Compared to using anti-affinity purely for spreading purposes, topology spread constraints offer more precise, quantifiable control over distribution evenness across many topology domains simultaneously, rather than anti-affinity's more binary avoid-or-allow logic per domain, making them the generally preferred mechanism specifically for balanced spreading scenarios.
Label Selector Precision in Scheduling Contexts
Why Selector Scope Matters for Scheduling Outcomes
Because these scheduling mechanisms directly influence placement decisions with real availability and performance consequences, an overly broad label selector, matching more Pods than intended, can produce unexpected co-location or spreading behavior, while an overly narrow selector might fail to capture the full intended Pod group, undermining the fault-tolerance or performance goals the affinity or spread configuration was meant to achieve.
Namespace Scoping Considerations
Pod affinity and anti-affinity selectors can optionally be scoped to specific namespaces via a namespaces field or a namespaceSelector, since by default they only consider Pods within the same namespace as the Pod being scheduled, a detail that matters when co-location or separation needs to account for Pods living in a different namespace than the one currently being scheduled into.