All guides

Taints, tolerations and affinity: decide where pods run

A taint lets a node repel pods. Node selectors and node affinity attract pods to nodes. Pod anti-affinity and topology spread constraints place replicas relative to each other.

Lines of code on a screen, coloured by syntax

Taints and tolerations

A taint lets a node repel a set of pods. A toleration on a pod allows the scheduler to place it on nodes with a matching taint, and it does not guarantee that placement.

Shell

kubectl taint nodes <node-name> dedicated=batch:NoSchedule
kubectl label nodes <node-name> dedicated=batch

The first command places a taint with the key dedicated, the value batch and the effect NoSchedule. The key and the value are yours to choose. The same command with a trailing - removes the taint. The second command adds a label for the node affinity further down. A taint has one of three effects:

  • NoSchedule: no new pods are scheduled on the node unless they have a matching toleration. Pods already running on the node are not evicted.
  • PreferNoSchedule: the soft version. The control plane tries to avoid the node, and this is not guaranteed.
  • NoExecute: pods that do not tolerate the taint are evicted immediately. A pod whose toleration sets tolerationSeconds stays bound for that time.

A toleration does not keep a pod on the tainted nodes. The documentation says so in its dedicated-nodes use case: a pod with the toleration may land on the tainted nodes and equally on any other node of the cluster.

The node controller also taints nodes on its own when certain conditions are true, for example with node.kubernetes.io/unreachable. Kubernetes adds a toleration for that taint with tolerationSeconds=300 to pods, unless you set one explicitly.

The nodeSelector field

nodeSelector is the simplest recommended form of node selection. The pod is scheduled only onto a node that carries every label you specify. The documentation's example:

YAML

nodeSelector:
  disktype: ssd

Node affinity

Node affinity functions like nodeSelector with a more expressive syntax. It supports the operators In, NotIn, Exists, DoesNotExist, Gt and Lt, and it has two types:

  • requiredDuringSchedulingIgnoredDuringExecution: the scheduler cannot schedule the pod unless the rule is met.
  • preferredDuringSchedulingIgnoredDuringExecution: the scheduler looks for a node that meets the rule and, finding none, schedules the pod anyway. Each preference carries a weight between 1 and 100.

IgnoredDuringExecution means that if node labels change after the pod is scheduled, the pod continues to run. If a pod specifies both nodeSelector and node affinity, both must be satisfied.

Pod anti-affinity

Inter-pod affinity and anti-affinity constrain a pod by the labels of pods that already run in a topology domain. The form of an anti-affinity rule: this pod should not run in a domain that already runs pods matching the selector. topologyKey is the key of the node label that denotes the domain, such as kubernetes.io/hostname or topology.kubernetes.io/zone.

YAML

affinity:
  podAntiAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          labelSelector:
            matchLabels:
              app: my-app
          topologyKey: kubernetes.io/hostname

With the required type, only a single matching pod can be scheduled into a single topology domain. The documentation also warns that inter-pod affinity requires substantial processing, and does not recommend it in clusters larger than several hundred nodes.

Topology spread constraints

Topology spread constraints control how pods are spread across domains such as zones and nodes. With whenUnsatisfiable: DoNotSchedule, which is the default, maxSkew caps how far the count of matching pods in one domain may exceed the global minimum. With ScheduleAnyway, the scheduler still schedules the pod and prioritizes nodes that reduce the skew.

This pod template tolerates the taint from the first section, requires the labeled nodes and spreads its replicas over zones:

YAML

spec:
  tolerations:
    - key: dedicated
      operator: Equal
      value: batch
      effect: NoSchedule
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
              - key: dedicated
                operator: In
                values: ["batch"]
  topologySpreadConstraints:
    - maxSkew: 1
      topologyKey: topology.kubernetes.io/zone
      whenUnsatisfiable: DoNotSchedule
      labelSelector:
        matchLabels:
          app: my-app

If you configure no cluster-level defaults, the scheduler acts as if these were set: a maxSkew of 3 on kubernetes.io/hostname and of 5 on topology.kubernetes.io/zone, both with ScheduleAnyway.

Which mechanism to use

The documentation's own use cases map to the mechanisms like this:

  • Keep pods off nodes with special hardware, such as GPUs: taint those nodes, and add a toleration to the pods that use the hardware.
  • Dedicate nodes to a set of users: a taint and a toleration, plus a matching node label and a required node affinity, so that the pods only schedule onto those nodes.
  • Put a pod on a node with an SSD: a label and nodeSelector. Node affinity adds operators and soft rules.
  • Keep replicas of one workload on separate nodes: required pod anti-affinity with the topology key kubernetes.io/hostname.
  • Co-locate pods that communicate a lot, such as web servers and their cache: pod affinity.
  • Distribute pods across zones or nodes for availability: topology spread constraints.

Mistakes to avoid

  1. A toleration with no node affinity. The pods may use the tainted nodes and every other node as well.
  2. A required rule that no node meets. The scheduler cannot schedule the pod.
  3. A spread constraint whose labelSelector does not match the pod's own labels. The pod does not count itself, and such pods can accumulate in the same domain.
  4. Nodes that lack the topologyKey label. Pod anti-affinity requires nodes to be consistently labeled.
  5. Expecting constraints to hold after pods are removed. Scaling down a Deployment may leave the distribution imbalanced, and the documentation points to the Descheduler to rebalance it.
  6. Setting nodeName on a pod. It bypasses the scheduler, and the pod is bound to the node even if the node has NoSchedule taints.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides