Linux cgroups: How Kubernetes CPU and Memory Limits Work
A Linux control group (cgroup) is a kernel mechanism for organizing processes and controlling their resource use. Learn how container runtimes apply Kubernetes requests and limits, why CPU throttling differs from memory OOM termination, and how to diagnose each case.
Linux cgroups: How Kubernetes CPU and Memory Limits Work
A Linux control group (cgroup) organizes processes hierarchically so the kernel can observe, distribute, or limit resources such as CPU time and memory. Containers share the host kernel; they do not receive separate physical CPUs or memory. A container runtime therefore commonly configures cgroups so the kernel can enforce workload resource policies. In Kubernetes, requests and limits serve different purposes: requests mainly guide scheduling, while Linux limits are typically enforced through kernel cgroups.
What does a cgroup control?
A cgroup is a Linux kernel mechanism for applying resource policies to groups of processes. Restrictions higher in the hierarchy also affect descendants; a child cannot exceed its parent's boundary. Controllers expose controls for particular resources, such as CPU time or memory. Available controllers and files depend on the kernel and cgroup version.
Current Linux cgroup v2 uses one unified hierarchy. The kernel exposes controller and status files in cgroup directories. Legacy cgroup v1 allowed multiple controller hierarchies, so do not assume that paths or behavior from v1 apply to v2. Directly writing host cgroup files is a low-level administrative operation that can affect other workloads; ordinary Kubernetes operation should let kubelet and the runtime connect API resources to cgroup settings.
How does this relate to Kubernetes containers?
When kubelet starts a container, it passes CPU and memory requests and limits to the runtime, which typically configures kernel cgroups on Linux. A CPU request commonly contributes a relative weight when workloads compete. A CPU limit sets a ceiling on CPU time and can cause throttling; exceeding it does not normally terminate the container.
Memory behaves differently. A memory request is mainly used by the scheduler to decide whether a Pod fits on a node. A memory limit sets a cgroup boundary. If the limit is reached and memory pressure occurs, kernel OOM handling may terminate a process in that cgroup. Kubernetes may restart a container if its process exits, depending on Pod behavior.
| Kubernetes field | Main purpose | Common result at the boundary |
|---|---|---|
CPU request | Scheduling capacity and relative weight during contention | Not a hard usage cap |
CPU limit | Runtime-configured ceiling on CPU time | Throttling may occur |
Memory request | Scheduling decision | Exceeding it alone does not immediately kill the container |
Memory limit | Runtime-configured cgroup memory boundary | OOM handling may terminate a process |
This summarizes common Linux behavior. Kernel, cgroup version, runtime, and Kubernetes configuration can change implementation and observability. On cgroup v2, a runtime may use memory requests as hints for memory.min or memory.low; that is not guaranteed to be configured identically everywhere.
How can you diagnose a resource problem?
For a slow application, distinguish CPU throttling from a process restarting after memory exhaustion. This is a diagnostic starting point, not a report of a test performed on a cluster.
Check `kubectl describe pod` events and container status for `OOMKilled`, restart counts, or scheduling failures. If the Pod has no node assignment, investigate requests and node capacity before cgroup enforcement.
If CPU use is near its limit and throttling metrics rise, check whether the CPU limit is the bottleneck. Raising the CPU request alone does not remove a hard limit.
For `OOMKilled`, compare memory use with the limit and review concurrent processes and memory-backed `emptyDir` usage. Node-pressure eviction is different from a container cgroup OOM.
Compare the Pod's `resources` with its deployment source, then check new Pod status, events, and metrics after a change. Raising limits without checking node capacity can harm other workloads.
Cgroups account for and control resources; they do not replace all security isolation. Linux namespaces and security policies govern other boundaries. Do not treat cgroups alone as a strong security boundary.
Sources
[Linux kernel: Control Group v2](https://docs.kernel.org/admin-guide/cgroup-v2.html)
[Kubernetes: Resource Management for Pods and Containers](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/)
[Kubernetes: Troubleshooting applications](https://kubernetes.io/docs/tasks/debug/debug-application/)