Sub topic 7
Tolerating a taint does not place a pod on the node
This chapter assumes you can write a pod manifest, use kubectl describe, and know that the scheduler picks a node for each pending pod. It also assumes you have seen how a toleration matches a taint.
A toleration only removes a restriction
A taint repels pods from a node. A toleration tells the scheduler that a pod accepts that repulsion. The toleration does not ask for the node. It only removes one reason the scheduler would have rejected it.
Consider a cluster where node1 carries the taint dedicated=gpu:NoSchedule. A pod with a matching toleration is allowed on node1. It is also still allowed on every untainted node. The scheduler chooses among all nodes that pass its filters, and node1 is one of them.
apiVersion: v1
kind: Pod
metadata:
name: gpu-job
spec:
containers:
- name: app
image: nginx
tolerations:
- key: "dedicated"
operator: "Equal"
value: "gpu"
effect: "NoSchedule"This pod may land on node1, but nothing steers it there. If the other nodes have free capacity, it can run on any of them.
Two tools, two directions
Taints and tolerations control which pods a node accepts. Node affinity and node selectors control which nodes a pod wants. You need both to build a dedicated node.
| Mechanism | Set on | Effect |
|---|---|---|
| Taint | Node | Repels pods that do not tolerate it |
| Toleration | Pod | Allows the pod onto tainted nodes |
| Node label with affinity or selector | Node and pod | Attracts the pod to matching nodes |
The taint keeps other pods out. The affinity rule keeps your pod in. Without the taint, other workloads can fill the node. Without the affinity, your pod can end up elsewhere.
Pinning a pod to the tainted node
Label the node and taint it. Both commands target the same node.
# Label and taint the same node
kubectl label nodes node1 dedicated=gpu
kubectl taint nodes node1 dedicated=gpu:NoScheduleThen give the pod a required node affinity for the label, plus the toleration for the taint.
apiVersion: v1
kind: Pod
metadata:
name: gpu-only-job
spec:
containers:
- name: app
image: nginx
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: dedicated
operator: In
values: ["gpu"]
tolerations:
- key: "dedicated"
operator: "Equal"
value: "gpu"
effect: "NoSchedule"Now the scheduler must place gpu-only-job on a node labeled dedicated=gpu. The toleration lets it past the taint on those nodes. The pod runs only there.
Common errors
- Toleration without affinity. The pod schedules, but on the wrong node. You only notice when a GPU job lands on a plain node.
- Affinity without toleration. The pod stays
Pending. The affinity selects the tainted node, and the taint rejects the pod. The events show an untolerated taint. - Taint without label. You tainted the node but gave the pod nothing to select. Add the label so affinity has something to match.
- Preferred affinity. With
preferredDuringSchedulingIgnoredDuringExecution, the scheduler only favors the node. The pod can still run elsewhere if the node is full.
Check your understanding
Exercise
A node has the taint dedicated=gpu:NoSchedule. Your pod tolerates it, but the pod keeps landing on an untainted node. Why, and what do you add to fix it?
Show answer
The toleration only permits the pod on the tainted node. It does not attract the pod, so the scheduler may choose any node that passes its filters, including untainted ones. Label the node (for example dedicated=gpu) and add a required node affinity or nodeSelector for that label to the pod.
Exercise
A pod has required node affinity for dedicated=gpu but no toleration. The only node with that label has the taint dedicated=gpu:NoSchedule. What state is the pod in, and where do you see the reason?
Show answer
The pod is Pending. Run kubectl describe pod and read the Events section, which reports that the node had an untolerated taint.