Skip to content
LF Cert Trainer

Lesson 29 · Section 3: Scheduling

Lesson 29: Taints and tolerations: nodes that say no

How a taint keeps Pods away from a node and a toleration lets chosen Pods through: the three effects, how matching works, the taints Kubernetes adds by itself, and the kubectl commands and YAML you need in the exam. · 20 min read · 4 min video

1/11Taints and tolerations
0:00 / 3:58

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

Why taints exist

Left alone, the scheduler treats every node as fair game: a Pod can land on any node that has room for it. That is the right default, but real clusters have nodes that are not for everyone:

  • Control plane nodes should run the API server, etcd and friends, not your web shop.
  • Special hardware (GPU nodes, nodes with fast disks, nodes paid for by one team) should be kept for the workloads that need it.
  • Unhealthy or half-drained nodes should stop receiving work, and sometimes should lose the work they have.

You could try to solve this from the Pod side, by telling every Pod where to go. But that means changing every Pod in the cluster, including the ones written next year. A taint solves it from the node side: the node itself says "stay away", and every Pod obeys without knowing anything about it. A toleration is the exception pass you hand to the few Pods that are allowed in.

What a taint and a toleration look like

A taint repels, a toleration lets throughfitsblockedallowedalso finePod webno tolerationPod traintolerates dedicated=gpunode-1no taintsnode-2dedicated=gpu:NoSchedule
A taint repels, a toleration lets through. The taint on node-2 blocks every Pod that does not tolerate it. The toleration only removes that block: the tolerating Pod may still land on node-1.

Parts:

  • Pod web (no toleration)
  • Pod train (tolerates dedicated=gpu)
  • node-1 (no taints)
  • node-2 (dedicated=gpu:NoSchedule)

Connections:

  • Pod web to node-1: fits
  • Pod web to node-2: blocked
  • Pod train to node-2: allowed
  • Pod train to node-1: also fine

A taint has three parts, and you write it as key=value:effect:

PartExampleMeaning
KeydedicatedThe name of the taint. May have a prefix, such as node-role.kubernetes.io/control-plane
ValuegpuOptional detail. A taint can have a key and no value: maintenance:NoSchedule
EffectNoScheduleWhat happens to Pods that do not tolerate the taint (see below)

Taints are stored on the Node object, in spec.taints. A toleration is stored on the Pod, in spec.tolerations, and names the taint it forgives:

tolerations:
  - key: dedicated
    operator: Equal
    value: gpu
    effect: NoSchedule

In the diagram, node-2 is tainted dedicated=gpu:NoSchedule. The Pod web has no toleration, so node-2 is closed to it. The Pod train has the toleration above, so node-2 is open to it. Look at the last arrow, though: train may still be placed on node-1. A toleration removes an obstacle; it expresses no wish.

The three effects

The three effectsnewrunningnewrunningnewrunningPreferNoSchedulesoft: a preferenceNew Podsavoided if possibleRunning PodsstayNoSchedulehard: a filterNew Podsnever placed hereRunning PodsstayNoExecutehard, and evictsNew Podsnever placed hereRunning Podsevicted
The three effects. The effect is the part of the taint that says what happens to a Pod that does not tolerate it. Only NoExecute touches Pods that are already running.

Parts:

  • PreferNoSchedule (soft: a preference)
  • New Pods (avoided if possible)
  • Running Pods (stay)
  • NoSchedule (hard: a filter)
  • New Pods (never placed here)
  • Running Pods (stay)
  • NoExecute (hard, and evicts)
  • New Pods (never placed here)
  • Running Pods (evicted)

Connections:

  • PreferNoSchedule to New Pods: new
  • New Pods to Running Pods: running
  • NoSchedule to New Pods: new
  • New Pods to Running Pods: running
  • NoExecute to New Pods: new
  • New Pods to Running Pods: running
EffectNew Pods without a tolerationPods already running on the nodeWho enforces it
PreferNoSchedulePlaced elsewhere if possible, here if nothing else fitsStayThe scheduler (scoring)
NoScheduleNever scheduled hereStayThe scheduler (filtering)
NoExecuteNever scheduled hereEvictedThe scheduler, plus the control plane, which deletes the Pods

