Sub topic 11
Attracting versus repelling: affinity and taints
This chapter assumes you can write a pod manifest, label nodes, and read the nodeSelector and nodeAffinity examples from earlier chapters.
Two tools, two directions
Node affinity and taints both influence which node a pod lands on, but they work from opposite sides.
- Node affinity is set on the pod. It says "I want nodes with these labels." It attracts the pod to certain nodes.
- A taint is set on the node. It says "keep pods away unless they tolerate me." It repels pods from that node.
- A toleration is set on the pod. It permits the pod to run on a tainted node. It does not pull the pod toward that node.
| Feature | Set on | Effect | Default for pods without it |
|---|---|---|---|
| Node affinity | Pod | Attracts to matching nodes | Pod may run on any node |
| Taint | Node | Repels pods | Pod is rejected by tainted node |
| Toleration | Pod | Allows tainted nodes | Pod cannot use tainted nodes |
Taint effects
A taint has a key, an optional value, and an effect. The effect decides how strongly the node repels pods.
NoSchedule: the scheduler will not place new pods without a matching toleration.PreferNoSchedule: the scheduler tries to avoid the node but may use it.NoExecute: new pods are rejected, and running pods without a toleration are evicted.
Add and remove a taint with kubectl. The trailing hyphen removes it.
kubectl taint nodes worker-1 dedicated=gpu:NoSchedule
kubectl describe node worker-1 | grep Taints
kubectl taint nodes worker-1 dedicated=gpu:NoSchedule-Why a toleration is not enough
A toleration only removes a barrier. A pod that tolerates dedicated=gpu can still be scheduled on any untainted node. To reserve nodes for certain pods, you need both sides:
- Taint the nodes so other pods stay away.
- Label the same nodes so the pod can be attracted to them.
- Give the pod a toleration and a node affinity rule.
kubectl taint nodes worker-1 dedicated=gpu:NoSchedule
kubectl label nodes worker-1 dedicated=gpuapiVersion: v1
kind: Pod
metadata:
name: gpu-job
spec:
tolerations:
- key: dedicated
operator: Equal
value: gpu
effect: NoSchedule
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: dedicated
operator: In
values:
- gpu
containers:
- name: nginx
image: nginx:1.27The scheduler first removes nodes the pod cannot tolerate, and it also removes nodes that fail the affinity rule. Only nodes that pass both checks remain.
Choosing between them
Use a taint when the node owner wants to protect a node, for example GPU nodes, control plane nodes, or nodes being drained. Pods opt in with a toleration, so you do not edit every other workload.
Use node affinity when the pod owner wants to choose where a workload runs, for example a zone or a disk type.
Use both when you want dedicated nodes that only certain pods use, and those pods use nothing else.
Differences in running pods
Both mechanisms are applied at scheduling time, with one exception. Required node affinity ends in IgnoredDuringExecution, so changing a node label later does not evict a running pod. The NoExecute taint effect does evict running pods that lack a toleration. A toleration can include tolerationSeconds to delay that eviction.
Common errors
- A pod has a toleration but stays on untainted nodes. The toleration does not attract. Add affinity.
- A pod is Pending with an event like "had untolerated taint". The
kubectl describe podoutput shows which taint blocked each node. - A taint and a toleration differ in
effectorvalue. The toleration then does not match, so the taint still blocks the pod.
Exercise
You tainted three nodes with team=data:NoSchedule. A pod with a matching toleration is sometimes scheduled on other, untainted nodes. What do you add to keep it on the three nodes, and where does each setting go?
Show answer
Label the three nodes, for example team=data, and add a required node affinity rule on the pod that matches that label. The taint stays on the nodes, and the toleration and the affinity rule go in the pod spec. The toleration lets the pod onto the tainted nodes. The affinity rule keeps it off the others.