Workloads: Deployments, rollouts and jobs · 6 MIN READ
Scheduling: node selection, affinity, taints and tolerations, spreading and draining nodes
Explain how the scheduler filters and scores nodes, steer Pods with nodeSelector and affinity, keep them off nodes with taints, and read FailedScheduling messages after a cordon, a drain or a bad constraint.
Every new Pod starts without a node. The scheduler picks one, and if it cannot, the Pod waits in Pending with an event that explains why, node by node. Most scheduling problems are constraints you wrote yourself: a selector no node matches, a taint nobody tolerates, or requests that do not fit. Knowing the vocabulary turns that event into a one-line diagnosis.
kubernetes · A MAP OF THE IDEA
- Label and taint nodes
- Select or prefer with affinity
- Tolerate the taint
- Spread across zones and nodes
Useful when
- Placement rules live in manifests, not in runbooks.
- FailedScheduling explains why every node was rejected.
Watch out
- A hard constraint with one matching node fails on drain.
- Tolerations without selectors do not dedicate nodes.
Implementation & operation cost: A labelling and tainting convention for nodes, kept consistent with the constraints written into every workload.
Filter, score, bind
kube-scheduler watches for Pods without spec.nodeName. For each, filter plugins remove nodes that cannot run it: not enough allocatable CPU or memory for its requests, a node selector or required affinity that does not match, an untolerated taint, a host port in use, a volume bound elsewhere, or a hard topology spread constraint. Score plugins then rank the remaining feasible nodes, preferring balanced resource use, preferred affinities, images already present and even spreading, and the Pod is bound to the best node. If no node passes the filters, the Pod stays Pending, a FailedScheduling event records the reasons, and the scheduler retries whenever the cluster changes; with Pod priorities it may also preempt lower-priority Pods to make room.
Attracting Pods: nodeSelector and node affinity
nodeSelector is the simple form: every listed label must be present on the node with exactly that value. Node affinity is the expressive form. requiredDuringSchedulingIgnoredDuringExecution is a hard filter with operators such as In, NotIn, Exists and Gt; preferredDuringSchedulingIgnoredDuringExecution adds weighted preferences to the score. IgnoredDuringExecution means a running Pod stays put if the node's labels change later. Prefer well-known labels such as kubernetes.io/arch, topology.kubernetes.io/zone and node.kubernetes.io/instance-type where they exist. In the GopherForge lab terminal, nodeSelector, taints and tolerations are enforced; affinity and topologySpreadConstraints are accepted but its scheduler does not use them.
spec:
nodeSelector:
kubernetes.io/arch: arm64
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disk
operator: In
values: ["ssd", "nvme"]
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 50
preference:
matchExpressions:
- key: topology.kubernetes.io/zone
operator: In
values: ["zone-a"]Both hard conditions must hold: the node runs arm64 and has disk set to ssd or nvme. Among the nodes that qualify, those in zone-a get extra score, but the Pod still runs elsewhere when zone-a is full. Several nodeSelectorTerms would be ORed, while the expressions inside one term are ANDed.
Repelling Pods: taints and tolerations
A taint on a node repels every Pod that does not tolerate it. NoSchedule blocks new Pods, PreferNoSchedule only lowers the score, and NoExecute also evicts running Pods that do not tolerate it, optionally after tolerationSeconds. A toleration permits scheduling onto a tainted node; it does not attract the Pod there. To dedicate nodes to a workload, combine both: a taint keeps everyone else off, and a selector or affinity keeps the workload on. Multi-node kind clusters taint the control plane with node-role.kubernetes.io/control-plane:NoSchedule, and when a node fails the node lifecycle controller adds not-ready or unreachable NoExecute taints, which Pods tolerate for 300 seconds by default before they are evicted.
kubectl taint nodes kind-worker2 dedicated=batch:NoSchedule
kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taints[*].key
kubectl taint nodes kind-worker2 dedicated=batch:NoSchedule-A taint is written key=value:effect. The custom-columns query lists the taint keys of every node, which is quicker than describing each one. A trailing minus removes the taint again, and kubectl taint nodes kind-worker2 dedicated- would remove every taint with that key.
Spreading replicas across failure domains
Three replicas on one node survive nothing that takes that node down. topologySpreadConstraints state the intent directly: with topologyKey: topology.kubernetes.io/zone and maxSkew: 1, the number of matching Pods in any two zones may differ by at most one. whenUnsatisfiable: DoNotSchedule makes the rule a filter, ScheduleAnyway a scoring preference. The scheduler also applies cluster-wide default constraints that softly spread Pods of one workload across nodes and zones, which is why a Deployment usually lands on several nodes without configuration. Pod anti-affinity can express never two on one node as well, but costs more to evaluate in large clusters. Pair spreading with a PodDisruptionBudget so that voluntary disruptions cannot take all replicas at once. The lab terminal accepts these constraints but does not enforce them.
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: web
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: webThe first constraint is hard: zones may differ by at most one web Pod, so a new replica goes to the emptier zone or waits. The second is soft: within that rule, the scheduler prefers nodes running fewer web Pods. labelSelector decides which Pods are counted, normally the workload's own.
Cordon, drain and FailedScheduling messages
kubectl cordon marks a node unschedulable while its Pods keep running. kubectl drain cordons and then evicts Pods through the Eviction API, which honours PodDisruptionBudgets, so the node can be patched or removed; --ignore-daemonsets is needed because the DaemonSet controller would recreate its Pods there anyway. Controllers create replacements elsewhere, and if their constraints allow no other node, the replacements stay Pending. uncordon reopens the node. A FailedScheduling message counts nodes per reason, for example 0/3 nodes are available: 1 node(s) didn't match Pod's node affinity/selector, 1 node(s) had untolerated taint {dedicated: batch}, 1 node(s) had untolerated taint {node-role.kubernetes.io/control-plane: }. Each phrase names one filter, and together they account for every node.
Try it in the lab terminal
The cluster has a tainted control-plane node and two workers. You label kind-worker with disk=ssd and pin the fast Deployment there with a nodeSelector, taint kind-worker2 for batch work, watch a Deployment without a toleration fail to schedule, and add the toleration with a patch. Finally you drain kind-worker to see what a nodeSelector does when its only matching node goes away.
cluster create --workers 2
kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taints[*].key
kubectl label node kind-worker disk=ssd
cat > fast.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: fast
spec:
replicas: 3
selector:
matchLabels:
app: fast
template:
metadata:
labels:
app: fast
spec:
nodeSelector:
disk: ssd
containers:
- name: web
image: gopherforge/web:v1
EOF
kubectl apply -f fast.yaml
kubectl rollout status deploy/fast
kubectl get pods -o wide
kubectl taint nodes kind-worker2 dedicated=batch:NoSchedule
cat > batch.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: batch
spec:
replicas: 2
selector:
matchLabels:
app: batch
template:
metadata:
labels:
app: batch
spec:
nodeSelector:
kubernetes.io/hostname: kind-worker2
containers:
- name: worker
image: gopherforge/web:v1
EOF
kubectl apply -f batch.yaml
kubectl events --types=Warning
kubectl patch deploy batch --patch '{"spec":{"template":{"spec":{"tolerations":[{"key":"dedicated","operator":"Equal","value":"batch","effect":"NoSchedule"}]}}}}'
kubectl rollout status deploy/batch
kubectl drain kind-worker --ignore-daemonsets
kubectl get pods -o wide
kubectl uncordon kind-workerThe TAINTS column shows node-role.kubernetes.io/control-plane on kind-control-plane only. All three fast Pods land on kind-worker. The batch Pods stay Pending, and events shows FailedScheduling: 1 node(s) didn't match Pod's node affinity/selector, 1 node(s) had untolerated taint {dedicated: batch}, plus the control-plane taint. After the patch the rollout finishes on kind-worker2. drain prints node/kind-worker cordoned, warns about ignored DaemonSet Pods and evicts the fast Pods; their replacements stay Pending with NODE <none>, since no other node has disk=ssd, until uncordon lets the scheduler place them again.
TAKE THIS WITH YOU
The scheduler filters nodes on requests, selectors, affinity, taints and spread constraints, then scores the rest. Selectors and affinity attract, taints repel and tolerations only permit, and a FailedScheduling message tells you which rule excluded each node.
Check your understanding
You taint three GPU nodes dedicated=gpu:NoSchedule and give your training Pods a matching toleration, yet some still land on ordinary nodes. Why?
Go to the source
Progress stays in this browser. No account needed.