Sub topic 7
The scheduler: choosing a node for each Pod
This chapter assumes you know what a Pod is, can read basic YAML, and have seen the control plane components named in the architecture overview. It covers how kube-scheduler picks a node for each new Pod and what to check when a Pod stays Pending.
What the scheduler does
The scheduler is a control plane component. It watches the API server for Pods that have no node assigned. For each one it chooses a node and writes that choice back to the API server by setting the Pod's spec.nodeName. It does not start containers. The kubelet on the chosen node sees the assignment and does that work.
Filtering and scoring
Scheduling runs in two main phases.
- Filtering removes nodes that cannot run the Pod. Reasons include too little free CPU or memory for the Pod's resource requests, a
nodeSelectoror affinity rule that does not match, or a taint the Pod does not tolerate. - Scoring ranks the remaining nodes. Plugins prefer things such as spreading replicas across nodes or nodes that already hold the container image. The highest score wins, and ties are broken randomly.
If filtering leaves no node, the Pod stays Pending and the scheduler retries as the cluster changes.
Requests drive placement
The scheduler decides using resource requests, not live usage. A node is full when the sum of requests on it reaches its allocatable capacity, even if the containers are idle.
apiVersion: v1
kind: Pod
metadata:
name: api
spec:
nodeSelector:
disktype: ssd
containers:
- name: api
image: nginx:1.27
resources:
requests:
cpu: 250m
memory: 128MiThis Pod can only land on a node labeled disktype=ssd that has at least 250 millicores of CPU and 128 MiB of memory unrequested.
Influencing placement
| Mechanism | Purpose |
|---|---|
| `nodeSelector` | Require nodes with specific labels |
| Node affinity | Richer label rules, required or preferred |
| Pod affinity and anti-affinity | Place Pods near or away from other Pods |
| Taints and tolerations | Nodes repel Pods unless they tolerate the taint |
| Topology spread constraints | Spread Pods across zones or nodes |
Seeing the decision
The scheduler records its result as an event. Run these commands to see it:
kubectl get pods -o wide
kubectl describe pod <pod-name>A successful placement shows a Scheduled event with a message like Successfully assigned default/web-abc12 to node-1. A failure shows FailedScheduling with a reason such as 0/3 nodes are available: 3 Insufficient cpu.
Common errors
- Pending with
Insufficient cpuorInsufficient memory: the requests are larger than any node's free capacity. Lower the requests or add nodes. - Pending with
didn't match Pod's node affinity/selector: a label is misspelled or no node carries it. - Pending with
had untolerated taint: add a matching toleration or use another node. - Pods all on one node: no spread rule was set, and the scoring preferences happened to favor that node.
Exercise
A Pod has been Pending for ten minutes. kubectl describe pod shows FailedScheduling: 0/3 nodes are available: 3 Insufficient memory. The nodes are using only 30% of their memory. Why can the Pod still be unschedulable, and what do you change?
Show answer
The scheduler compares the Pod's memory request with each node's allocatable memory minus the requests of Pods already placed there, not with actual usage. The nodes are fully reserved by requests even though they are mostly idle. Reduce the Pod's memory request, reduce the requests of other Pods, or add node capacity.