interviewpath
  • 0day streak
  • 10total XP
  • Level 1
0/6

Unit 3 · OS & concurrency · Lesson 11 of 11

Containers, VMs, namespaces and cgroups

Containers and VMs are the units cloud platforms schedule, and their isolation comes from specific mechanisms: namespaces, cgroups and reduced privileges for containers, hardware virtualization for VMs. Knowing them explains CPU throttling, OOM kills and noisy neighbors, and how microVMs such as Firecracker combine VM isolation with container-like overhead.
About 3 h 30 minFocus mode

Step 1: Learn the ideas

  • Containers as processes: namespaces, cgroups, privileges, root filesystems
  • Namespaces: mount, PID, network, UTS, IPC, user, cgroup, time
  • cgroup v2 hierarchy and controllers
  • CPU weights, quotas and throttling
  • Memory limits, reclaim, page-cache charging and OOM kills
  • I/O and PID limits; pressure stall information
  • Capabilities, seccomp and user namespaces
  • Kubernetes requests and limits mapped to cgroups
  • Hardware virtualization: trap-and-emulate, nested page tables, virtio
  • MicroVMs (Firecracker) and sandboxed runtimes
  • What isolation does not cover: caches, memory bandwidth, the shared kernel

Why it matters

Cloud platforms schedule containers and VMs, and much puzzling behavior inside them traces back to isolation mechanisms: a service with a CPU limit shows latency spikes at low average CPU, a pod is OOM-killed while its heap looks small, or co-located workloads slow each other down. If your coursework covered Kubernetes, resource allocation or interference, this topic supplies the kernel mechanisms behind those observations. Infrastructure, SRE and cloud-research interviews expect you to explain them and to compare containers with VMs and microVMs on isolation and overhead.

Core ideas

A container is a process. The Linux kernel has no container object. A runtime starts ordinary processes with four ingredients: namespaces (what the process can see), cgroups (what it can use), reduced privileges (capabilities, seccomp system-call filters, security modules) and its own root filesystem, usually image layers stacked with an overlay filesystem. All containers on a host share one kernel.

Namespaces give a process its own view of one kind of global resource: mount points, process IDs (the first process inside becomes PID 1), the network stack (interfaces, routes, ports), the hostname (UTS), System V IPC, user and group IDs (user namespaces map root inside to an unprivileged user outside), the cgroup tree and clocks.

cgroup v2 organizes processes in one hierarchy and attaches controllers:

  • CPU: cpu.weight divides CPU proportionally, and only matters under contention. cpu.max sets a hard quota per period, for example 50 ms every 100 ms for half a CPU. A multithreaded process can burn its whole quota early in the period on several cores and is then throttled until the next period begins, which produces latency spikes even when average usage looks low; cpu.stat counts throttled periods and time. Kubernetes CPU limits map to this quota and CPU requests to weights.
  • Memory: memory.max is a hard limit. Usage includes anonymous memory, page cache the group brought in and some kernel memory. Near the limit the kernel reclaims, clean page cache first; if it cannot reclaim enough, the OOM killer kills a process in that cgroup, visible as exit status 137 (SIGKILL) and in the oom_kill count in memory.events. memory.high throttles and reclaims earlier, before the hard limit.
  • I/O and PIDs: bandwidth or IOPS limits and weights, and a cap on the number of processes that stops fork bombs. Pressure stall information (PSI) reports how long tasks wait for CPU, memory or I/O.

What isolation does not cover. cgroups meter CPU time and memory size, but co-located workloads still share last-level cache, memory bandwidth, kernel data structures and locks, and often network and disk queues. These are the usual sources of noisy-neighbor interference. Some processors can partition cache and memory bandwidth, but that is not configured by default.

Virtual machines. A hypervisor runs guest kernels on virtualized hardware. Virtualization extensions let guest code run directly until it executes a sensitive instruction, which traps to the hypervisor; nested page tables translate guest-physical to host-physical addresses in hardware; paravirtual virtio devices replace slow device emulation. The isolation boundary is a narrow, hardware-enforced interface rather than the full system-call surface, so VMs isolate better, at the cost of a guest kernel's memory, slower boot and lower density.

MicroVMs and sandboxes. Firecracker is a minimal virtual machine monitor built on KVM with a very small device model, designed to boot quickly with little per-VM memory overhead so multi-tenant serverless platforms can give every function VM-grade isolation. gVisor intercepts system calls in a user-space kernel, and Kata Containers runs containers inside lightweight VMs. Each trades some performance for a smaller shared attack surface.

Common misconceptions

  • "A container is a lightweight VM." It is a set of processes sharing the host kernel; a kernel exploit can cross the boundary.
  • "A CPU limit of 1 means one dedicated core." It is a time quota across all cores per period; use cpusets for dedicated cores.
  • "Low average CPU means there is no CPU problem." Quota throttling within periods causes latency; check cpu.stat.
  • "The memory limit only counts my heap." Page cache, tmpfs files and some kernel memory are charged too.
  • "Root inside a container is harmless." Without user namespaces it is host root with fewer capabilities, and privileged containers or a mounted runtime socket can escape.
  • "VMs fully isolate performance." They still share caches, memory bandwidth and storage with other tenants.

