Lesson 33 · Section 3: Scheduling
Lesson 33: Resource requirements: telling Kubernetes what a Pod needs
What a resource request is, how CPU and memory are written, how the scheduler adds requests up against a node's allocatable capacity, what a request still does once the Pod runs, and how to set, read and fix requests in the exam. · 20 min read · 3 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why resource requirements exist
A node has a fixed amount of CPU and memory, and the scheduler has to choose a node for a Pod before the Pod has run for a single second. It cannot measure what the Pod will use. If it had nothing to go on, it would keep placing Pods on a node until they starved each other, and the first sign of trouble would be slow applications and killed containers.
So you tell Kubernetes what the Pod needs. That statement is the Pod's resource requirements, and its most important part is the request: the amount of each resource a container needs at least.
This lesson is about requests. The other half of the resources block, limits, is the ceiling a container may
not pass; the next lesson covers it.
What a request is
Requests are written per container, in resources.requests:
apiVersion: v1
kind: Pod
metadata:
name: api
spec:
containers:
- name: api
image: nginx:1.29-alpine
resources:
requests:
cpu: 250m # a quarter of a CPU core
memory: 128Mi # 128 mebibytes
The resources you can request:
| Resource | Counted in | Notes |
|---|---|---|
cpu | Cores. 1 is one core (one vCPU or hyperthread); 250m is 250 millicores, a quarter of a core | 0.25 and 250m are the same. The smallest step is 1m |
memory | Bytes, with a suffix: Ki, Mi, Gi (powers of 1024) or k, M, G (powers of 1000) | 128Mi is 134 217 728 bytes; 128M is 128 000 000 bytes |
ephemeral-storage | Bytes, like memory | Local disk for the writable layer, logs and emptyDir volumes |
hugepages-2Mi, hugepages-1Gi | Bytes | Pre-allocated huge pages; request must equal limit |
Extended resources such as nvidia.com/gpu | Whole numbers | Advertised by a device plugin; cannot be shared or overcommitted |
A few rules that follow from the API:
- No request means zero. A container without a request for a resource is treated as needing none of it. It fits on any node, and it has no reserved share when the node gets busy.
- A limit without a request copies the limit. If you set only
limits.cpu: 500m, Kubernetes sets the request to500mas well (unless a LimitRange supplies a different default). - A request can never be larger than its limit. The API server rejects the Pod.
- The Pod's request is the sum of its containers. Two containers that each request
250mmake a Pod that needs500mfree on one node.
How the scheduler uses requests
Parts:
- Capacity (4 CPU, 8Gi)
- Reserved (system and eviction)
- Allocatable (3.8 CPU, 7.5Gi)
- Requested (Pods here: 3 CPU)
- Unreserved (0.8 CPU left)
- New Pod (requests 1 CPU)
Connections:
- Capacity to Reserved: minus
- Reserved to Allocatable: leaves
- Allocatable to Requested: minus requests
- Requested to Unreserved: leaves
- New Pod to Unreserved: too big
Each node reports two numbers per resource, and you can see both in kubectl describe node:
- Capacity is what the machine has.
- Allocatable is what Pods may reserve: capacity minus what the kubelet holds back for the operating system
(
systemReserved), for Kubernetes daemons such as the kubelet and the container runtime (kubeReserved), and the hard eviction threshold (by default 100Mi of memory). On a plain kubeadm node nothing else is reserved, so allocatable memory is capacity minus 100Mi and allocatable CPU equals capacity.
The scheduler's NodeResourcesFit filter then does simple arithmetic for every node:
unreserved = allocatable - sum of the requests of all Pods assigned to the node
the Pod fits if, for every resource, its request <= unreserved
Parts:
- Pod api (1 CPU, 1Gi)
- kube-scheduler (NodeResourcesFit)
- node-1 (500m, 4Gi free)
- node-2 (2 CPU, 512Mi free)
- node-3 (2 CPU, 4Gi free)
Connections:
- Pod api to kube-scheduler: requests
- kube-scheduler to node-1: no: cpu
- kube-scheduler to node-2: no: mem
- kube-scheduler to node-3: fits: bind
Three things about this sum are worth remembering:
- Real usage is not in it. A node at 5% CPU load can be "full" because its Pods have reserved everything. A node on its knees can still accept Pods that request nothing.
- Every resource must fit on the same node. Spare CPU on node-1 and spare memory on node-2 do not help a Pod that needs both.
- Requests also steer scoring. Among the nodes that pass, the default scheduler prefers the one that keeps the largest unreserved fraction after placing the Pod, and a balanced use of CPU and memory.
If no node passes, the Pod stays Pending with a FailedScheduling event such as
0/3 nodes are available: 3 Insufficient cpu. It is scheduled on its own as soon as room appears.
Under the hood · The effective request of a Pod with init containers and sidecars
Init containers run one after another before the app containers start, so they do not all need room at the same time. The scheduler therefore reserves the larger of two numbers, per resource:
- the sum of the requests of all app containers and sidecars (init containers with
restartPolicy: Always), which all run together, and - the largest request of any single init container, plus the sidecars that are already running when it starts.
| Containers | CPU requests | Effective Pod request |
|---|---|---|
app 400m, app 100m | sum 500m | 500m |
init 1, app 200m | init 1, apps 200m | 1 (the init container is the peak) |
sidecar 100m, init 200m, app 50m | init phase 300m, running 150m | 300m |
A big init container can therefore make a small Pod unschedulable. If the Pod uses a RuntimeClass with an
overhead, that amount is added on top.
Since v1.34 (beta, on by default) a Pod can also state one budget for all its containers in spec.resources, for
cpu, memory and huge pages. When it is set, the scheduler uses the Pod-level request instead of the sum of the
containers.
What a request does once the Pod runs
A request is not only a scheduling number. The kubelet passes it on to the Linux kernel through the container's cgroup.
Parts:
- Container a (requests 250m)
- Container b (requests 750m)
- Linux kernel (one busy CPU)
- a runs 25% (of the CPU time)
- b runs 75% (of the CPU time)
Connections:
- Container a to Linux kernel: weight 1
- Container b to Linux kernel: weight 3
- Linux kernel to a runs 25%: 1 part
- Linux kernel to b runs 75%: 3 parts
| Resource | At run time, the request means |
|---|---|
cpu | A weight. When containers compete for CPU, each gets time in proportion to its request. When CPU is idle, a container may use far more than it requested (up to its limit, if it has one). A container with no CPU request gets the smallest weight there is |
memory | Nothing is fenced off. A container may use more memory than it requested. But when the node runs short, the kubelet evicts Pods that use more than they requested first, and the kernel's out-of-memory killer prefers them too |
ephemeral-storage | Like memory: Pods that use more than they requested are evicted first when the disk fills up |
That is the reason for the usual advice: request what the application normally needs. Too low, and the Pod is packed onto a crowded node and is the first to suffer there. Too high, and the cluster looks full while it is idle, and other Pods stay Pending.
Requests and limits together also decide the Pod's QoS class, which you can read in status.qosClass:
| QoS class | When | Under node pressure |
|---|---|---|
Guaranteed | Every container has CPU and memory requests equal to its limits | Evicted last |
Burstable | At least one request or limit is set, but not the Guaranteed pattern | In between |
BestEffort | No requests and no limits at all | Evicted first |
Where requests come from
You do not always write the request yourself. Three mechanisms can set, require or cap it:
| Source | What it does | When |
|---|---|---|
| The Pod spec | resources.requests on each container (or spec.resources for the whole Pod) | You write it |
LimitRange in the namespace | defaultRequest fills in a request for containers that have none; min and max reject values outside the range | At admission, when the Pod is created |
ResourceQuota in the namespace | Caps the sum of requests in the namespace (requests.cpu, requests.memory). Once it covers a resource, every new Pod must request that resource or is rejected | At admission |
A LimitRange only touches Pods created after it exists. It never changes running Pods.
apiVersion: v1
kind: LimitRange
metadata:
name: defaults
namespace: shop
spec:
limits:
- type: Container
defaultRequest: # used when a container sets no request
cpu: 100m
memory: 64Mi
Using it
Example · Create a Pod with requests
Task: run a Pod api with image nginx:1.29-alpine that requests 250m CPU and 128Mi memory. kubectl run has no
flag for resources, so let it write the YAML and add the block:
kubectl run api --image=nginx:1.29-alpine --dry-run=client -o yaml > api.yaml
The generated file already contains resources: {}. Replace it:
spec:
containers:
- name: api
image: nginx:1.29-alpine
resources:
requests:
cpu: 250m
memory: 128Mi
kubectl apply -f api.yaml
kubectl get pod api -o jsonpath='{.spec.containers[0].resources}{"\n"}{.status.qosClass}{"\n"}'
{"requests":{"cpu":"250m","memory":"128Mi"}}
Burstable
Example · Set requests on a Deployment in one command
Task: every Pod of the Deployment web must request 200m CPU and 256Mi memory.
kubectl set resources deployment web --requests=cpu=200m,memory=256Mi
kubectl rollout status deployment web
With several containers in the template, name the one you mean with -c:
kubectl set resources deployment web -c nginx --requests=cpu=200m,memory=256Mi
This edits the Pod template, so it starts a rolling update. If the new Pods do not fit, the rollout stalls with new
Pods Pending while the old ones keep serving; kubectl rollout undo deployment web takes you back.
Example · Find out why a Pod does not fit, and fix it
The Pod report is Pending:
kubectl describe pod report | tail -n 3
Warning FailedScheduling default-scheduler 0/3 nodes are available: 1 node(s) had untolerated taint
{node-role.kubernetes.io/control-plane: }, 2 Insufficient memory.
Two workers were rejected for memory. Compare what the Pod asks for with what a worker can still give:
kubectl get pod report -o jsonpath='{.spec.containers[*].resources.requests}{"\n"}'
kubectl describe node node-1 | grep -A 3 "^Allocatable"
kubectl describe node node-1 | grep -A 8 "Allocated resources"
{"cpu":"100m","memory":"8Gi"}
Allocatable:
cpu: 2
memory: 3928Mi
Allocated resources:
Resource Requests Limits
cpu 850m (42%) 1 (50%)
memory 1200Mi (30%) 2Gi (52%)
The Pod requests 8Gi and the node offers 3928Mi in total, so it can never fit, even on an empty node. Here the
request is simply wrong (perhaps 8Gi should have been 800Mi). A Pending Pod has no node yet, but its resources
still cannot be changed with a plain edit, so recreate it:
kubectl get pod report -o yaml > report.yaml # correct the request in the file
kubectl replace --force -f report.yaml
kubectl get pod report -o wide
If the request is right and the nodes are only crowded, the fix is on the other side: scale down or delete something that holds reservations, or add a node. The Pod is then scheduled without any further action.
Example · Change the CPU request of a running Pod
Task: lower the CPU request of the running Pod api to 100m without recreating it.
kubectl patch pod api --subresource resize --patch \
'{"spec":{"containers":[{"name":"api","resources":{"requests":{"cpu":"100m"}}}]}}'
kubectl get pod api -o jsonpath='{.status.containerStatuses[0].resources}{"\n"}'
spec holds what you asked for and status.containerStatuses[].resources holds what the container really has. If
you raise a request beyond what the node can give, the Pod keeps running with its old values and gets the condition
PodResizePending, with reason Deferred (may fit later) or Infeasible (can never fit on this node).
Tricks
Common mistakes
- Explaining "Insufficient cpu" with
kubectl top. The scheduler never looks at usage. ReadAllocated resourcesinkubectl describe node. - Writing
memory: 256mormemory: 256mi. The first is a quarter of a byte, the second is not a valid quantity. Memory is256Mi; CPU is250m. - Putting
resourcesat the wrong level. It belongs under each entry ofcontainers, next toimage, not directly underspec(that is the separate Pod-level field) and not undermetadata. - Editing the request of a running Pod with
kubectl edit. It is refused. Use the Pod template of the owning Deployment,kubectl replace --force, or theresizesubresource. - Expecting a new LimitRange to fix existing Pods. Defaults are applied only when a Pod is created.
- Setting only a limit and being surprised by the request. The request silently becomes equal to the limit, so a generous limit can make the Pod unschedulable.
- Forgetting init containers. One init container with a large request sets the request of the whole Pod.
Recap
- A request is the minimum of a resource a container needs:
cpuin cores or millicores,memoryin bytes withMiorGi. - The scheduler places a Pod only where allocatable minus the requests already there covers every request of the Pod.
- Real usage is never counted; a container may use more than it requested, and may use less.
- At run time a CPU request is a share under contention, and Pods using more memory than they requested are evicted first.
- Pending with
Insufficient cpuorInsufficient memory: compare the Pod's requests withkubectl describe node.
Official docs
- Kubernetes docs: Resource Management for Pods and Containers
- Kubernetes docs: Assign CPU Resources to Containers and Pods
- Kubernetes docs: Assign Memory Resources to Containers and Pods
- Kubernetes docs: Reserve Compute Resources for System Daemons
- Kubernetes docs: Effective Pod Resource Requests and Limits
- Kubernetes docs: Resize CPU and Memory Resources assigned to Containers
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.
Find out why a pod fits no node, read requests written in different units, and give a two-container pod the reservation its team measured.
Fix · Level 2 · standard · target 15 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.