cgroups & Namespaces
cgroups v1 vs v2, resource limits, container isolation mechanics, and namespace types.
Overview
Control groups (cgroups) and namespaces are the two kernel features that make containers possible. Cgroups provide resource limiting: they cap how much a group of processes may use. Namespaces provide isolation: they limit what those processes can see. This chapter takes them in that order and then puts them together.
Control Groups (cgroups v2)
Cgroups v2 organize processes into a hierarchy, mounted at /sys/fs/cgroup/, with resource controllers attached to it. On a systemd machine the top of the tree is split into slices, and each service gets a cgroup of its own, with its own limits.
Four controllers do most of the work.
- cpu: CPU time, as a share and as a quota.
- memory: the maximum RSS, swap and page cache.
- io: disk bandwidth.
- pids: the maximum number of processes.
When a cgroup's memory limit is exceeded, the kernel's out-of-memory (OOM) killer kills a process within that cgroup, not one elsewhere on the machine.
Key concepts
- cgroup memory.max
- PID namespace
- Network namespace
- User namespace
- Cgroup v2 unified hierarchy
Interview questions
- How do Linux namespaces enable container isolation? Walk through each namespace type.
Sign up free to read the full topic
More Linux topics
Start free on Cariara · Coding problems · System design studio