Two consequences are worth saying out loud:

  • NoSchedule is not retroactive. Tainting a busy node with NoSchedule changes nothing for the Pods on it. They keep running until something else deletes them. If you want the node emptied, use NoExecute or kubectl drain.
  • PreferNoSchedule is only a hint. It lowers the node's score. In a full cluster, Pods still land there, and there is no event that tells you the preference was overruled.

NoExecute and tolerationSeconds

A NoExecute taint lands on a node with running Podsnowwaitsstaysnode-1NoExecute taint addedPod ano tolerationPod btolerationSeconds: 60Pod ctolerates, no limitEvictedat onceEvictedafter 60 secondsKeeps runningfor as long as it likes
A NoExecute taint lands on a node with running Pods. What happens to each Pod depends only on its toleration: none, one with tolerationSeconds, or one without a time limit.

Parts:

  • node-1 (NoExecute taint added)
  • Pod a (no toleration)
  • Pod b (tolerationSeconds: 60)
  • Pod c (tolerates, no limit)
  • Evicted (at once)
  • Evicted (after 60 seconds)
  • Keeps running (for as long as it likes)

Connections:

  • node-1 to Pod a
  • node-1 to Pod b
  • node-1 to Pod c
  • Pod a to Evicted: now
  • Pod b to Evicted: waits
  • Pod c to Keeps running: stays

When a NoExecute taint is added to a node, each Pod on it falls into one of three groups:

  1. No matching toleration: the Pod is evicted straight away.
  2. A matching toleration with tolerationSeconds: the Pod stays for that many seconds, then it is evicted. If the taint is removed before the time is up, nothing happens.
  3. A matching toleration without tolerationSeconds: the Pod stays for good.
tolerations:
  - key: maintenance
    operator: Exists
    effect: NoExecute
    tolerationSeconds: 60      # only allowed with effect NoExecute

"Evicted" here means the Pod is deleted through the API. A Pod owned by a Deployment, ReplicaSet or StatefulSet is replaced by its controller, and the replacement is scheduled on another node. A bare Pod is simply gone.

How a toleration matches a taint

A toleration matches a taint when the key, the effect and (depending on the operator) the value agree.

TolerationMatches
key: dedicated, operator: Equal, value: gpu, effect: NoScheduleExactly dedicated=gpu:NoSchedule
key: dedicated, operator: Exists, effect: NoSchedulededicated with any value, effect NoSchedule
key: dedicated, operator: Exists (no effect)dedicated with any value and any effect
operator: Exists (no key, no effect)Every taint. The Pod can go anywhere

The rules behind the table:

  • operator defaults to Equal. With Equal, a missing value only matches a taint with an empty value.
  • With Exists you must not set a value; the API server rejects the Pod if you do.
  • An empty effect matches all effects. An empty key is only allowed with Exists, and matches all keys.
  • Everything is case sensitive, effects included: NoSchedule, not noschedule.

A node can carry several taints, and a Pod several tolerations. Kubernetes works it out like a filter: take all the node's taints, cross out each one the Pod tolerates, and apply whatever is left. One untolerated taint is enough to block the Pod, so a Pod needs a toleration for every NoSchedule and NoExecute taint on the node.

Under the hood · Where the checks actually happen

Taints are checked in more than one place, which explains some surprising behaviour:

  • The scheduler runs the TaintToleration plugin. As a filter it rejects nodes with an untolerated NoSchedule or NoExecute taint. As a scoring rule it gives fewer points to nodes with untolerated PreferNoSchedule taints.
  • A controller in kube-controller-manager watches for NoExecute taints and deletes the Pods that do not tolerate them, waiting out tolerationSeconds where it is set.
  • The kubelet refuses to start a Pod that does not tolerate a NoExecute taint on its node.

Nobody re-checks NoSchedule after scheduling. So a Pod that skips the scheduler, because it sets spec.nodeName or because it is a static Pod, lands on a NoSchedule node without any toleration. NoExecute still catches a Pod placed with nodeName.

