Part 6 of 7 · 2 min read
Container tools
From flags to files
Think of a travel agent who takes a request such as "two rooms, one night, near the station" and turns it into forms for the hotel's booking system. The customer never sees the forms. The analogy stops in one place: a travel agent may choose a room for you, and a container tool writes exactly the value you asked for.
In plain words: Container tools create a cgroup for each container and write the values you give. The flags map as follows: --memory=20m writes the memory limit (20 x 1,048,576 bytes), --cpus=0.5 writes a quota of 50,000 in a period of 100,000, and --pids-limit=5 writes pids.max. Systemd does the same for its services, with MemoryMax= and CPUQuota= in a unit file. Because the effect is in plain files, a tool or a person can read the group of a running process from /proc/PID/cgroup and check the limits.
A worked example. A service must not use more than a quarter of 8 cores nor more than 512 MiB. The quota for 2 cores is 2 x 100,000 = 200,000 microseconds per 100,000 period, written as --cpus=2. The memory is 512 x 1,048,576 = 536,870,912 bytes. A service that starts 100 workers will share the 2 cores, so each worker gets on average 2 / 100 = 2 per cent of a core.
The key lines. Check the effect, as the programs of this page do: run a test inside the group, see the kill or the throttling, and read the counters.
Interview angle
A common question is "What is a container made of?" A good answer says: namespaces for the view, cgroups for the limits, a changed root file system for the files, and capabilities, seccomp and a MAC policy for the rights, all on one shared kernel.
Check yourself:
1. What would --cpus=1.5 write for the quota with a 100,000 period?
2. Where can you read the group of a running process?
3. Which resource is the group's memory limit measured in?
Words you will meet
| Word | Meaning |
|---|---|
| Control group (cgroup) | A group of processes with shared accounting and limits. |
| Controller | The part that manages one resource. |
| Quota / period | CPU time allowed per time slice. |
| Throttled | Stopped until the next period. |
pids.max |
The cap on processes in a group. |
| OOM kill | The kernel ends a process when memory is gone. |
Quick notes
- A cgroup limits a whole group. An
rlimitlimits one process. - Controllers: memory, cpu, pids, io. A group is a directory, a limit is a file.
- Move a process in by writing its ID to
cgroup.procs. - The test: a 20 MiB group killed the program near 10 to 20 MiB (exit 137), failcnt 36.
- A pid limit of 5 allowed 4 children. A 50 per cent quota gave 1.40 s of CPU in 3 s.
--memory,--cpusand--pids-limitwrite these files.