Sub topic 1
How the scheduler picks a node
Prerequisites
This chapter assumes you already know:
- Pods, nodes, and Deployments.
- How to write a YAML manifest and apply it with
kubectl. - What labels and label selectors are.
What the scheduler does
The scheduler is the control plane component kube-scheduler. It watches for pods that have no node assigned, which means spec.nodeName is empty. For each one, it chooses a node and records the choice by binding the pod to that node. The kubelet on the chosen node then pulls the images and starts the containers. The scheduler never starts containers itself.
The decision happens once, when the pod is created or first becomes schedulable. After that, the scheduler does not move the pod, even if conditions change.
Filter, then score
The scheduler picks a node in two phases.
- Filtering. The scheduler removes every node that cannot run the pod. The nodes that remain are called feasible nodes.
- Scoring. The scheduler ranks the feasible nodes. Each scoring plugin gives a node a score, and the scores are combined. The highest total wins. If several nodes tie, the scheduler picks one of them at random.
If filtering leaves no nodes, the pod stays Pending, and the scheduler retries later.
Where labels fit in
Labels on nodes feed both phases. The rest of this tutorial builds on that, so keep this table in mind.
| Mechanism | Phase | If the rule is not met |
|---|---|---|
| Resource requests (CPU, memory) | Filter | The node is removed |
| `nodeSelector` | Filter | The node is removed |
| Required node affinity | Filter | The node is removed |
| Taints the pod does not tolerate | Filter | The node is removed |
| Preferred node affinity | Score | The node gets a lower score |
A hard rule can leave a pod with no node. A soft rule only changes the ranking, so the pod can still run on a node that does not match.
Seeing the decision
The -o wide output shows which node a pod landed on. The Events section of describe shows why a pod is not scheduled.
kubectl get pod nginx-ssd -o wide
kubectl describe pod nginx-ssdWhen filtering removes every node, the Events section contains a FailedScheduling message. It looks like this:
Warning FailedScheduling 0/3 nodes are available: 3 node(s) didn't match Pod's node affinity/selector.The message counts the nodes and gives the reason each one was rejected. Read it first when a pod is stuck. A later chapter covers this in detail.
Common mistakes
- Assuming the scheduler looks at live usage. Filtering uses the resource requests of pods already on the node, not their current consumption. A node that is idle can still be full.
- Expecting a running pod to move. If you change a node's labels after scheduling, existing pods stay where they are.
- Expecting a preference to be a guarantee. Scoring only prefers nodes. Only filtering can exclude them.
Exercise
A pod has a required rule that no node satisfies, and a preferred rule that every node satisfies. What happens to the pod, and in which phase?
Show answer
The pod stays Pending. The required rule removes every node during filtering, so no feasible nodes remain. Scoring never runs, so the preferred rule has no effect.