Newer Kubernetes versions also define the operators Gt and Lt, which compare a numeric taint value with the toleration's value. They sit behind a feature gate and you are unlikely to need them; Equal and Exists are the two to know.

Taints Kubernetes adds by itself

You will meet taints you never set. All of them use the same mechanism, which is why they are worth recognising:

TaintEffectAdded when
node-role.kubernetes.io/control-planeNoSchedulekubeadm sets up a control plane node
node.kubernetes.io/not-readyNoSchedule and NoExecuteThe node's Ready condition is False (kubelet reports trouble)
node.kubernetes.io/unreachableNoSchedule and NoExecuteThe node's Ready condition is Unknown (kubelet stopped reporting)
node.kubernetes.io/unschedulableNoScheduleThe node is cordoned (kubectl cordon, kubectl drain)
node.kubernetes.io/memory-pressure, disk-pressure, pid-pressureNoScheduleThe kubelet reports that the node is short of that resource
node.kubernetes.io/network-unavailableNoScheduleThe node's network is not set up yet

Three things follow from this table.

Why your Pods avoid the control plane. It is not a special rule in the scheduler. It is an ordinary taint, and the control plane's own Pods carry tolerations for it. Remove the taint and the node takes normal workloads, which is how a single-node practice cluster works:

kubectl taint nodes controlplane node-role.kubernetes.io/control-plane:NoSchedule-

Why a dead node keeps its Pods for five minutes. The API server adds two tolerations to every Pod that does not set them itself:

tolerations:
  - key: node.kubernetes.io/not-ready
    operator: Exists
    effect: NoExecute
    tolerationSeconds: 300
  - key: node.kubernetes.io/unreachable
    operator: Exists
    effect: NoExecute
    tolerationSeconds: 300

You can see them on any Pod with kubectl get pod <name> -o yaml. When a node goes down it gets the NoExecute taint, and the Pods on it are evicted 300 seconds later. Set your own toleration for these keys with a smaller tolerationSeconds if a workload should fail over faster.

Why DaemonSet Pods run on nodes that are in trouble. The DaemonSet controller adds tolerations to its Pods for not-ready and unreachable (NoExecute, with no time limit) and for unschedulable and the pressure taints (NoSchedule). That is why a log collector or the network plugin keeps running on a cordoned node. It does not add a toleration for the control plane taint or for taints you invent: a DaemonSet that should run on every node needs those tolerations in its own Pod template.

Using it

Example · Taint a node, check it, remove it

The whole command set is one verb with three forms:

kubectl taint nodes node-2 dedicated=gpu:NoSchedule      # add
kubectl describe node node-2 | grep -A 2 Taints          # check
kubectl taint nodes node-2 dedicated=gpu:NoSchedule-     # remove this one taint (note the trailing minus)
kubectl taint nodes node-2 dedicated-                    # remove every taint with the key "dedicated"
Taints:             dedicated=gpu:NoSchedule

A taint is identified by its key and effect, so one node can carry dedicated=gpu:NoSchedule and dedicated=gpu:NoExecute together. Adding a taint whose key and effect already exist with another value fails unless you add --overwrite. Taints without a value drop the equals sign: kubectl taint nodes node-2 maintenance:NoSchedule.

Example · Let one Pod onto a tainted node

node-2 is tainted dedicated=gpu:NoSchedule, and the Pod train must be able to run there. kubectl run has no flag for tolerations, so generate the manifest and add the block:

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:
  containers:
    - name: train
      image: busybox:1.37
      command: ["sleep", "3600"]
  tolerations:
    - key: dedicated
      operator: Equal
      value: gpu
      effect: NoSchedule
kubectl apply -f train.yaml
kubectl get pod train -o wide              # which node did it get?

tolerations sits directly under spec, at the same level as containers, not inside a container. If the task says the Pod must run on node-2, the toleration alone is not enough; add nodeSelector with a label of that node (kubernetes.io/hostname: node-2 always exists).

