Sub topic 8
Dedicating nodes to a workload
This chapter assumes you can write pod manifests, use kubectl to label and describe nodes, and know that a toleration lets a pod ignore a matching taint. It combines taints, tolerations, and node affinity so that a group of nodes serves only one workload.
The problem with taints alone
A taint keeps unwanted pods off a node. A toleration lets a pod onto a tainted node, but it does not send the pod there. The scheduler can still place a tolerating pod on any other node that has room.
To dedicate nodes, you need both effects:
- Keep other pods off the node. A taint does this.
- Keep your pods on the node. Node affinity does this.
Step 1: Label and taint the node
Apply a label and a taint with the same key and value to the node. The label is for pods to select. The taint keeps everything else away.
# Label and taint the same node
kubectl label nodes node1 dedicated=gpu
kubectl taint nodes node1 dedicated=gpu:NoScheduleUsing the same dedicated=gpu pair for both is a convention. It makes the intent easy to read. Kubernetes does not link a label to a taint.
Step 2: Add a toleration and node affinity to the pod
The pod below requires nodes labeled dedicated=gpu, and it tolerates the taint on those nodes.
apiVersion: v1
kind: Pod
metadata:
name: gpu-only-job
spec:
containers:
- name: app
image: nginx
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: dedicated
operator: In
values: ["gpu"]
tolerations:
- key: "dedicated"
operator: "Equal"
value: "gpu"
effect: "NoSchedule"The requiredDuringSchedulingIgnoredDuringExecution rule is a hard requirement at scheduling time. If no labeled node has room, the pod stays Pending. It does not fall back to another node. Once the pod is running, later label changes do not evict it.
How the pieces fit together
What each combination gives you
| Setup | Other pods on the node? | Your pods only on the node? |
|---|---|---|
| Taint only | No | No, your pods can go elsewhere |
| Label and affinity only | Yes, anything can still land there | Yes |
| Taint, toleration, and affinity | No | Yes |
Only the last row gives you a truly dedicated node.
Checking the result
Create the pod and see where it landed:
kubectl apply -f gpu-only-job.yaml
kubectl get pod gpu-only-job -o wideThe NODE column should show node1. To confirm that other pods are kept off, create a pod with no toleration. It should not be scheduled onto node1.
If your pod stays Pending, check the Events section of kubectl describe pod gpu-only-job. A message such as node(s) had untolerated taint means the toleration does not match. A message such as node(s) didn't match Pod's node affinity/selector means no node carries the label.
Common mistakes
- Taint and label do not match. A typo such as
dedicated=gpuson the node means the affinity finds no nodes. The pod stays Pending. - Forgetting the effect. A toleration with
effect: "NoSchedule"does not match aNoExecutetaint. - Expecting existing pods to move. A
NoScheduletaint does not evict pods already running on the node. OnlyNoExecutedoes that, so check what is already running before you rely on dedication. - Using only a toleration. The pod can run anywhere. Add affinity or a node selector.
Exercise
You taint node2 with team=data:NoSchedule and add a matching toleration to a pod, but you add no node affinity or selector. The cluster has other untainted nodes with free capacity. Is the pod guaranteed to run on node2? What would you add to guarantee it, and what keeps other teams' pods off node2?
Show answer
No. The toleration only permits the pod onto node2, so the scheduler may place it on any untainted node. Label node2 (for example team=data) and add a required node affinity or a nodeSelector for that label to the pod. The NoSchedule taint keeps other teams' pods off node2, because they lack the toleration.