Sub topic 2
CPU Throttles, Memory Kills: Why Resource Types Behave Differently
This chapter assumes you can read Pod manifests, know what resources.requests and resources.limits mean, and have a rough idea of what the kubelet does on a node. The last chapter covered how the scheduler and the kubelet use requests and limits. This one covers what happens when a container wants more than it was given.
Two kinds of resource
Kubernetes separates resources by what the node can do when demand exceeds supply.
- A compressible resource can be taken back from a running process without killing it. CPU is the main example. A process that gets less CPU time runs slower, but it keeps running.
- An incompressible resource cannot be taken back without ending the process that holds it. Memory is the main example. Once a process has written data into memory, the kernel cannot simply remove it.
This difference decides what failure looks like for each resource.
What happens when CPU runs short
A CPU request is a share of CPU time. On a busy node, the Linux scheduler divides CPU time among containers in proportion to their requests. A container that asks for more than its request can only use it if other containers leave CPU idle.
A CPU limit is a hard ceiling. The kernel enforces it with CFS bandwidth control, which gives the container a quota of CPU time in each 100 ms period. When the container uses up its quota, it is throttled: its threads wait until the next period starts. The container is not killed. You see higher latency, not a restart.
What happens when memory runs short
A memory request is used for scheduling. Memory is not divided by shares at runtime, and nothing slows a process down to make it use less memory.
A memory limit is enforced by the kernel's cgroup. If a container tries to use more than its limit and the kernel cannot reclaim enough memory, the kernel's out-of-memory (OOM) killer ends a process in that container. Kubernetes reports this as OOMKilled, and the kubelet restarts the container according to the Pod's restartPolicy.
Memory can also run out for the whole node, even when no container has reached its own limit. Then the kernel or the kubelet must choose victims. Later chapters cover how QoS class shapes that choice.
Seeing the difference
Both failures show up in different places.
| Symptom | Resource | Where to look |
|---|---|---|
| Slow responses, no restarts | CPU | Container CPU throttling metrics, such as `container_cpu_cfs_throttled_periods_total` |
| Restarts with reason `OOMKilled` | Memory | `kubectl describe pod`, under `Last State` |
To check for a memory kill, read the last termination reason:
kubectl get pod qos-burstable -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}{"\n"}'If the container was killed for exceeding its memory limit, this prints OOMKilled.
Why this matters for QoS
When a node is short of CPU, containers slow down, and nothing needs to be removed. When a node is short of memory, something must be killed. QoS classes and priority only matter for the second case, which is why the rest of this tutorial focuses on memory when it discusses OOM kills and eviction.
Exercise
A container has a CPU limit of 500m and a memory limit of 256Mi. It starts responding slowly under load, but kubectl get pod shows zero restarts. Which resource is the likely cause, and why did the container not restart?
Show answer
CPU is the likely cause. CPU is compressible, so when the container reaches its 500m limit the kernel throttles it instead of killing it. A memory overrun would have caused an OOM kill and a restart with reason OOMKilled.