For a Deployment the block goes into the Pod template, under spec.template.spec.tolerations. Changing it there starts a rollout, so the new Pods carry the toleration.

Example · Find out why a Pod is Pending because of a taint

kubectl describe pod train | tail -n 4
Warning  FailedScheduling  default-scheduler  0/3 nodes are available: 1 node(s) had untolerated taint
{node-role.kubernetes.io/control-plane: }, 2 node(s) had untolerated taint {dedicated: gpu}.

The message lists each untolerated taint as {key: value} with the number of nodes that have it. Here every node is closed: one by the control plane taint and two by dedicated=gpu. Compare with what the Pod tolerates:

kubectl get pod train -o jsonpath='{.spec.tolerations}'
kubectl describe node node-1 | grep -A 2 Taints

Typical causes are a value that does not match (GPU against gpu), the wrong effect, operator: Exists combined with a value (rejected at creation), or a second taint on the node that the Pod does not tolerate. Then choose the fix the task asks for: add or correct the toleration on the Pod, or remove the taint from the node. A Pending Pod is scheduled by itself a few seconds after the taint is gone.

Example · Empty a node with NoExecute

You want every ordinary Pod off node-1 now, and nothing new on it:

kubectl get pods -o wide --field-selector spec.nodeName=node-1    # what runs there now
kubectl taint nodes node-1 maintenance=true:NoExecute
kubectl get pods -o wide                                          # replacements appear on other nodes

Pods without a matching toleration are deleted at once, and their controllers recreate them elsewhere. DaemonSet Pods are deleted too unless their template tolerates your taint. When the work is done:

kubectl taint nodes node-1 maintenance=true:NoExecute-

Removing the taint does not bring the Pods back; the scheduler never moves running Pods. For planned maintenance kubectl drain is usually the better tool, because it respects PodDisruptionBudgets and graceful shutdown. A NoExecute taint is the blunt version.

Tricks

Common mistakes

  • Expecting a toleration to place the Pod. It only allows. Without a node selector or affinity the Pod can land on any other node that fits.
  • Expecting NoSchedule to clear a node. Running Pods stay. Only NoExecute (or a drain) removes them.
  • Tolerating one taint out of two. Every NoSchedule and NoExecute taint on the node needs a matching toleration. Read the whole Taints: list; it can span several lines.
  • Mismatched effect. A toleration with effect: NoSchedule does not match a NoExecute taint with the same key and value. Leave effect out if the Pod should tolerate the key with any effect.
  • operator: Exists with a value. The Pod is rejected. Use Equal with a value, or Exists without.
  • Putting tolerations in the wrong place. It belongs under the Pod's spec (for a Deployment: spec.template.spec), not under a container and not under the Deployment's own spec.
  • Using nodeName and expecting the taint to hold. nodeName skips the scheduler, so NoSchedule and PreferNoSchedule are never checked.
  • Trying to remove a toleration from a running Pod. You can add tolerations to a live Pod, but not change or remove them. Recreate the Pod, or edit the template of the controller that owns it.

Recap

  • A taint is key=value:effect on a node; a toleration on a Pod matches it by key, value (or Exists) and effect.
  • PreferNoSchedule is a hint, NoSchedule blocks new Pods, NoExecute blocks new Pods and evicts running ones, optionally after tolerationSeconds.
  • Tolerations permit; they do not attract. Pair a taint with a node selector or affinity to dedicate a node.
  • The control plane taint, cordons and the not-ready and unreachable taints are all the same mechanism; every Pod tolerates the last two for 300 seconds.
  • kubectl taint nodes <node> key=value:effect adds, the same with a trailing - removes, and kubectl describe node shows what is set.

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.

  • Keep ordinary pods off an expensive node with a taint, without throwing out what already runs there, and let the one workload that needs the node back in.

    Build · Level 2 · standard · target 15 min

  • Find out why a workload lost its node to a NoExecute taint, reopen the node, and give another workload a limited time to stay on a node that gets the same taint tonight.

    Fix · Level 2 · standard · target 15 min

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.