Virtualisation and Containers — Hypervisors, Namespaces and cgroups

Type 1 and type 2 hypervisors, how containers use namespaces and cgroups, and what that means for Docker and Kubernetes.

Est. 40 min Updated Suggest a fix
// Part 01 — Why isolate workloads

One Machine Can Host Many Environments

A server may run a payment API, a reporting worker, and a database at once. They need separate dependencies, identities, and resource boundaries even though they share physical CPU, memory, storage, and network hardware. Virtualisation creates an abstraction between software and hardware; the abstraction can be a whole virtual machine (VM) or a process-level container.

Isolation has degrees, not a magical on/off switch. A VM has a guest operating system and a virtual hardware boundary managed by a hypervisor. A container normally shares the host kernel while presenting an isolated view of selected operating-system resources. Both still depend on the host, configuration, and privileges they are given.

PropertyVirtual machineContainer
KernelGuest kernel per VMUsually shares the host kernel
Isolation boundaryVirtual hardware and hypervisorProcess isolation features of the host OS
Startup and footprintIncludes guest OS lifecycle and resourcesTypically lighter; starts as host processes
Operating systemsCan run a compatible guest OS independently of host user spaceRequires host-kernel compatibility
Best fitStrong environment separation and mixed guest OS needsPortable application packaging and dense services
💡 Note
A container is not a tiny VM. It is a process with a constrained and namespaced view of the host OS, packaged with files and configuration. That distinction explains both its speed and its shared-kernel risk.
// Part 02 — Hypervisors

The Hypervisor Shares Physical Hardware

A hypervisor allocates virtual CPUs, memory, devices, and storage to guest operating systems. A type 1 hypervisor runs directly on hardware or in a hardware-oriented privileged layer. A type 2 hypervisor runs as an application on a host operating system. Product terminology can vary, but the useful distinction is where the virtualization control layer sits and what software is between it and the hardware.

The guest OS schedules its own processes, while the hypervisor schedules virtual CPUs onto physical CPUs. A guest may believe it owns a device that is actually virtual; the hypervisor or host maps that device to physical resources. Overcommitting CPU or memory can improve utilisation but may cause contention. A VM with a large configured memory limit still needs physical capacity somewhere on the host.

  1. The VM manager creates virtual hardware and attaches an OS image.
  2. The guest boots its own kernel and starts its services.
  3. Guest CPU and I/O requests are scheduled through the virtualization layer.
  4. The host monitors contention, capacity, and VM health across guests.
🎯 Pro Tip
When debugging a VM, separate guest symptoms from host symptoms. A guest can show spare memory inside itself while the physical host is under pressure, or the reverse if guest limits are too tight.
// Part 03 — Container model

An Image Is a Template; a Container Is a Running Process

A container image is a set of filesystem layers plus metadata such as its entry point. A running container adds a process, configuration, network attachment, and writable runtime state. Image layers can be reused across instances, which makes deployment efficient, but mutable container state should not be mistaken for durable application data.

container-lifecycle.sh
bash
# Build an image from the Dockerfile in the current project directory.
docker build -t study-api:local .

# Start one short-lived container and remove it when the process exits.
docker run --rm study-api:local

# List running containers; this does not change their state.
docker ps

Containers are useful for repeatable packaging, local development, and service deployment. They do not automatically provide a deployment strategy, secrets system, network policy, backups, or safe patching. Those concerns belong in the surrounding platform and operational design.

// Part 04 — Namespaces

Namespaces Change What a Process Can See

Linux namespaces give a process a view of selected global resources. Depending on which namespaces are configured, processes can see an isolated process ID space, network stack, mount view, hostname, or user ID mapping. Namespaces affect visibility and identity; they do not by themselves guarantee a CPU or memory limit. A process may have a separate PID view while still competing for host CPU.

inspect_namespaces.py
python
from pathlib import Path

namespace_dir = Path("/proc/self/ns")
if not namespace_dir.exists():
    raise SystemExit("This example requires a Linux procfs mount")

for entry in sorted(namespace_dir.iterdir()):
    try:
        print(f"{entry.name}: {entry.resolve()}")
    except OSError as error:
        print(f"{entry.name}: unavailable ({error})")

On Linux, this read-only example shows namespace links for the current process. The identifiers are useful for comparison, not for granting access. Container runtimes combine namespaces with filesystem setup, capabilities, security policies, and resource controls. Do not infer the complete security boundary from a process listing alone.

// Part 05 — cgroups

cgroups Account for and Limit Resource Use

Control groups (cgroups) organise processes so the kernel can account for and control resource use. A runtime can apply CPU shares or quotas, memory limits, process-count limits, and I/O controls when supported by the host. A namespace answers "what can this process see?"; a cgroup answers questions such as "how much of this resource may this group consume?" The exact interfaces differ between cgroup versions and distributions.

ControlWhat it helps withWhat to monitor
CPUAvoid one service monopolising computeThrottling and latency under load
MemoryBound a process group's memory useWorking set, pressure, and out-of-memory events
Process countLimit excessive child process creationCurrent count and rejected process starts
Block I/OManage storage access where supportedQueue delay and application response time
CPU / memory accountingAttribute resource use to a workloadUsage trends and host contention

Limits must fit the workload. A strict memory ceiling can protect other services, but an undersized limit can cause restarts or application failures. A CPU limit can improve fairness while extending response time during a burst. Measure actual demand and alert on throttling, pressure, and restarts instead of assuming a configured limit is safe.

// Part 06 — Container configuration

Set Boundaries Deliberately

This example starts a short-lived container with resource bounds and a reduced privilege surface. It assumes the image exists locally and its default command is appropriate. Review the image and workload before using flags in a real environment; a read-only root filesystem, for example, requires the application to write only to explicitly mounted locations.

bounded-container.sh
bash
docker run --rm \
  --memory=256m \
  --cpus=1 \
  --pids-limit=100 \
  --read-only \
  --cap-drop=ALL \
  --security-opt=no-new-privileges \
  study-api:local

Start from the narrowest capabilities and mounts the application needs, then add only justified access. Mounting the host filesystem broadly or running privileged erodes isolation. Use explicit volumes for required persistent state, and keep credentials in a secrets mechanism rather than baking them into an image layer.

⚠️ Important
Containers share the host kernel in the usual model. Do not run untrusted code in a container as though that made it equivalent to a separate physical security boundary; consider a stronger isolation model for high-risk workloads.
// Part 07 — Production operations

Images, State and Observability Are Part of the Boundary

Build images from maintained base images, pin and review dependencies through your normal release process, and scan for known issues. Run as a non-root identity when possible. Keep the image minimal, avoid shipping debugging tools that are not needed at runtime, and ensure the process handles termination signals so deployments can stop it cleanly.

Treat container filesystems as replaceable. Store important uploads, database files, and user-generated content in managed persistent storage with explicit backup and recovery plans. Emit logs to the platform's log pipeline and include health signals for the application, not only the container process. A process that is alive but cannot reach its database is not necessarily healthy.

  1. Define CPU, memory, process, network, and filesystem needs for each workload.
  2. Build a reproducible image and keep runtime configuration outside the image.
  3. Run with a dedicated identity and only required capabilities, mounts, and network access.
  4. Monitor resource pressure, restarts, errors, and end-to-end service health.
  5. Test image updates, graceful shutdown, persistent-data recovery, and rollback.
// Part 08 — Pitfalls

Common Virtualisation and Container Mistakes

Calling a container a lightweight VM

Why: The usual container model shares the host kernel.

Fix: Explain the shared-kernel boundary and select isolation to match workload risk.

Assuming namespaces impose resource limits

Why: Namespaces change resource views; cgroups handle accounting and controls.

Fix: Configure and monitor both isolation and resource policy.

Treating the container writable layer as durable storage

Why: Containers may be replaced during restart or deployment.

Fix: Put durable state in explicitly managed storage and test recovery.

Running every service as root with broad host mounts

Why: Convenience expands the impact of a compromised process.

Fix: Use a non-root identity, least privilege, narrow mounts, and explicit capabilities.

// Part 09 — Practice

Package a Small Service with Resource Limits

Practice

Reason about isolation before deployment

  1. Explain whether a VM or container is a better fit for two services that need different operating-system kernels.
  2. Run the namespace inspection script on Linux and describe what the output can and cannot prove.
  3. Choose CPU, memory, and process limits for a test worker, then identify the metrics needed to validate those limits.
  4. List the host mounts, network access, and Linux capabilities a hypothetical API actually requires; remove the rest.
  5. Design a restart test proving that application data survives container replacement and that shutdown is graceful.

Hint: Start with the threat model and data lifecycle. Isolation settings are useful only when their operational effects are understood.

🎯 Key Takeaways

  • ✓A VM virtualises hardware and runs a guest kernel; a typical container shares the host kernel.
  • ✓Hypervisors allocate virtual hardware, and host contention still affects guest performance.
  • ✓Namespaces change a process view; cgroups account for and constrain resource consumption.
  • ✓Container images are templates, while running containers are replaceable processes with runtime configuration.
  • ✓Least privilege, narrow mounts, bounded resources, and maintained images reduce risk.
  • ✓Persistent data, logs, health checks, and recovery plans belong in the production design.
  • For implementation details, consult the Linux kernel documentation on namespaces and cgroups, and the documentation for your selected container runtime and hypervisor.

Next

What comes next

Isolation is one part of defence. Next, connect users, permissions, memory protection, and sandboxing into an OS security model.

Lesson 10 · Protection and Security in the OS →

HELP US IMPROVE

How was this lesson?

Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.

Choose your feedback

Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.

Share

Discussion

0

Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.

Continue with GitHub
Loading...