Node
Starting a container
Namespaces and cgroups
What a container really is: a process with its own namespaces, limited by cgroups. Replay the start of one web container on node-2, zoomed into the operating system: a directory of layers glued together, a set of clone() flags, and two files in a cgroup. No virtual machine anywhere.
- Container
- web · registry.example.com/shop/web:1.4.2
- Runtime
- containerd → runc (OCI)
- Isolation
- namespaces + cgroups v2 · one shared kernel
- Start to running
- ≈ 90 ms (image already on disk)
Read the whole lesson as text
1. The bundle
Zoom into node-2. The kubelet has already pulled the image and started the sandbox; now it asks containerd to create the app container. containerd resolves the image to its OCI config and hands the snapshotter a list of layer digests. The result is an OCI bundle: a rootfs directory and a config.json that describes exactly the process to run.
- The kubelet asks containerd to create the web container inside the sandbox.
- containerd's snapshotter glues the image's five layers into one directory tree.
2. The rootfs
The merged directory is an overlayfs mount: the five image layers stacked as read-only lower directories, one empty writable upper directory beside it. The app never sees any of that. It sees an ordinary filesystem: /app/server exists, /etc/hostname exists, and writes to /tmp land in the upper layer without touching the image.
- The layers are mounted as one tree, with one small writable layer on top.
3. Namespaces
containerd hands the bundle to runc, its OCI runtime. runc doesn't build a machine: it calls clone() once with a handful of flags, and the new process wakes up unable to see the host's PID tree or mount table. Three namespaces are deliberately not new: network, IPC and UTS belong to the sandbox's pause container, and runc joins them with setns(). That's why every container in the Pod shares one eth0, one localhost and one hostname.
- containerd calls runc, which will make the process itself.
- One system call gives the process its own PID tree and mounts; it joins the Pod's network, IPC and hostname.
- The new mount table's root swings over to the overlayfs, and the host's filesystem disappears from view.
4. cgroups
While it sets up the namespaces, runc also creates the container's cgroup and writes the limits from config.json as plain files. memory.max is the container's memory limit as a byte count; cpu.max is its CPU limit as a quota per period; the CPU request becomes cpu.weight, a relative share. Nothing watches these files. The kernel's scheduler and memory manager read them on every allocation decision, forever - that's the entire enforcement mechanism.
- The limits from the Pod spec become two files in a cgroup directory.
5. PID 1
Now runc execs the app. Inside the PID namespace it is PID 1 - the first process, the one that gets signals first. On the host it is PID 8412, an unremarkable /app/server process that ps can see. Both are true at once: same process, two numbering systems. It shares the sandbox's network namespace, so eth0 with 10.244.2.17 is already configured and waiting.
- The app starts: PID 1 inside, ordinary process 8412 on the host.
- The app binds its port on the shared eth0. Forty milliseconds after exec, it is serving.
6. Not a VM
Every container on this node runs on the same kernel. Namespaces hide views; cgroups ration resources; and two more mechanisms trim what the process may ask the kernel to do: capabilities (no CAP_SYS_ADMIN, no CAP_NET_ADMIN here) and seccomp (the RuntimeDefault profile blocks a few dozen of Linux's 300-plus syscalls; this Pod asks for it in its securityContext, since Kubernetes otherwise runs containers unconfined). Exit means gone - when PID 1 exits, the container is over and the kernel reaps everything.
- Just before execve, runc had dropped capabilities and loaded the syscall filter. This is what the app was left with.
- The kubelet reports the container Running. Ready comes later, after the probe.
Summary
A container is three cheap tricks stacked: an overlayfs so a pile of shared layers looks like /, one clone() call with the right flags so the process can't see the host, and two cgroup files so the kernel rations it. The image pull was the slow part (an earlier lesson); becoming a process took 90 ms.
Next lesson: Pulling an image