Learn Labs
Resources & Scaling

Requests & Limits

Scheduling by requests, CPU throttling, OOM kills, and QoS classes

resources:requests: { cpu: 100m, memory: 64Mi }reserved: the scheduler only places the pod where this fitslimits: { cpu: 500m, memory: 192Mi }enforced at runtime by the kernel (cgroups)

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   33Mi

CPU 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
20 ms burst100% of full speed
  • working
50 ms burst100% of full speed
  • working
200 ms burst54% of full speed
  • working
  • throttled
1 s burst50% of full speed
  • working
  • throttled
3 s burst51% of full speed
  • 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.

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=4

Exit 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
ClassWhenUnder node memory pressure
Guaranteedrequests = limits for CPU and memory, every containerEvicted last
BurstableSome requests or limits setIn between
BestEffortNothing setEvicted first

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 LimitRange so pods without resources get defaults, and a ResourceQuota to cap what a namespace can claim.

Lab

  1. Run resources.py. Then remove the CPU limit and run the CPU part again.
  2. Set the memory request to 4Gi and apply. What happens to the new pods, and what does kubectl describe pod say?
  3. Make the web pods Guaranteed. What did you have to change?

On this page