>_ IzzyLab

Hands-on task

This task is step 3 of the course “Containers from the inside: namespaces and cgroups”

The limit Docker sets

easy~10 min

The cache service keeps warmed-up data in memory: every restart means half an hour of cold cache and a slow site. It's run with a 128 MiB memory limit, and today it turned out that's not enough: it needs 384 MiB, and without a restart.

  1. Start a container cache in the background from the busybox:1.36 image with a memory limit of 128m; instead of a real cache, let sleep run inside for an hour.
  2. Find the container's cgroup on the server and look at its memory.max — that's how the kernel sees the --memory flag.
  3. Raise the running container's limit to 384m without recreating or restarting it, and make sure memory.max in the same group has changed.

The cache needs no swap: let the combined memory-and-swap limit equal the new memory limit.

What you'll practise:
  • cgroups
  • cgroup v2
  • docker
  • /proc

Topic breakdown

A cgroup (control group) is a kernel mechanism that limits and accounts for the resources of a group of processes. In cgroup v2 all groups form one directory tree mounted at /sys/fs/cgroup, and each group's directory holds the files of its limits and counters. Which group a process is in is shown in /proc/<PID>/cgroup: the line 0::/docker/<id> means the directory /sys/fs/cgroup/docker/<id>. Docker creates a group for every container and turns the docker run flags into its files: --memory into memory.max (in bytes), --cpus into cpu.max, --pids-limit into pids.max. --memory-swap is the combined limit of memory and swap, so memory.swap.max gets the difference --memory-swap minus --memory; if --memory-swap isn't set, Docker makes it twice the memory. cgroup limits can be changed on the fly while the group's processes keep running: docker update just rewrites the files of the same group. Kubernetes does the same with resources.limits, and in the end it's the kernel that enforces them — by exactly these files.

What's checked

  1. The cache container is running and was started during the task
  2. memory.max in cache's group is the new limit from the task
  3. Swap is not allowed for the cache container
  4. The limit was raised on the fly, without recreating or restarting

What it looks like