Resource requests and limits

Sharing a node fairly.

Beginner20 min · lesson 14 of 21
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (5 files, 1 KB): resources.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/resources.tar.gz && tar -xzf resources.tar.gz, which creates ~/lab/resources/. SHA-256: b54b5162883e2144aaa903bc1df11fc15451e0e8044b1932e8f86dc9a1ae0a88

Every Pod so far has run without saying how much CPU or memory it needs. On a three-node lab that is harmless. On a busy cluster it means the scheduler packs Pods onto nodes blind, and one app with a memory leak can starve its neighbours.

Two numbers per container change that. A request is what the container says it needs: the scheduler only places a Pod on a node with that much left unreserved. A limit is the most it may use, enforced by the node while it runs. CPU and memory limits are enforced very differently, as the broken Pods below show.

How much does a node have?

ubuntu@secopslog-k8s:~/lab/resources · Kubernetes 1.37
$ kubectl get nodes -o custom-columns=NAME:.metadata.name,CPU:.status.allocatable.cpu,MEMORY:.status.allocatable.memory
NAME CPU MEMORY lab-control-plane 4 6039664Ki lab-worker 4 6039664Ki lab-worker2 4 6039664Ki

Each node offers 4 CPUs and about 5.8 GiB of memory. All three are containers on the same machine, so they all report the whole VM. On a real cluster each node reports its own size, minus what the system keeps for itself.

Requests and limits on a Pod

CPU is counted in cores, and m means thousandths, so 250m is a quarter of one CPU. Memory is counted in bytes, with Mi meaning mebibytes:

web.yaml
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: nginx
image: nginx:1.30.6-alpine
resources:
requests: # what the scheduler reserves on a node
cpu: 250m # a quarter of one CPU
memory: 64Mi
limits: # the most the container may use
cpu: 500m
memory: 128Mi
ubuntu@secopslog-k8s:~/lab/resources · Kubernetes 1.37
$ kubectl apply -f web.yaml && kubectl wait --for=condition=Ready pod/web --timeout=60s && kubectl get pod web -o wide
pod/web created pod/web condition met NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES web 1/1 Running 0 2s 10.244.1.2 lab-worker <none> <none>
$ kubectl describe node "$(kubectl get pod web -o jsonpath='{.spec.nodeName}')" | grep -A 9 'Allocated resources'
Allocated resources: (Total limits may be over 100 percent, i.e., overcommitted.) Resource Requests Limits -------- -------- ------ cpu 350m (8%) 500m (12%) memory 114Mi (1%) 128Mi (2%) ephemeral-storage 0 (0%) 0 (0%) hugepages-1Gi 0 (0%) 0 (0%) hugepages-2Mi 0 (0%) 0 (0%) hugepages-32Mi 0 (0%) 0 (0%)

Allocated resources adds up the requests and limits of every Pod on the node the scheduler chose. The node shows 350m CPU and 114Mi memory requested, more than the web Pod asked for. The rest belongs to system Pods that kind runs on every node. The scheduler only looks at the Requests column. It never checks how much CPU a node is really using at this moment. The note above the table also matters: limits may add up to more than 100%, because Kubernetes lets nodes be overcommitted on limits.

Over the memory limit

This Pod reads /dev/zero. tail waits for the end of a line, and /dev/zero never sends one, so it keeps buffering in memory until something stops it:

hog.yaml
# Reads /dev/zero, which never ends a line, so tail keeps buffering until it hits the limit.
apiVersion: v1
kind: Pod
metadata:
name: hog
spec:
containers:
- name: hog
image: busybox:1.38.0
command: ["tail", "/dev/zero"]
resources:
limits:
memory: 32Mi

Apply it, then run kubectl get pod hog every few seconds until RESTARTS shows 1, and ask describe why:

ubuntu@secopslog-k8s:~/lab/resources · Kubernetes 1.37
$ kubectl apply -f hog.yaml
pod/hog created
$ kubectl get pod hog
NAME READY STATUS RESTARTS AGE hog 1/1 Running 1 (6s ago) 7s
$ kubectl describe pod hog | grep -A 5 'Last State'
Last State: Terminated Reason: OOMKilled Exit Code: 137 Started: Wed, 11 Nov 2026 09:39:26 +0000 Finished: Wed, 11 Nov 2026 09:39:26 +0000 Ready: False

RESTARTS went to 1 within about five seconds. Last State says why: Terminated, reason OOMKilled, exit code 137. That code is 128 plus 9, the number of the KILL signal. When a container goes over its memory limit, the kernel kills it, with no warning and no chance to clean up. The kubelet then restarts it, it fills up again, and before long it ends up in CrashLoopBackOff. If you see exit code 137 with OOMKilled, either the limit is too low or the app has a leak.

A request no node can meet

Requests protect the node, but a request that is too big means the Pod never runs:

big.yaml
apiVersion: v1
kind: Pod
metadata:
name: big
spec:
containers:
- name: nginx
image: nginx:1.30.6-alpine
resources:
requests:
cpu: "64" # 64 CPUs: more than any node in the lab has
ubuntu@secopslog-k8s:~/lab/resources · Kubernetes 1.37
$ kubectl apply -f big.yaml && kubectl wait --for=condition=PodScheduled=false pod/big --timeout=30s && kubectl get pod big
pod/big created pod/big condition met NAME READY STATUS RESTARTS AGE big 0/1 Pending 0 2s
$ kubectl events --for pod/big
LAST SEEN TYPE REASON OBJECT MESSAGE 2s Warning FailedScheduling Pod/big 0/3 nodes are available: 1 node(s) had untolerated taint(s), 2 Insufficient cpu. preemption: 0/3 nodes are available: 3 Preemption is not helpful for scheduling.

