Sub topic 3
The Three Taint Effects
This chapter assumes you know what pods, nodes, and the scheduler are. It also assumes you can read a simple YAML manifest and run kubectl get, describe, and apply. Here you will learn what each taint effect does, so you can pick the right one for a node.
Where the effect fits
A taint has three parts: a key, an optional value, and an effect. It is written key=value:effect. The key and value name the reason for the taint. The effect decides what Kubernetes does to pods that do not tolerate it.
There are three effects: NoSchedule, PreferNoSchedule, and NoExecute. They differ in how strict they are and in whether they affect pods that are already running.
| Effect | New pods without a toleration | Pods already running without a toleration |
|---|---|---|
| `NoSchedule` | Not scheduled on the node | Keep running |
| `PreferNoSchedule` | Avoided if possible, but may be scheduled | Keep running |
| `NoExecute` | Not scheduled on the node | Evicted (stopped and removed) |
NoSchedule
NoSchedule is a hard rule for scheduling. The scheduler will not place a pod on the node unless the pod has a matching toleration.
The rule applies only when a pod is being scheduled. If you add the taint to a node that already runs pods, those pods are not touched. This makes NoSchedule the safe choice for reserving a node without disrupting its current workload.
kubectl taint nodes node1 dedicated=gpu:NoSchedulePreferNoSchedule
PreferNoSchedule is the soft version of NoSchedule. The scheduler tries to avoid the node for pods that do not tolerate the taint. If no other node can run the pod, the scheduler may still use this one.
Use it when you want to steer ordinary pods away from a node but not leave them Pending when the cluster is full. It gives no guarantee, so do not use it to protect a node.
kubectl taint nodes node1 dedicated=gpu:PreferNoScheduleNoExecute
NoExecute is the strictest effect. It does two things:
- It blocks new pods that lack a toleration, as
NoScheduledoes. - It evicts pods already running on the node that lack a toleration.
Eviction means the pod is deleted from the node. If a controller such as a Deployment owns the pod, the controller creates a replacement, and the scheduler places it on another node. A bare pod with no controller is not recreated.
A pod can tolerate a NoExecute taint and set tolerationSeconds. The pod then stays on the node for that many seconds after the taint appears, and is evicted afterward. A later chapter covers this in detail.
kubectl taint nodes node1 maintenance=true:NoExecuteHow the effects behave over time
The diagram shows what happens to a pod with no toleration when each taint is added to a node that is already running it.
Common mistakes
- Expecting NoSchedule to remove pods. It does not. Pods that were already running stay until something else removes them. Use
NoExecuteif you need them gone. - Relying on PreferNoSchedule for isolation. The scheduler can ignore it when it runs out of options.
- Using NoExecute without checking what runs on the node. Every pod without a matching toleration is evicted, including pods you did not mean to move. Check with
kubectl get pods -A -o wide --field-selector spec.nodeName=node1first. - Mixing up the same key with different effects. A node can hold
dedicated=gpu:NoScheduleanddedicated=gpu:NoExecuteat the same time. They are separate taints, and a pod needs a toleration for each one.
Choosing an effect
Ask what you want to happen to pods that are not allowed on the node.
- Keep new pods off, leave current ones alone:
NoSchedule. - Discourage new pods, but allow them if there is no room elsewhere:
PreferNoSchedule. - Keep new pods off and remove current ones:
NoExecute.
Exercise
A node runs three Deployment pods with no tolerations. You add the taint team=ml:NoSchedule to the node. What happens to the three pods, and what happens to a new pod without a toleration?
Show answer
The three running pods keep running, because NoSchedule affects only scheduling decisions. The new pod will not be scheduled on that node. To evict the running pods, use NoExecute instead.
Exercise
You want ordinary pods to avoid node2 when other nodes have room, but still run there if the cluster is full. Which effect do you choose, and why?
Show answer
PreferNoSchedule. It is a soft preference, so the scheduler avoids the node when it can and uses it when no other node fits.