How to approach it

Run the experiments in the practice guidance on a Linux VM; seeing throttling counters and an OOM kill makes the explanations concrete. For interviews, be ready to draw the container recipe (namespaces, cgroups, privileges, root filesystem), explain throttling with a timeline of quota periods, and argue when you would choose a container, a VM or a microVM for untrusted code.

Suggested order

  1. 1Read namespaces(7) and cgroups(7) for an overview of each namespace type and of the cgroup model.
  2. 2Read the cgroup v2 documentation sections on the CPU and memory controllers, then CFS Bandwidth Control for quotas and throttling.
  3. 3Read the Kubernetes page Resource Management for Pods and Containers and map requests and limits onto the cgroup files you just read.
  4. 4Read OSTEP Appendix B (Virtual Machine Monitors) for trap-and-emulate, memory virtualization and the information gap between hypervisor and guest.
  5. 5Read the Firecracker paper’s design and evaluation sections and note which overheads it removes and which isolation it keeps.
  6. 6Do the cgroup experiments in the practice guidance and record what each counter showed.

Step 2: Check off what you can do

Tap each one you can genuinely do or explain. Each earns 10 XP.

Step 3: Go deeper

  • CFS Bandwidth Control and cgroup v2 (Linux kernel documentation)Start here

    The Linux Kernel documentation (docs.kernel.org)

    Section: Control Group v2

    Primary specification of the CPU, memory, I/O and PID controllers and their statistics files; the entry’s main page, CFS Bandwidth Control, explains quota throttling.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • The Linux man-pages project (man7.org)Start here

    man7.org (Linux man-pages project)

    Section: namespaces(7)

    Overview of every namespace type and the related system calls (clone, unshare, setns); cgroups(7) is the companion page.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Operating Systems: Three Easy Pieces

    University of Wisconsin–Madison (free online book)

    Section: Appendix B: Virtual Machine Monitors

    How a hypervisor virtualizes the CPU and memory, and why VMs and containers draw the isolation boundary differently.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Firecracker: Lightweight Virtualization for Serverless Applications

    USENIX

    Case study of a microVM built for multi-tenant serverless isolation with low overhead; focus on the design rationale and the evaluation setup.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Kubernetes components (official documentation)

    Kubernetes project

    Section: Resource Management for Pods and Containers

    How Kubernetes requests and limits translate into scheduling decisions and cgroup settings.

    Open(opens in a new tab)Link responded, 5 Oct 2026

Step 4: Practice

Use a Linux VM with cgroup v2 (check that /sys/fs/cgroup/cgroup.controllers exists) and work as root or in a delegated subtree. (1) Create a cgroup, set memory.max to 100M (and memory.swap.max to 0 if the VM has swap), run a program that allocates and touches 300 MB, and find the OOM kill in memory.events and the kernel log; repeat with memory.high instead and watch it throttle. (2) Set cpu.max to "20000 100000", run a four-thread busy loop inside, and read nr_throttled and throttled_usec from cpu.stat; then compare cpu.weight between two busy cgroups. (3) Start a shell with unshare --pid --fork --mount-proc and run ps. (4) Repeat (1) and (2) with docker run --memory and --cpus and find the same files from the host. Record what each counter showed rather than relying on remembered numbers.

No external problems are linked; follow the guidance above.

Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).

Step 5: Test yourself

Answer each one out loud or on paper before you finish the lesson.

  1. Question 1Which namespace makes a process believe it is PID 1, and what responsibilities come with being PID 1?
  2. Question 2What is the difference between cpu.weight and cpu.max?
  3. Question 3Why can page cache push a container to its memory limit?
  4. Question 4What does exit status 137 usually indicate for a container?
  5. Question 5Which shared resources can still cause interference between containers with strict CPU and memory limits?
  6. Question 6What does Firecracker give up compared with a general-purpose VMM, and what does it gain?

You’re done when you can

  • Explain the container recipe (namespaces, cgroups, privileges, root filesystem) and the VM alternative in under three minutes.
  • Reproduce CPU quota throttling and read it from cpu.stat.
  • Reproduce an OOM kill in a memory-limited cgroup and explain what was charged to the cgroup.
  • Explain why a CPU limit can raise tail latency even at low average utilization.
  • Argue for a container, a VM or a microVM for running untrusted code, citing the isolation boundary and the overhead.

Finish the lesson

How confident do you feel?

Lesson complete!

+50 XP

Reviews will come up on the Practice tab so it sticks.

Unlocks next: Kubernetes and container orchestration, Cloud paper route: Firecracker and cluster management

Your notes and study time

Log study time