Skip to content

Lesson 8 of 9 · 23m

cgroups, Namespaces, and Container Isolation

What containers actually are, how limits and isolation differ, and why 'inside the container' is only half the truth.

A container is not a lightweight virtual machine. It is a collection of Linux namespaces controlling what a process can see, plus cgroups controlling what it can use, plus filesystem and security layers that shape its execution. Once you see it that way, most container mysteries stop being container-specific.

Namespaces control visibility

Common namespaces isolate process IDs, mounts, network interfaces, hostnames, users, and IPC. Inside a PID namespace, the main process is PID 1 even though it is an ordinary nonzero PID on the host.

readlink /proc/self/ns/pid
lsns -p 18422

PID 1 inside a container is operationally important. If your app does not reap zombie children or forward signals properly, shutdowns hang and zombies accumulate. That is why minimal init systems or proper signal-aware executables matter.

Visibility isolation is not the same as resource protection. A process can see only its own network namespace while still consuming host CPU, memory, disk bandwidth, or kernel resources unless cgroups limit it.

cgroups control usage and accounting

cat /sys/fs/cgroup/cpu.max
cat /sys/fs/cgroup/memory.current
cat /sys/fs/cgroup/memory.max
cat /sys/fs/cgroup/io.stat

cpu.max is a quota over a period. If a container reads 100000 100000, it gets one CPU-equivalent per period. A latency-sensitive service with four busy threads and a one-CPU quota will throttle regardless of spare host CPU.

Memory limits are similarly deliberate. Exceeding them triggers cgroup OOM behavior, often killing the workload before host memory is exhausted. That is isolation working as designed — but only if you chose the limit intentionally.

Containers see host truth through leaky abstractions

Applications often read /proc/meminfo or count CPUs from /proc/cpuinfo and size thread pools or heaps for the entire host. Inside a restricted container, that is wrong. Many modern runtimes are cgroup-aware, but older ones are not. Verify what the runtime detects instead of assuming.

Also remember that kernel state is shared. File handles, network conntrack entries, inotify instances, and some sysctls are host or namespace scoped in ways people do not expect. 'It worked in a VM' can differ because a VM has its own kernel.

systemd services use the same primitives

You do not need Docker to benefit from isolation. systemd units can set:

[Service]
MemoryMax=1G
CPUQuota=150%
PrivateTmp=yes
ProtectSystem=strict
NoNewPrivileges=yes

This is often the right middle ground for simple production services: strong guardrails without a container runtime. It also reinforces the point — containers are composition of Linux mechanisms, not magic.

Debugging requires both viewpoints

From inside the container, memory may look tight but you cannot see the host pressure. From the host, you can see a cgroup being throttled but not which application thread is causing it. Correlate both: container ID or cgroup path, host PID, unit name, and runtime metrics. In Kubernetes, the pod's QoS class and container requests/limits shape scheduling and eviction; knowing the cgroup outcome is still necessary.

Field notes

  • A container exit code of 137 often means SIGKILL, frequently from a cgroup OOM, but verify with runtime and kernel logs rather than assuming.
  • cpu.max throttling can produce low average CPU and high p99 latency at the same time.
  • Hostnames, IP addresses, and even user IDs can differ across namespaces. Record namespace-aware context in incident notes.
  • Isolation reduces blast radius; it does not make an overcommitted host healthy.

Was this lesson helpful?

Sign in to rate