Sub topic 4
Adding and Removing Taints with kubectl
This chapter assumes you know what a node, a pod, and a namespace are. It also assumes you can run kubectl get, describe, and apply, and that you have a rough idea of what the scheduler does. You should already know that a taint has a key, an optional value, and an effect. The earlier chapters on taints and taint effects cover these.
The kubectl taint command
You manage taints with kubectl taint. The command takes a resource type, a node name, and a taint specification in key=value:effect form. The value is optional, so key:effect is also valid.
# Add a taint: key=dedicated, value=gpu, effect=NoSchedule
kubectl taint nodes node1 dedicated=gpu:NoSchedule
# Show the taints on a node
kubectl describe node node1 | grep Taints
# Remove the taint (note the trailing minus)
kubectl taint nodes node1 dedicated=gpu:NoSchedule-The first command adds the taint. The second reads it back. The third removes it. A trailing minus sign on the same specification means "delete this taint".
What the command changes
A taint is stored in the node's spec.taints list. The command edits that list. It does not restart the node or touch pods directly. The scheduler reads the list the next time it considers a pod for that node.
The effect you choose decides what happens to pods that already run on the node:
NoScheduleandPreferNoScheduleaffect only new scheduling decisions. Pods already running stay put.NoExecutealso evicts running pods that do not tolerate the taint.
Use NoExecute with care on a node that is busy. Pods without a matching toleration are evicted as soon as the taint is applied.
Inspecting taints
kubectl describe node shows a Taints: line, or <none> if the node has none. To see all nodes at once, use custom columns:
kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taintsThe TAINTS column prints the raw list of taint objects. A node without taints shows <none>.
Rules for adding and removing
Several details cause errors in practice.
- Adding an identical taint fails. If the node already has a taint with the same key and effect, kubectl reports that the taint already exists. Use
--overwriteto change the value of an existing key and effect pair. - Key and effect together identify a taint. A node can hold
dedicated=gpu:NoScheduleanddedicated=gpu:NoExecuteat the same time. They are separate entries. - Removal must match.
kubectl taint nodes node1 dedicated:NoSchedule-removes the taint with that key and effect, whatever its value. If you omit the effect (dedicated-), kubectl removes every taint with that key. - Removing a missing taint fails. kubectl reports that the node has no such taint.
The table below summarizes the common forms.
| Goal | Command |
|---|---|
| Add a taint with a value | `kubectl taint nodes node1 dedicated=gpu:NoSchedule` |
| Add a taint without a value | `kubectl taint nodes node1 maintenance:NoSchedule` |
| Change a value | `kubectl taint nodes node1 dedicated=cpu:NoSchedule --overwrite` |
| Remove one key and effect | `kubectl taint nodes node1 dedicated:NoSchedule-` |
| Remove all effects for a key | `kubectl taint nodes node1 dedicated-` |
| Taint every node with a label | `kubectl taint nodes -l pool=batch dedicated=batch:NoSchedule` |
Pairing a taint with a label
A taint only repels pods. It does not attract any. For the dedicated-node pattern, which a later chapter covers in full, you normally label and taint the same node:
# Label and taint the same node
kubectl label nodes node1 dedicated=gpu
kubectl taint nodes node1 dedicated=gpu:NoScheduleThe label lets pods select the node. The taint keeps other pods off it.
Common mistakes
- Forgetting the effect.
kubectl taint nodes node1 dedicated=gpuis rejected. The effect is required. - Typing the effect in the wrong case. Effects are case sensitive. Use
NoSchedule,PreferNoSchedule, orNoExecute. - Leaving out the minus on removal. Without it, kubectl tries to add the taint again and fails with an "already exists" message.
- Expecting a
NoScheduletaint to move pods. It never evicts. OnlyNoExecutedoes.
Exercise
Node node2 has the taints team=a:NoSchedule and team=a:NoExecute. You run kubectl taint nodes node2 team:NoSchedule-. Which taints remain, and why?
Show answer
Only team=a:NoExecute remains. A taint is identified by its key and effect, so team:NoSchedule- removes the entry with key team and effect NoSchedule, whatever its value, and leaves the NoExecute entry alone. To remove both, run kubectl taint nodes node2 team-.
Exercise
You taint a node with kubectl taint nodes node1 dedicated=gpu:NoSchedule. Three pods without tolerations are already running on node1. What happens to them?
Show answer
Nothing. NoSchedule affects only new scheduling decisions, so the running pods stay on node1. They would be evicted only if the taint used the NoExecute effect.