The Pod is Pending and stays that way. The event reads like a small report: of 3 nodes, the control-plane node has a taint that keeps apps off it, and the 2 workers have Insufficient cpu. Preemption, which can evict lower-priority Pods to make room, would not help either, since no node has 64 CPUs at all. Nothing else happens. The Pod waits until a big enough node appears or you lower the request. Pending with Insufficient cpu or Insufficient memory is one of the most common reasons a new Pod will not start.

QoS classes

Kubernetes also gives every Pod a QoS class, worked out from its requests and limits. A Pod is Guaranteed when every container has CPU and memory requests equal to its limits, BestEffort when no container sets any request or limit, and Burstable otherwise.

spin.yaml
# Burns CPU in a loop, capped at a fifth of one CPU.
apiVersion: v1
kind: Pod
metadata:
name: spin
spec:
containers:
- name: spin
image: busybox:1.38.0
command: ["sh", "-c", "while true; do :; done"]
resources:
requests:
cpu: 200m
memory: 16Mi
limits:
cpu: 200m
memory: 16Mi
ubuntu@secopslog-k8s:~/lab/resources · Kubernetes 1.37
$ kubectl apply -f spin.yaml && kubectl wait --for=condition=Ready pod/spin --timeout=60s && kubectl get pods web hog spin -o custom-columns=NAME:.metadata.name,QOS:.status.qosClass
pod/spin created pod/spin condition met NAME QOS web Burstable hog Burstable spin Guaranteed

spin sets equal requests and limits for both, so it is Guaranteed. web has requests below its limits, so it is Burstable. hog set only a memory limit. Kubernetes copies a limit into the request when the request is missing, but hog has no CPU values at all, so it is Burstable too.

The class matters when a node runs short of memory. The kernel's OOM killer uses it: Kubernetes sets each container's oom_score_adj from the class, so BestEffort containers are killed first and Guaranteed ones last. The kubelet's own eviction, which removes whole Pods before memory runs out completely, does not sort by class. It looks first at whether a Pod uses more than it requested, then at the Pod's priority, then at how far over its request it is. A BestEffort Pod requested nothing, so any memory it uses counts as over, and it ends up near the front of the line anyway.

CPU limits throttle

spin runs a busy loop that would happily use a whole CPU. Its limit is 200m. Look at its cgroup, the kernel feature that enforces the limit:

ubuntu@secopslog-k8s:~/lab/resources · Kubernetes 1.37
$ kubectl exec spin -- sh -c 'cat /sys/fs/cgroup/cpu.max; grep throttled /sys/fs/cgroup/cpu.stat'
20000 100000 nr_throttled 9 throttled_usec 485565
$ kubectl get pod spin
NAME READY STATUS RESTARTS AGE spin 1/1 Running 0 7s

cpu.max reads 20000 100000: in every 100 millisecond period the container may run for 20 milliseconds, which is 200m. Once it has used its share, the kernel pauses it until the next period. nr_throttled counts those pauses, already a handful in the first few seconds. And the Pod is still Running with 0 restarts. That is the big difference from memory: going over a CPU limit makes a container slow, never dead. A container that is often throttled shows up as slow responses, not as errors.

Hands-on

1. Start a Pod with no resources at all: kubectl run plain --image=nginx:1.30.6-alpine.

2. Wait for it: kubectl wait --for=condition=Ready pod/plain --timeout=60s.

3. Read its class: kubectl get pod plain -o jsonpath='{.status.qosClass}{"\n"}'.

4. Clean up with kubectl delete pod web hog big spin plain, then check with kubectl get pods.

What you should see: step 2 prints pod/plain condition met, and step 3 prints BestEffort, since kubectl run sets no requests or limits. Its memory use is all above its zero request, so it is among the first Pods the kubelet would evict under memory pressure. Step 4 removes all five Pods, including big, which never ran, and kubectl get pods says No resources found in default namespace.

Remember

Requests decide where a Pod is placed, and a request no node can meet leaves it Pending. Going over a memory limit gets a container OOMKilled with exit code 137, while going over a CPU limit only slows it down.

Quick check
01A container keeps restarting. describe shows Last State: Terminated, Reason: OOMKilled, Exit Code: 137. What happened?
Incorrect — Going over a CPU limit throttles. spin kept running with 0 restarts.
Incorrect — An eviction removes the whole Pod and shows reason Evicted, not OOMKilled.
Correct — hog reached its 32Mi limit and the kernel killed it; 137 is 128 plus signal 9.
Incorrect — A probe kill shows a Killing event, and the reason is not OOMKilled.
02A Pod requests 64 CPUs and every node has 4. What does kubectl get pod show?
Correct — The scheduler found no node with 64 CPUs free; the event said 2 Insufficient cpu.
Incorrect — Throttling applies to limits on a running container. This one is never placed.
Incorrect — The Pod never reaches a node, so it never starts or crashes.
Incorrect — The control-plane taint keeps all app Pods off it, whatever their size.
03A Pod sets a memory limit of 128Mi and nothing else. What is its QoS class?
Incorrect — Guaranteed needs CPU and memory requests equal to limits in every container.
Incorrect — The memory request is copied from the limit, so it is not BestEffort.
Incorrect — The class is set when the Pod is created, before scheduling.
Correct — hog set only a memory limit and its class was Burstable.

Related