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.