Sub topic 10
NoExecute, eviction, and tolerationSeconds
This chapter assumes you know how to add taints with kubectl taint, how a toleration matches a taint, and that NoSchedule only affects pods that are not yet scheduled. Here you will see how NoExecute also affects pods that are already running.
What NoExecute adds
NoSchedule and PreferNoSchedule are checked only when the scheduler places a pod. A pod already running on the node is left alone. NoExecute is stricter. It does two things:
- The scheduler will not place new pods that lack a matching toleration.
- Pods already running on the node that lack a matching toleration are evicted.
Eviction is carried out by a controller in kube-controller-manager. When a NoExecute taint appears on a node, the controller checks every pod on that node and deletes the ones that do not tolerate it. A pod owned by a Deployment, StatefulSet, or similar controller is then recreated and scheduled on another node. A pod with no owner is simply gone.
You can try it on a node that is running pods:
# Evict every pod on node1 that does not tolerate this taint
kubectl taint nodes node1 maintenance=true:NoExecute
# Watch the pods leave the node
kubectl get pods -o wide --watch
# Allow pods back
kubectl taint nodes node1 maintenance=true:NoExecute-Three outcomes for a running pod
What happens to a running pod depends on how its tolerations match the NoExecute taint.
| Pod's tolerations | Result |
|---|---|
| No matching toleration | Evicted immediately |
| Matching toleration without `tolerationSeconds` | Stays bound indefinitely |
| Matching toleration with `tolerationSeconds: N` | Stays for N seconds after the taint is added, then is evicted |
Delaying eviction with tolerationSeconds
The field tolerationSeconds gives a pod a grace period. The pod tolerates the taint for that many seconds, and then the controller evicts it. This is useful when a problem may be temporary and you do not want pods to move at the first sign of trouble.
tolerations:
- key: "node.kubernetes.io/unreachable"
operator: "Exists"
effect: "NoExecute"
tolerationSeconds: 60With this toleration, a pod on a node that becomes unreachable stays bound for 60 seconds. If the node recovers and the taint is removed within that time, the pod is never evicted. If the taint is still there after 60 seconds, the pod is evicted.
Rules to remember
tolerationSecondsonly has an effect with theNoExecuteeffect. It is not meaningful forNoScheduleorPreferNoSchedule.- The timer starts when the taint is added to the node. It does not restart if the pod is later updated.
- Omitting
tolerationSecondsmeans the pod tolerates the taint forever. This is the most common source of pods that "refuse to leave" a node. - If several
NoExecutetaints match, the pod is evicted after the shortest applicable grace period, and a taint with no matching toleration evicts it immediately.
Common errors
- Expecting NoSchedule to remove pods. Adding a
NoScheduletaint to a busy node does not move anything. UseNoExecuteif you need the node cleared. - Forgetting that unowned pods are not recreated. Evicting a bare pod deletes it for good.
- Setting
tolerationSecondson the wrong effect. The field is ignored in practice for effects other thanNoExecute, so the pod does not get the delay you expected.
The next chapters cover the NoExecute taints that Kubernetes adds on its own, including the default grace period that pods receive for them.
Exercise
A pod has this toleration: key "maintenance", operator "Equal", value "true", effect "NoExecute", tolerationSeconds 120. You run kubectl taint nodes node1 maintenance=true:NoExecute while the pod is running on node1. You remove the taint after 90 seconds. What happens to the pod, and what would change if you had removed it after 150 seconds?
Show answer
After 90 seconds the taint is gone before the 120-second grace period ends, so the pod is never evicted and keeps running. If the taint were still present at 150 seconds, the pod would have been evicted at 120 seconds, and removing the taint later would not bring it back.