Pod cgroup Hierarchy

You set CPU and memory limits on a Pod, and the Linux kernel on its Node must enforce them, yet the kernel never reads a Pod spec. So where do those numbers end up for the kernel to find, and who writes them?

Play the animation 6 steps, animated, plays on a computer or a tablet

Pod cgroup Hierarchy diagram: Pod cgroup hierarchy on a Node

Pod cgroup Hierarchy, step by step

  1. On a cgroup v2 Node the whole tree is one unified hierarchy mounted at /sys/fs/cgroup, and stat -fc %T on that path answers cgroup2fs. The Kubelet and the container runtime both write into it, so they have to agree on one cgroup driver, and with cgroup v2 that is the systemd driver on both.

    Play from step 1
  2. Under that root the Kubelet creates kubepods.slice and parents every end-user Pod below it, which is what cgroupsPerQOS does and it is on by default. The default enforceNodeAllocatable setting then makes Allocatable the ceiling over that whole subtree, enforced by evicting once the Pods together pass it.

    Play from step 2
  3. One level down, kubepods.slice has three kinds of child. Burstable Pods go under kubepods-burstable.slice and BestEffort Pods under kubepods-besteffort.slice, so a value written on one QoS cgroup reaches the whole tier below it. A Guaranteed Pod gets no QoS slice: its own slice hangs straight off kubepods.slice.

    Play from step 3
  4. When Pod web-0 starts, the Kubelet gives it a slice of its own inside the tier its QoS class picked, named for the Pod uid. That slice is the parent of every container in the Pod, so the Pod is accounted in one place whatever its containers do inside it.

    Play from step 4
  5. The container runtime creates the last level, one leaf cgroup per container, and writes the values into it: cpu.max from limits.cpu, memory.max from limits.memory in bytes, cpu.weight from requests.cpu. On runc 1.3.2 and crun 1.23 or later, 250m maps to weight 35, on older ones to 10. The no-internal-process rule keeps processes on the leaves, so this is the cgroup the app runs in.

    Play from step 5
  6. The tree only holds the numbers. Two cards show what acts on them: CPU Throttling and CFS Quota takes cpu.max as the budget a container may spend inside each period, and Container OOMKill takes memory.max as the cap that gets a container killed.

    Play from step 6

Sources