Sub topic 5
QoS and the Linux OOM killer
This chapter assumes you can read Pod manifests and know the three QoS classes (Guaranteed, Burstable, BestEffort) and how a Pod is assigned one. It also assumes you know that memory is incompressible. Here you will see how the kubelet turns a QoS class into a number the Linux kernel uses to pick victims.
Two kinds of OOM kill
An out-of-memory (OOM) kill can happen at two levels, and they work differently.
- Container-level: A container's memory use reaches its own limit. The kernel kills a process inside that container's cgroup. Kubernetes reports
OOMKilledwith exit code 137. QoS class does not matter here. - Node-level: The whole node runs out of memory. The kernel OOM killer picks a process to kill from the entire node. QoS class matters a lot here.
Node-level OOM kills are the last resort. The kubelet tries to evict Pods first, but it can be too slow when memory grows quickly.
How QoS becomes oom_score_adj
Every Linux process has an oom_score_adj value from -1000 to 1000. The kernel adds it to a score based on the process's memory use, and it kills the process with the highest total first. The kubelet sets oom_score_adj for each container according to its Pod's QoS class.
| QoS class | oom_score_adj |
|---|---|
| Guaranteed | -997 |
| BestEffort | 1000 |
| Burstable | `min(max(2, 1000 - (1000 × memory request) / node memory capacity), 999)` |
Pods with a system critical priority class also get -997. The kubelet itself runs at -999.
For Burstable Pods, a larger memory request gives a lower score, so the Pod is less likely to be killed. For example, a container requesting 128Mi on a 16Gi node gets 1000 - 7 = 993 using integer division. A container requesting 8Gi on the same node gets 500.
Check it on a running Pod
Apply the three manifests from the earlier chapters. Then read the value from each container's main process:
kubectl exec qos-guaranteed -- cat /proc/1/oom_score_adj
kubectl exec qos-burstable -- cat /proc/1/oom_score_adj
kubectl exec qos-besteffort -- cat /proc/1/oom_score_adjExpect -997 for the Guaranteed Pod and 1000 for the BestEffort Pod. The Burstable value depends on your node's memory size.
Common errors
- Assuming Guaranteed means unkillable. A Guaranteed container that exceeds its own memory limit is still OOM-killed at container level.
- Setting only a memory limit. Kubernetes copies the limit into the request, but if CPU has neither, the Pod is Burstable, not Guaranteed. It gets a score near the middle of the range.
- Confusing OOM kill with eviction. Eviction is the kubelet gracefully removing a Pod, and the Pod shows
Evicted. An OOM kill is the kernel killing a process, and the container restarts according torestartPolicy.
Exercise
A node has 10Gi of memory. A Burstable container requests 1Gi of memory. Roughly what oomscoreadj does the kubelet assign, and is it killed before or after a BestEffort container under node-level memory pressure?
Show answer
The score is about 1000 - (1000 × 1Gi / 10Gi) = 900. The BestEffort container has a score of 1000, so it is killed first, and the Burstable container comes after it.