Requests & Limits
Scheduling by requests, CPU throttling, OOM kills, and QoS classes
Requests are a scheduling promise: the scheduler adds up the requests
of pods on each node and won't overfill it. Limits are enforced while
running, and CPU and memory are enforced in completely different ways.
100m is a tenth of a CPU core.
kubectl describe node lab-worker2
Resource Requests Limits
cpu 300m (2%) 1 (8%)
memory 178Mi (1%) 384Mi (3%)
kubectl top pods -n shop # actual usage, from metrics-server
web-7cb7f4d4cd-8rf6l 2m 33MiCPU over the limit: throttled
A 500m limit means 50 ms of CPU time in every 100 ms period. A process that
uses its quota early in a period is paused until the next one starts.
resources.py spins the CPU for a fixed wall-clock time and counts the
work done:
./scripts/lab.sh resources.py
CPU: spin for N ms of wall-clock time, count the work done
20 ms burst: 3,507 hashes/ms = 100% of full speed
50 ms burst: 3,652 hashes/ms = 104% of full speed
200 ms burst: 1,909 hashes/ms = 54% of full speed
1000 ms burst: 1,762 hashes/ms = 50% of full speed
3000 ms burst: 1,775 hashes/ms = 51% of full speed- working
- working
- working
- throttled
- working
- throttled
- working
- throttled
Short bursts fit inside one period's quota and run at full speed; anything
longer settles at half speed, exactly 500m. Nothing crashes and nothing
is logged: requests just get slower. Throttling is invisible unless you
look at container_cpu_cfs_throttled_periods_total.
A CPU limit can make latency much worse even on an idle node with spare cores: a request needing 80 ms of CPU waits through a throttled period. Many teams set CPU requests but no CPU limit for latency-sensitive services, and rely on requests for fair sharing. Memory limits are different: always set them.
Memory over the limit: killed
Memory can't be throttled: once a container uses more than its limit, the kernel's OOM killer ends it, and the kubelet restarts it.
memory: allocating 50 MB at a time in web-5865ccc48d-2g5q7 (limit 192Mi)
+50 MB -> holding 50 MB
+50 MB -> holding 100 MB
+50 MB -> holding 150 MB
+50 MB -> no response (command terminated with exit code 52)
container restarted: reason=OOMKilled exit_code=137 restarts=4Exit code 137 is 128 + 9: killed by SIGKILL. The request in flight got
an empty reply; everything in the process's memory was lost. Repeated OOM
kills turn into CrashLoopBackOff, with growing delays between restarts.
The KubeContainerOOMKilled alert catches it.
QoS classes
kubectl -n shop get pods -o custom-columns=NAME:.metadata.name,QOS:.status.qosClass
redis-0 Burstable
shop-nginx-5dcc7f4599-bns59 BestEffort
web-7cb7f4d4cd-8rf6l Burstable| Class | When | Under node memory pressure |
|---|---|---|
| Guaranteed | requests = limits for CPU and memory, every container | Evicted last |
| Burstable | Some requests or limits set | In between |
| BestEffort | Nothing set | Evicted first |
The Gateway's own NGINX pod, provisioned by NGINX Gateway Fabric with
default settings, is BestEffort: the first pod the kubelet evicts
when a node runs low on memory, taking all incoming traffic with it.
Platform components deserve explicit requests (NGINX Gateway Fabric sets
them through its NginxProxy resource), and often a PriorityClass.
Sizing
- Start requests from measured usage (
kubectl top, Prometheus) at typical load, not from guesses. Requests far above usage waste nodes; requests far below make nodes overcommitted and noisy. - Memory limit: comfortably above peak usage. An OOM kill is a crash.
- Use a
LimitRangeso pods without resources get defaults, and aResourceQuotato cap what a namespace can claim.
Lab
- Run
resources.py. Then remove the CPU limit and run the CPU part again. - Set the memory request to
4Giand apply. What happens to the new pods, and what doeskubectl describe podsay? - Make the web pods Guaranteed. What did you have to change?