Sub topic 5
Tolerations: letting a pod accept a taint
This chapter assumes you know what a taint is (a key, value, and effect on a node) and the three effects. It also assumes you can write a pod manifest and run kubectl apply and kubectl describe.
What a toleration is
A taint on a node says "keep pods away unless they accept this." A toleration is the pod's way of accepting. You add it to the pod's spec.tolerations list. When the scheduler considers a node, it compares each of the node's taints with the pod's tolerations. A taint that some toleration matches no longer blocks the pod.
A toleration does not change the node and does not tell the scheduler to prefer that node. It only removes one reason to reject the node.
A first toleration
Suppose you tainted a node like this:
kubectl taint nodes node1 dedicated=gpu:NoScheduleThis pod accepts that taint:
apiVersion: v1
kind: Pod
metadata:
name: gpu-job
spec:
containers:
- name: app
image: nginx
tolerations:
- key: "dedicated"
operator: "Equal"
value: "gpu"
effect: "NoSchedule"The tolerations field belongs to the pod spec, at the same level as containers. It is not part of a container definition.
Each toleration has four fields:
| Field | Meaning |
|---|---|
| `key` | The taint key to match, here `dedicated` |
| `operator` | `Equal` compares the value too. `Exists` ignores the value |
| `value` | The taint value to match. Used with `Equal` only |
| `effect` | The taint effect to match. Leave it out to match every effect for that key |
The next chapter covers the exact matching rules. For now, the four fields in the example must line up with the taint: key dedicated, value gpu, effect NoSchedule.
Tolerating several taints
A pod can list several tolerations. Each one handles the taints it matches. If a node has two taints, the pod needs a toleration for each one before the node counts as acceptable for NoSchedule.
You can also use Exists when the value does not matter:
tolerations:
# Matches any value for the key "dedicated", with the NoSchedule effect
- key: "dedicated"
operator: "Exists"
effect: "NoSchedule"
# An empty key with Exists matches every taint
- operator: "Exists"Be careful with the second entry. A toleration with no key and Exists matches every taint, so the pod ignores all node restrictions. Use it only for workloads that must run everywhere.
Where to put tolerations
You rarely write bare pods. For a Deployment, StatefulSet, or Job, put the tolerations in the pod template, under spec.template.spec.tolerations. A toleration placed on the Deployment's top-level spec is invalid and the API server rejects it.
Existing pods do not change when you edit a template. The controller creates new pods from the updated template, so a Pending pod that lacks the toleration is replaced during the rollout.
Common errors
- Typo or mismatch.
value: "GPU"does not match a taint with valuegpu. Matching is case-sensitive. - Wrong effect. A toleration for
NoExecutedoes not cover aNoScheduletaint on the same key. - Wrong location. The field sits under
containersinstead ofspec. - Expecting placement. The pod stays Pending or lands on an untainted node anyway. A toleration permits a node but does not choose it. The chapter after next explains this.
- Expecting eviction of running pods. Adding a
NoScheduletaint does not remove pods that are already running, and tolerations do not matter for them. OnlyNoExecuteaffects running pods.
Checking the result
After you apply the manifest, confirm where the pod went:
kubectl get pod gpu-job -o wide
kubectl get pod gpu-job -o jsonpath='{.spec.tolerations}'The second command prints the tolerations the API server stored. You may see extra entries for node.kubernetes.io/not-ready and node.kubernetes.io/unreachable that you did not write. Kubernetes adds those automatically, and a later chapter explains them.
Exercise
A node has the taint team=data:NoSchedule. A pod has a toleration with key team, operator Equal, value data, and effect NoExecute. Can the scheduler place the pod on this node, and why?
Show answer
No. The toleration's effect (NoExecute) does not match the taint's effect (NoSchedule), so the toleration does not apply and the taint still rejects the pod. Changing the effect to NoSchedule, or removing effect so it matches all effects, would let the pod through.
Exercise
You add tolerations to the top-level spec of a Deployment and the apply fails. Where should the field go?
Show answer
Under the pod template, at spec.template.spec.tolerations, because tolerations belong to the pod spec.