Lesson 32 · Section 3: Scheduling
Lesson 32: Taints and tolerations vs node affinity: keep out, or pull in
Why a toleration does not send a Pod anywhere and node affinity does not keep anyone away, how the two fail on their own, and how to combine them to dedicate nodes to a workload in the exam. · 15 min read · 3 min video
Official competency this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Two tools that look alike
By now you have met two ways to influence where a Pod runs:
- Taints and tolerations: you taint a node, and only Pods with a matching toleration may be scheduled there.
- Node affinity (and its short form
nodeSelector): you label a node, and a Pod asks for nodes with that label.
Both involve marking a node and adding something to the Pod, so it is tempting to treat them as two spellings of the same idea. They are not. They answer two different questions, they are set by two different people, and each one leaves a hole that only the other can close. This lesson is about that hole, because exam tasks are often written to see whether you fall into it.
The same goal, three attempts
Take a two-node cluster. node-1 has an expensive GPU, node-2 is an ordinary worker. You want two things:
- The training Pod
trainruns onnode-1, and only there. - Ordinary Pods such as
webstay offnode-1, so they do not use up its capacity.
Attempt 1: taints and tolerations only
kubectl taint nodes node-1 gpu=true:NoSchedule
and train gets a matching toleration.
Parts:
- Pod train (tolerates gpu taint)
- Pod web (no toleration)
- node-1 (GPU) (taint gpu=true)
- node-2 (no taint, no label)
Connections:
- Pod train to node-1 (GPU): allowed
- Pod train to node-2: allowed too
- Pod web to node-2: only here
Wish 2 is granted: web has no toleration, so the scheduler filters node-1 out for it. Wish 1 is not. A
toleration only removes an obstacle. With it, node-1 and node-2 are both acceptable for train, and the
scheduler picks whichever scores higher. If node-2 has more free resources, train lands there, next to web,
and without its GPU.
Attempt 2: node affinity only
kubectl taint nodes node-1 gpu=true:NoSchedule- # remove the taint again
kubectl label nodes node-1 gpu=true
and train gets a required node affinity for gpu=true.
Parts:
- Pod train (requires gpu=true)
- Pod web (no rules)
- node-1 (GPU) (label gpu=true)
- node-2 (no taint, no label)
Connections:
- Pod train to node-1 (GPU): only here
- Pod web to node-1 (GPU): allowed too
- Pod web to node-2: allowed
Now wish 1 is granted: for train, every node without the label is filtered out, so it can only run on node-1.
Wish 2 is not. A label is just a fact about the node. web asks for nothing, so both nodes pass its filters, and it
can land on node-1. Enough Pods like web, and node-1 is full when train arrives: train stays Pending,
because the only node it accepts has no room.
Attempt 3: both
Keep the taint and the label on node-1, and give train the toleration and the affinity.
Parts:
- Pod train (tolerates + requires)
- Pod web (no rules)
- node-1 (GPU) (taint + label)
- node-2 (no taint, no label)
Connections:
- Pod train to node-1 (GPU): only here
- Pod web to node-2: only here
The taint keeps everyone else out, the toleration lets train in, and the affinity stops train from going
anywhere else. This pattern is called dedicated nodes, and it is the standard way to reserve nodes for a team,
for special hardware or for a noisy workload.
Side by side
| Taints and tolerations | Node affinity (and nodeSelector) | |
|---|---|---|
| Question answered | Which Pods may this node accept? | Which nodes does this Pod accept? |
| Marked on the node with | A taint: kubectl taint nodes | A label: kubectl label nodes |
| Written in the Pod as | spec.tolerations | spec.affinity.nodeAffinity or spec.nodeSelector |
| Who it affects by default | Every Pod, unless it opts out with a toleration | Only the Pods that carry the rule |
| Direction | Repels: keeps Pods away from a node | Attracts: pulls a Pod to matching nodes |
| Guarantees the Pod runs on that node | No | Yes, with the required... form |
| Guarantees other Pods stay away | Yes, with NoSchedule or NoExecute | No |
| Soft version | Effect PreferNoSchedule | preferredDuringSchedulingIgnoredDuringExecution |
| Effect on Pods already running | NoExecute evicts Pods that do not tolerate it | None: IgnoredDuringExecution |
| Typical owner | The cluster administrator | The author of the workload |
The row "who it affects by default" is the heart of the difference. A taint is opt-out: it applies to all Pods without anyone editing them, which is exactly what you want for protection. Affinity is opt-in: it changes nothing for Pods that do not mention it, which is exactly what you want for a wish of one workload.
How it works underneath
Both rules are checked by the scheduler, in the filter phase you saw in the kube-scheduler lesson, by two separate plugins:
| Plugin | Looks at | A node is rejected when |
|---|---|---|
TaintToleration | The node's taints and the Pod's tolerations | A NoSchedule or NoExecute taint is left over that the Pod does not tolerate |
NodeAffinity | The node's labels and the Pod's nodeSelector and required node affinity | The labels do not satisfy the Pod's rules |
A node must pass every filter. That is why the two mechanisms combine so cleanly: with both in place, the set of
nodes left for train is "labelled gpu=true" and "no untolerated taint", and the set left for web is "no
untolerated taint", which excludes node-1.
The same two plugins also score. PreferNoSchedule taints and preferred... affinity rules never reject a node;
they only move it up or down the ranking.
Under the hood · Why Kubernetes keeps two mechanisms instead of one
The two halves belong to different people and change at different times.
- Taints are node policy. An administrator, or Kubernetes itself, marks a node as special or unhealthy, and the
rule must hold for workloads that have never heard of it. The control plane taint
(
node-role.kubernetes.io/control-plane:NoSchedule) and the automaticnode.kubernetes.io/not-readyandnode.kubernetes.io/unreachabletaints work this way: no Pod author has to do anything to be kept away. - Affinity is workload need. Only the author knows that the Pod needs an SSD, a zone or a GPU. Nodes just
publish facts as labels (many are set for you, such as
kubernetes.io/hostname,kubernetes.io/osandtopology.kubernetes.io/zone), and each Pod chooses among them.
There is also a difference after scheduling. A NoExecute taint is enforced on running Pods: the node lifecycle
controller evicts the ones that do not tolerate it, at once or after their tolerationSeconds. Node affinity has no
such form. IgnoredDuringExecution means that removing the label from a node leaves the Pods on it running.
On shared clusters, administrators often do not trust every team to add the right rules by hand. Two admission
plugins, PodNodeSelector and PodTolerationRestriction, can add a node selector and tolerations to every Pod of a
namespace automatically. Admission controllers have their own lessons later in this section.
Which one do I need?
| The task says | Use |
|---|---|
"No Pods should be scheduled on node-1, except ..." | Taint on the node, toleration on the exceptions |
"Pod x must run on a node labelled disk=ssd" | nodeSelector or required node affinity |
"Pod x should prefer nodes labelled ..., but may run elsewhere" | Preferred node affinity |
"Try to keep Pods off node-1, but do not block them" | Taint with effect PreferNoSchedule |
| "Run on nodes in zone a or zone b", "any node except ..." | Node affinity (In with several values, NotIn) |
"Remove the Pods that are running on node-1 now" | Taint with effect NoExecute (or kubectl drain) |
"node-1 is reserved for team blue: only their Pods, and their Pods only there" | Both: taint and label on the node, toleration and affinity on the Pods |
| "Run this Pod on the control plane node" | Both: tolerate the control plane taint and select the control plane label |
Using it
Example · Dedicate a node to one workload
Reserve node-1 for the train Pod. Start with the node: one taint, one label.
kubectl taint nodes node-1 gpu=true:NoSchedule
kubectl label nodes node-1 gpu=true
Then the Pod. Generate the skeleton and add the two blocks under spec:
kubectl run train --image=busybox:1.37 --dry-run=client -o yaml --command -- sleep 3600 > train.yaml
apiVersion: v1
kind: Pod
metadata:
name: train
spec:
tolerations:
- key: gpu
operator: Equal
value: "true"
effect: NoSchedule
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: gpu
operator: In
values: ["true"]
containers:
- name: train
image: busybox:1.37
command: ["sleep", "3600"]
kubectl apply -f train.yaml
kubectl run web --image=nginx:1.29-alpine
kubectl get pods -o wide # train on node-1, web on node-2
When the rule is a plain "label equals value", nodeSelector is the same filter with far less typing. Replace the
whole affinity block with:
nodeSelector:
gpu: "true"
Example · Run a Pod on the control plane node
A kubeadm control plane node carries the taint node-role.kubernetes.io/control-plane:NoSchedule and the label
node-role.kubernetes.io/control-plane with an empty value. Check both before you write anything:
kubectl describe node controlplane | grep -i taints
kubectl get node controlplane --show-labels
A toleration alone only makes the control plane node possible; the Pod will usually land on a worker. Add the selector to make it certain:
apiVersion: v1
kind: Pod
metadata:
name: audit
spec:
tolerations:
- key: node-role.kubernetes.io/control-plane
operator: Exists
effect: NoSchedule
nodeSelector:
node-role.kubernetes.io/control-plane: ""
containers:
- name: audit
image: busybox:1.37
command: ["sleep", "3600"]
operator: Exists matches the taint whatever its value, which suits a taint that has none. The label also has no
value, so the selector asks for the empty string "".
Example · Find out which of the two rules blocks a Pod
train is Pending. The event names the filter that failed on each node:
kubectl describe pod train | grep -A 3 FailedScheduling
Warning FailedScheduling default-scheduler 0/2 nodes are available: 1 node(s) didn't match Pod's node
affinity/selector, 1 node(s) had untolerated taint {gpu: true}.
Read it per node: one node (node-2) has no matching label, which is correct, and one node (node-1) has a taint
the Pod does not tolerate, which is the bug. Compare the two sides character by character:
kubectl describe node node-1 | grep -i taints # gpu=true:NoSchedule
kubectl get pod train -o jsonpath='{.spec.tolerations}' # key, value and effect must all match
kubectl get nodes -L gpu # which nodes carry the label, and its value
Tolerations and affinity cannot be changed on a running Pod (you can only add tolerations). Fix the file and
recreate the Pod with kubectl replace --force -f train.yaml, or, for a Deployment, edit the Pod template and let
it roll out.
Tricks
Common mistakes
- Expecting a toleration to place the Pod. It only allows the tainted node. Without affinity or a node selector, the Pod can run on any untainted node as well.
- Expecting a label and affinity to reserve the node. Other Pods ignore your label. Without a taint, they can fill the node.
- Doing only the Pod half, or only the node half. A dedicated node needs four things: taint and label on the
node, toleration and affinity on the Pod. A toleration for a taint that was never applied changes nothing, and an
affinity for a label that no node has leaves the Pod
Pending. - Tainting every node and forgetting the workloads that must still run. A custom
NoScheduletaint also keeps out DaemonSet Pods and new system Pods such as CoreDNS, unless they tolerate it. DaemonSet Pods only get automatic tolerations for the built-in node condition taints, not for yours. - Using
NoExecutewhen you meantNoSchedule.NoExecutealso evicts the Pods that are already running on the node and do not tolerate the taint. Node affinity never evicts anything. - Using
nodeNameto "make sure".nodeNameskips the scheduler, so neither taints (exceptNoExecute) nor affinity is checked. It satisfies the letter of "runs on node-1" but not a task that asks for scheduling rules. - Putting the rules in the wrong place for a Deployment. Both blocks belong in the Pod template,
spec.template.spec, not in the Deployment's ownspec.
Recap
- A taint is set on a node and keeps away every Pod that does not tolerate it. A toleration is a pass, not a destination.
- Node affinity (or
nodeSelector) is set on a Pod and restricts it to labelled nodes. It does nothing to other Pods. - Taints alone: your Pod may land elsewhere. Affinity alone: other Pods may land on your node.
- Dedicated nodes need both: taint and label on the node, toleration and affinity on the Pod.
- A
PendingPod tells you which half failed:untolerated taintordidn't match Pod's node affinity/selector.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
2 hands-on labs for this lesson. You get goals, not steps: any solution that reaches them passes.
Dedicate a node to one team: a taint keeps everyone else off it, and a second mechanism keeps the team's own pods from wandering to the shared nodes.
Build · Level 3 · challenge · target 18 min
Section capstone on a three-node cluster. Reserve a node for one team, keep a search engine on fast disks, run an agent on every node whatever its taints, set resource defaults for a namespace, and place a Pod no scheduler will take.
Build · Level 3 · challenge · target 35 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.