Scheduling constraints
Use labels, affinity, and taints to control which nodes a workload can run on.
What you will understand
- Predict which nodes survive the scheduler’s required placement rules.
- Use Pod affinity and anti-affinity to express relationships between workloads.
- Pair a label requirement with a toleration on a dedicated worker.
Builds on: Requests, limits, and QoS
Which nodes can run this Pod
A cluster shares workers between ordinary web apps and maintenance tools. You want the tools to run on one labeled worker and ordinary apps to stay off it. Kubernetes has a way to express the Pod’s requirements and a separate way for the node to repel workloads.
A node selector requires matching node labels. Node affinity also evaluates node labels and can express alternatives and preferences. Required rules remove nodes from consideration. Preferred rules influence the scheduler’s scoring of eligible nodes. In the scene, find which nodes are still eligible before the scheduler compares preference scores.
Pod affinity evaluates where other Pods with selected labels run. For example, a worker could prefer the same zone as a cache it calls frequently. Pod anti-affinity can keep matching web replicas apart by hostname so one node failure affects fewer replicas. The topology key defines what “together” or “apart” means. A hard one-per-node requirement for three replicas needs at least three eligible nodes.
Open Inspect the placement to compare three workers. Watch one fail a required label check, then see how the cache’s location breaks the tie between the two eligible workers.
How does the scheduler narrow the node list?
The scheduler filters out nodes that fail hard requirements, scores feasible nodes, then records the chosen node.
- Pod templateRead required node and Pod affinitySchedulerRequired labels and topology define where the Pod can fit.
- Filter pluginsKeep eligible nodesCandidate listEvery remaining node satisfies the hard checks being modeled.
- Score pluginsRank feasible candidatesChosen nodePreferences affect the score after filtering.
- SchedulerBind the PodAPI serverThe selected node is recorded for its kubelet.
The scheduler filters out nodes that fail hard requirements, scores feasible nodes, then records the chosen node.
Two labeled worker nodes and a cache Pod; real scoring combines multiple plugins.
Inspect the placement
Add a third worker to the lesson’s example. The client Pod requires workload=tools and prefers the zone containing a cache Pod.
From API serverTo Scheduler
Observe the unassigned Pod
The scheduler observes a new client Pod without a node assignment. It reads the required worker label and the preferred affinity to Pods labeled app=cache.
- Before
- Stored without a node
- After
- Queued for placement
The scheduler observes a new client Pod without a node assignment. It reads the required worker label and the preferred affinity to Pods labeled app=cache.
This snapshot gives the two eligible nodes equal scores from other plugins so the affinity preference decides. The scheduler evaluates its cached view of Pods and Nodes.
Reveal prediction answer
Worker-b would still fail the workload=tools requirement. Only workers that pass the filters can be ranked.
Compare the lab’s unschedulable Pod with the eligible Pod. Read the FailedScheduling event and nodeSelector, then check the assigned node on the eligible Pod.
Verify a hard constraint in the placement labInspect YAML or commands
Read node labels
nodeSelector:
workload: toolsExplore another case
Reserve one worker for maintenance tools
A taint is a node setting that repels Pods without a matching toleration. NoSchedule blocks new placements that do not tolerate it. It leaves already-running Pods in place. PreferNoSchedule is a soft preference. NoExecute can also evict existing Pods without a matching toleration. A toleration gives a Pod permission to run on a tainted node when other rules allow it.
Save this manifest as tools-client.yaml. The Pod needs a node labeled workload=tools and tolerates dedicated=tools:NoSchedule. Put both settings on the same disposable worker. The label requirement guides the tools there, and the taint protects the worker from ordinary new Pods. Try each setting on its own in the scene.
apiVersion: v1
kind: Pod
metadata:
name: tools-client
labels:
app: tools-client
spec:
nodeSelector:
workload: tools # Requires this label on the chosen node.
tolerations:
- key: dedicated
operator: Equal
value: tools
effect: NoSchedule # Allows this taint; does not select the node.
containers:
- name: client
image: busybox:1.36
command: ["sleep", "3600"]
resources:
requests:
cpu: 25m
memory: 16MiWhy would a tools Pod run on the dedicated node?
A toleration permits placement on the tainted node. A selector makes the Pod choose the labeled worker pool.
Ordinary Pod is repelled
- Ordinary PodTools nodeuntolerated
The dedicated NoSchedule taint rejects a new ordinary Pod without the matching toleration.
Tools Pod tolerates the taint
- Tools PodTools nodetaint tolerated
- Tools PodGeneral nodealso eligible
A matching toleration lets the tools Pod pass this check. It says nothing about which feasible node wins.
Choose the worker pool with a selector
- Tools PodTools nodeselector + toleration
- Tools PodGeneral nodeselector mismatch
Adding workload=tools as a required selector restricts the tools Pod to the labeled pool.
Existing Pods under NoSchedule
- Running PodTools noderemains assigned
NoSchedule does not remove the already running Pod. It restricts future scheduler placements on this node.
A single training taint; other scheduler filters are assumed satisfied.
Explore another case
Read the scheduling event
Before applying, confirm the context points to the lab and choose a worker name from the node listing. After scheduling, read the NODE column in kubectl get pod -o wide. If it stays Pending, inspect the FailedScheduling event. It can point to an unmatched selector, an untolerated taint, or insufficient resources. Delete the Pod and remove the training settings after the experiment.
# Substitute a disposable lab worker name obtained from the first command.
kubectl get nodes --show-labels
kubectl label node LAB_WORKER workload=tools
kubectl taint node LAB_WORKER dedicated=tools:NoSchedule
kubectl apply -f tools-client.yaml
kubectl get pod tools-client -o wide
kubectl describe pod tools-client
# Undo only the training changes after deleting the Pod.
kubectl delete pod tools-client
kubectl taint node LAB_WORKER dedicated=tools:NoSchedule-
kubectl label node LAB_WORKER workload-The placement lab uses constrained capacity and labels to help you see why a Pod remains pending.
Check all the hard requirements together
Required constraints apply together. A labeled node can be rejected for a taint or for lack of CPU. A toleration can leave several nodes eligible, and adding a preference still leaves their final scores to the scheduler. Read the event summary and check the actual labels and taints on each candidate before relaxing constraints.
Check your understanding
A tools Pod tolerates the dedicated worker’s taint. An ordinary worker also has room. Can the Pod land on the ordinary worker?
Yes, if the Pod has no other requirement that rules it out. Add a node selector or required affinity matching the dedicated worker’s label to make that placement mandatory.
Primary documentation
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise