Sub topic 3
How requests drive scheduling
This chapter assumes you can apply YAML manifests with kubectl and know what pods, nodes and namespaces are. It also assumes you have read the earlier chapters on what requests and limits are and on CPU and memory units. Here you will see how the scheduler uses requests, and only requests, to choose a node.
The scheduler reads requests, not usage
When you create a pod, the scheduler picks a node for it. For each candidate node it compares two numbers:
- The node's allocatable capacity. This is the node's total capacity minus what the system and the kubelet reserve for themselves.
- The sum of the requests of all pods already placed on that node.
A node fits the new pod only if the pod's requests fit in the remaining space. The scheduler does not look at how much CPU or memory the running pods actually use. A node can be nearly idle and still be "full" if the requests on it add up to its allocatable capacity.
A pod's request is the sum of the requests of its containers. Init containers are handled separately: the pod's effective request is the larger of the biggest init container request and the sum of the app containers' requests.
A worked example
Take a node with 2 CPU cores (2000m) and 4 GiB of memory allocatable. Three pods are already placed on it, each requesting 500m CPU and 1 GiB of memory.
| Resource | Allocatable | Requested by 3 pods | Free for new pods |
|---|---|---|---|
| CPU | 2000m | 1500m | 500m |
| Memory | 4 GiB | 3 GiB | 1 GiB |
Now you create this pod:
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: app
image: nginx:1.27
resources:
requests:
cpu: 250m
memory: 128Mi
limits:
cpu: 500m
memory: 256MiIt requests 250m CPU and 128 MiB, which fits in the free space, so this node is a candidate. A pod requesting 600m CPU would not fit, even if the three existing pods were using almost no CPU.
The limits do not take part in this decision. Only requests count for placement.
What happens when nothing fits
If no node has enough free requested capacity, the pod stays in the Pending state. The scheduler keeps retrying as the cluster changes, for example when pods finish or a node is added. You can see the reason in the pod's events:
kubectl describe pod webLook for a line like this in the Events section:
Warning FailedScheduling default-scheduler 0/3 nodes are available: 3 Insufficient cpu.Insufficient cpu or Insufficient memory means the requests do not fit on any node. The fix is to lower the request, free up space, or add capacity.
Requests you do not set
If a container has no request, the scheduler counts it as zero for that resource. Many such pods can pile onto one node, because they take no scheduling space. They then compete for real CPU and memory at runtime. A LimitRange can supply default requests to prevent this, and a later chapter covers it.
If you set a limit but no request, Kubernetes copies the limit into the request. This is a common source of surprise: a pod with only limits.cpu: 500m reserves 500m for scheduling.
Common errors
- Requests set too high. The scheduler reserves capacity that the pod never uses. Nodes look full while their real utilization is low, and the cluster costs more than it needs to.
- Requests set too low. Pods pack tightly, and the node can run short of real CPU or memory during busy periods.
- Requests larger than any node. A pod asking for 16 CPU on a cluster of 8-CPU nodes stays
Pendingforever. Adding more nodes of the same size will not help. - Forgetting reserved capacity. A node's allocatable amount is smaller than its total capacity. Run
kubectl describe node <name>and read theAllocatableandAllocated resourcessections to see the real numbers.
Exercise
A node has 4000m CPU allocatable. Pods on it request a total of 3200m CPU but use only 400m. A new pod requests 1000m CPU. Can the scheduler place it on this node, and why?
Show answer
No. The scheduler compares requests, not usage. Only 800m is left (4000m minus 3200m), and the new pod needs 1000m. The low actual usage does not matter. If no other node fits, the pod stays Pending with a FailedScheduling event that says Insufficient cpu.