Node affinity and anti-affinity #
Configure node affinity and anti-affinity to steer Pod placement onto specific nodes.
Node affinity #
Node affinity lets you prefer that certain Pods run on nodes with specific labels. For example, you can configure an affinity so that Pods run only on nodes with an SSD disk.
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
Node anti-affinity #
With node anti-affinity, you can prevent certain Pods from being placed on nodes with specific labels. This is useful to avoid conflicts or spread Pods across different nodes.
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: zone
operator: NotIn
values:
- us-central1
Pod affinity and anti-affinity #
Using affinity and anti-affinity to place Pods close to or away from each other.
Pod affinity #
Pod affinity lets you place Pods close to other specific Pods using labels. This is often useful for performance reasons.
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: frontend
topologyKey: "kubernetes.io/hostname"
Pod anti-affinity #
Pod anti-affinity prevents specific Pods from being placed on the same node as other Pods selected by labels, improving application resilience.
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: backend
topologyKey: "kubernetes.io/hostname"
Taints and tolerations #
Applying taints and tolerations to reserve nodes for certain workloads.
Taint #
Taints apply restrictions to nodes, preventing Pods from running on them by default unless they have a matching toleration. This is useful to isolate resources for specific tasks.
kubectl taint nodes node1 key=value:NoSchedule
Toleration #
Tolerations allow Pods to tolerate taints applied to nodes, so Pods can be placed on nodes that are normally restricted to them.
tolerations:
- key: "key"
operator: "Equal"
value: "value"
effect: "NoSchedule"
Resource requests and limits #
Defining resource requests and limits for optimized control of resource allocation.
Resource requests #
Requests define the minimum resources a Pod needs, ensuring it is scheduled only on nodes that have those resources available.
resources:
requests:
memory: "64Mi"
cpu: "250m"
Resource limits #
Limits set the maximum resources a Pod can consume, preventing it from monopolizing a node's resources.
resources:
limits:
memory: "128Mi"
cpu: "500m"
Topology spread constraints #
Define topology spread constraints to ensure high availability.
Spread example #
Topology spread constraints let you control how Pods are spread across different domains, such as availability zones, to avoid single points of failure.
topologySpreadConstraints:
- maxSkew: 1
topologyKey: "topology.kubernetes.io/zone"
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: my-app
Priority and preemption #
Defining priority levels to ensure critical Pods get the resources they need.
Configuring priority #
Priority lets you assign importance levels to Pods, ensuring that high-priority Pods are scheduled first.
priorityClassName: high-priority
Preemption #
Preemption lets a high-priority Pod evict a lower-priority Pod when resources are limited, ensuring that critical workloads run first.
2026 update: what has changed #
Recent changes worth knowing (checked in November 2026).
New placement levers #
- Scheduling gates (stable since Kubernetes 1.30): a pod created with
schedulingGatesis not scheduled until a controller (quota, a queue such as Kueue) removes the gate. - Finer topology spreading:
matchLabelKeysspreads each Deployment revision separately;nodeAffinityPolicyandnodeTaintsPolicydefine which nodes count when computing the skew. - Dynamic Resource Allocation (DRA), stable in 1.36: GPUs and other devices are requested through
ResourceClaimobjects, and the scheduler picks a node that has the requested hardware.
apiVersion: v1
kind: Pod
metadata:
name: batch-worker
spec:
schedulingGates: # the pod stays pending until the gate is removed
- name: example.com/quota-ok
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels: { app: batch-worker }
matchLabelKeys: [pod-template-hash] # spread each revision independently
containers:
- name: worker
image: registry.example.com/worker:2.3Rebalancing afterwards #
The scheduler never moves a running pod. To fix a placement that has become unbalanced (new node, zone back online), use the descheduler, which evicts badly placed pods so the scheduler places them again.
Best practice: UsewhenUnsatisfiable: ScheduleAnywayfor workloads that must start no matter what, andDoNotSchedulefor those where high availability comes first.