liveBook · Manning

Cgroup

As explained in Kubernetes in Action, Second Edition

A cgroup is a Linux kernel mechanism that groups processes and limits, accounts for, and isolates their use of resources such as CPU, memory, and bandwidth.

A cgroup, or Linux Control Group, groups processes and limits, accounts for, and isolates their use of resources such as CPU, memory, disk, and network bandwidth. It prevents one process or container from consuming resources reserved for others. section 2.3.3

Namespaces control which resources a process can see; cgroups control how much of those resources it can consume. Without cgroups, a container may access all available CPU cores and system memory. section 2.3.3

Docker exposes common cgroup limits through container-run options:

  • Select permitted CPU cores with --cpuset-cpus. section 2.3.3
  • Limit CPU time with --cpus, --cpu-period, --cpu-quota, or --cpu-shares. section 2.3.3
  • Limit memory and swap usage with --memory, --memory-reservation, --kernel-memory, --memory-swap, and --memory-swappiness. section 2.3.3
$ docker run --cpuset-cpus="1,2" ...

Restrict a container to selected host CPU cores with Docker's --cpuset-cpus option. section 2.3.3

$ docker run --cpus="0.5" ...

Allow a container to use half of one CPU core with Docker's --cpus option. section 2.3.3

$ docker run --memory="100m" ...

Set a container's maximum memory size with Docker's --memory option. section 2.3.3

Cgroups help prevent resource starvation but do not make containers fully secure: their processes still share the system kernel. Capabilities, seccomp, AppArmor, and SELinux provide additional isolation. section 2.3.4 section 2.4

How Kubernetes in Action, Second Edition covers it

How deep it goes
Defines resource control and demonstrates Docker limits, but stops short of explaining cgroup internals.
Examples
Demonstrates Docker commands for selecting CPU cores, limiting CPU time, and setting container memory limits.
What it assumes
Assumes basic processes and containers, while distinguishing cgroups from namespaces and introducing their purpose directly.
What you can do after
Apply cgroups through Docker options to constrain a container's CPU and memory consumption.
How it connects
Connects cgroups with Linux namespaces, containers, Docker, the shared kernel, and security mechanisms such as seccomp and SELinux.

Also explained in

Compare how 3 books explain cgroup →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.