Sub topic 2
Taints: marking a node
A taint is a mark on a node that tells the scheduler to keep pods away unless they opt in. This chapter covers what a taint contains, where Kubernetes stores it, and how to read the taints on a node.
This chapter assumes you know pods, nodes, and namespaces, can read simple YAML, and can use kubectl get, describe, and apply. It also assumes you know the scheduler picks a node for each new pod.
What a taint is
A taint is a property of a node. It has three parts:
- key: a name, such as
dedicated. - value: an optional string, such as
gpu. - effect: what the cluster does to pods that do not tolerate the taint.
You write a taint as key=value:effect, for example dedicated=gpu:NoSchedule. The value can be left out, which gives key:effect.
A taint is not a label. Labels describe a node so that pods can select it. A taint pushes pods away from a node. The two can use the same key, and the dedicated-node pattern later in this tutorial does exactly that. They are separate fields with separate jobs.
Where taints live
Taints are stored in the node object, under spec.taints. A node carrying one taint looks like this:
spec:
taints:
- key: dedicated
value: gpu
effect: NoScheduleThe list can hold several taints. Each is identified by its key and effect together, so dedicated=gpu:NoSchedule and dedicated=gpu:NoExecute can exist on the same node.
Because taints are part of the node's spec, people can set them and so can Kubernetes itself. Cluster administrators usually add taints by hand. The node lifecycle controller adds some on its own when a node becomes unhealthy. A later chapter covers those built-in taints.
The scheduling decision
The scheduler checks each pod against the taints on each candidate node. A node with no taints accepts any pod that fits its other constraints. A node with a taint accepts only pods that carry a matching toleration.
The diagram shows one pod with no toleration and one with a matching toleration, both considered for a tainted node.
"May be placed" matters here. A matching toleration only removes the objection. It does not choose the node. Chapters later in this tutorial cover that point in detail.
Reading taints on a node
Start by checking what a node already carries. Two commands cover most cases:
# Taints for one node, in the describe output
kubectl describe node node1 | grep Taints
# Taints for every node, side by side
kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taintsIn describe output the line looks like Taints: dedicated=gpu:NoSchedule. A node with none shows Taints: <none>. The custom-columns form prints the raw list, so a node with no taints shows <none> in that column.
On many clusters the control plane nodes already carry a taint. Run the second command on a fresh cluster and you will probably see it. This is why ordinary workloads do not land on control plane nodes by default.
Adding one taint
To see a taint take effect, add one with kubectl taint:
kubectl taint nodes node1 dedicated=gpu:NoScheduleFrom this point the scheduler will not place new pods without a matching toleration on node1. Pods already running there are not affected by this particular effect. The next chapter explains why, and how the three effects differ.
Common mistakes
- Confusing taints with labels. A taint does not make a pod prefer the node. It only repels pods that lack a toleration.
- Typing the effect wrong. The effect is case-sensitive.
NoScheduleis valid, andnoscheduleis rejected. - Forgetting the effect. A taint always needs one.
dedicated=gpualone is not a complete taint. - Assuming a taint is cluster-wide. It applies to one node. Tainting
node1leaves every other node unchanged.
Exercise
A node's spec contains the taint dedicated=gpu:NoSchedule. Name its key, value, and effect, and say whether it is a label.
Show answer
The key is dedicated, the value is gpu, and the effect is NoSchedule. It is not a label. It is a taint, stored under spec.taints, and it repels pods that lack a matching toleration instead of describing the node for selection.
Exercise
Which command lists the taints of every node in one table?
Show answer
kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taints