I am trying to understand the underlying OS mechanism. Why exactly does the Linux kernel kill pods when Kubernetes memory limits are breached? Is there a way to configure the container runtime to throttle the memory usage instead of abruptly terminating the pod process?
3 answers
Kubernetes leverages Linux control groups, specifically cgroups, to enforce memory boundaries. Unlike CPU usage, which can be throttled by compressing time slices, memory cannot be throttled or compressed. If a process demands physical RAM that exceeds its cgroup allocation and there is no swap space configured, the operating system has no choice but to invoke the Out-Of-Memory killer. The kernel selects the process consuming the most resources relative to its allowance and terminates it immediately to maintain core OS stability.
Did you know that you can configure memory exchange settings or swap utilization in newer versions of Kubernetes to mitigate these sudden terminations?
Memory is a non-compressible resource. When you run out, you cannot slow down the execution like CPU; the system must free up physical space immediately by killing the largest consumer.
Exactly. It is a fundamental hardware limitation. Understanding the distinction between compressible and non-compressible resources is key to solid cluster architecture design.
Enabling swap can prevent immediate kills, but it introduces massive disk I/O latency. For real-time applications, relying on swap usually degrades performance to an unacceptable level compared to a quick restart.