Technical skill

Linux CGgoups V2

Give a group of programs a shared resource limit

A computer may run many programs at once. Control groups, shortened to cgroups, let Linux group processes so the kernel can count or limit their use of CPU time, memory, and input/output. For example, a service that uses too much memory can be given a cap so it cannot take all the machine's memory. A cgroup is about resource use; a namespace changes what resources or names a process can see. Containers often use both, for different purposes.

Cgroup version 2 uses one unified hierarchy. Version 1 commonly exposed separate hierarchies for different controllers. A controller is the part that accounts for or limits one resource, such as cpu, memory, io, or pids. Which controllers are available depends on kernel support and system configuration. A parent cgroup can set limits that also apply to its child cgroups, so a child cannot escape a limit set higher in the tree.

Read the cgroup files

A process's cgroup path appears in /proc/self/cgroup; a v2 entry often looks like 0::/some/path. The cgroup2 filesystem is commonly mounted at /sys/fs/cgroup, though the mount location and visible hierarchy depend on the system. A cgroup.controllers file lists controllers that can be enabled for child groups at that level.

With the memory controller, memory.current reports current memory use and memory.max gives the configured limit. The value max means this cgroup has no limit set there, though an ancestor may still impose one. These files show kernel accounting; they do not explain everything an application is doing.

On many systems, systemd owns the hierarchy. To find a unit's cgroup path, run systemctl show -p ControlGroup example.service; systemd-cgls displays cgroups as a tree. These views help connect a process to the service or slice that contains it. If another manager owns the hierarchy, its tools and configuration determine how to inspect it.

Understand the systemd limits

Systemd unit settings configure common resource controls. MemoryMax=1G sets an upper memory limit for a unit. If the workload cannot stay under that limit, systemd documents that the out-of-memory killer is invoked inside the unit. Treat this as a last line of defense and size the limit for the workload.

CPUWeight=200 changes the unit's relative share of CPU time among units in the same slice. It does not reserve a fixed percentage. The weight affects distribution when units compete for CPU time.

When systemd manages a cgroup, change limits through unit configuration and the service manager. Editing cgroup files directly can conflict with the manager or fail to persist. Some environments let another program manage a branch of the hierarchy, so identify who owns it before making changes.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful