Short answer: requests reserve, limits cap — a CPU cap slows you down, a memory cap kills
requests is what the scheduler reserves for a pod and what HPA utilization divides by. limits is the ceiling. A container at its CPU limit is throttled and slows down; past its memory limit it is OOMKilled (137) and dies.
Side by side
- CPU requests — scheduling reservation · HPA utilization = usage ÷ requests · contention splits CPU by requests · too high → Pending (
Insufficient cpu); too low → crowded nodes and a misled HPA - CPU limits — CPU time per CFS period (100 ms by default) · once spent, the container waits for the next period (throttling) · too low → low average, spiky latency
- Memory requests — scheduling reservation · sets eviction order when node memory runs short
- Memory limits — exceed it and the kernel OOM killer sends SIGKILL (
OOMKilled, 137) · no cushion like throttling - QoS — requests = limits for CPU and memory in every container → Guaranteed; anything else set → Burstable; nothing → BestEffort. Eviction generally starts with BestEffort and Burstable pods above their requests.
How it works in practice
resources:
requests:
cpu: "500m" # reserves 0.5 core · HPA 70% = 350m
memory: "512Mi"
limits:
cpu: "1" # above this: throttled
memory: "1Gi" # above this: OOMKilled
A CPU limit of 1 is not "one core" but 100 ms of CPU time every 100 ms. Eight busy threads burn it in 12.5 ms, then stall for 87.5 ms. Check from inside the container (cgroup v2):
$ kubectl exec checkout-7d9f8-x2k1p -n shop -- grep -E "periods|throttled" /sys/fs/cgroup/cpu.stat
nr_periods 54321
nr_throttled 13870
throttled_usec 912345678
nr_throttled ÷ nr_periods ≈ 25%: one period in four was cut short. On cgroup v1 the file is /sys/fs/cgroup/cpu/cpu.stat, with stalled time as throttled_time (nanoseconds). In Prometheus, divide the rate of container_cpu_cfs_throttled_periods_total by the rate of container_cpu_cfs_periods_total.
When to use which
- Requests on every container, near normal usage. With only limits set, requests default to them.
- Latency-sensitive APIs — leave CPU limit headroom above the peak. With accurate requests and trusted workloads on the node, dropping the CPU limit is an option, since contention still splits CPU by requests.
- Shared nodes or sloppy requests — keep CPU limits; otherwise one pod can grab the spare CPU and slow neighbours with undersized requests.
Incident 1: CPU usage looks low but p99 latency spikes
kubectl top shows 310m (31% of the limit), yet p99 spikes, worst in thread-heavy apps (JVM, Go). top shows an average, which hides 100 ms stalls. A high nr_throttled ratio means throttling. Raise the limit to the peak, or size worker and GC threads to it.
Incident 2: load surges but the HPA does not scale
$ kubectl describe hpa checkout -n shop
Warning FailedGetResourceMetric horizontal-pod-autoscaler failed to get cpu utilization: missing request for cpu in container "checkout" of Pod "checkout-7d9f8-x2k1p"
The HPA's 70% CPU means 70% of requests. With no requests, TARGETS stays <unknown>, so set them on every container, sidecars included. Requests several times real usage keep it from scaling; tiny ones make it overreact. New pods Pending with Insufficient cpu mean the requests no longer fit (Insufficient cpu guide).
Incident 3: treating the memory limit like CPU — OOMKilled
Last State: Terminated
Reason: OOMKilled
Exit Code: 137
Size the memory limit to normal usage, CPU-style, and the container dies at the first peak. CPU can be made to wait; memory cannot, so once reclaimable cache is gone the kernel kills the process. Always set a memory limit, with headroom above the peak (OOMKilled and exit code 137 guide).
What not to do
- Blindly following "always remove CPU limits" — weigh throttling, node sharing and QoS (no limits, no Guaranteed) per service.
- Removing all requests and limits — the pod becomes BestEffort: utilization-based HPA breaks and it is evicted first.
Summary
- Sort by symptom: Pending → requests, slowness → CPU limit, restarts with 137 → memory limit
- Put requests near normal usage on every container
- When latency spikes, check the
nr_throttledratio
To practise the HPA case, try HPA stays idle.
Quick diagnostic checklist
- Sort by symptom — Pending, latency spikes or restarts with 137 — to tell requests, CPU limit and memory limit apart.
- For Pending, read
Insufficient cpuin Events and the Requests percentage under the node's Allocated resources. - For latency spikes, compute
nr_throttled÷nr_periodsfrom the container's/sys/fs/cgroup/cpu.stat. - On cgroup v1 nodes, read
throttled_time(nanoseconds) in/sys/fs/cgroup/cpu/cpu.stat. - Use the
container_cpu_cfs_throttled_periods_totalratio in Prometheus to see when throttling rose. - If HPA TARGETS shows
<unknown>, check that every container, sidecars included, has a CPU request. Reason: OOMKilledwith exit code 137 is a memory limit problem, not a CPU one.- Check the QoS Class in
kubectl describe podto see who gets evicted first under node pressure.