what is namespace cgroups

Containers 101: What is namespaces and cgroups in Linux

Spread the love

Namespaces and cgroups are two fundamental Linux kernel features that enable containerization by isolating resources and processes.

Namespaces

Namespaces provide isolation of system resources for processes. Each namespace gives a process (or group of processes) its own view of the system, such as its own process tree, network stack, or filesystem.

This isolation ensures that processes running in one namespace are invisible to those in another.

Types of Namespaces

PID Namespace

  • Isolates process IDs. Each container has its own process tree starting from PID 1.
  • For example, a process in one namespace cannot see or kill processes in another namespace.

Mount Namespace

  • Isolates the filesystem. Each container can have its own filesystem view, mount points, and directories.
  • Example: A container may see /app as its root, while the host system sees /var/lib/containers/app.

Network Namespace

  • Isolates network interfaces, IP addresses, and routing tables. Each container can have its own virtual network interfaces (e.g., veth pairs) and network stack.
  • Example: Containers communicate over isolated networks using virtual switches (like bridge or veth).

UTS Namespace

  • Isolates hostname and domain name. Containers can have their own hostname without affecting the host system.
  • Example: A container may think its hostname is web-app even though the host machine has a different name.

IPC Namespace

  • Isolates inter-process communication (e.g., message queues, shared memory) so processes in one container cannot interfere with processes in another.

User Namespace

  • Isolates user and group IDs. A process in a container can run as root (UID 0) inside the container but be mapped to an unprivileged user on the host.

cgroup Namespace

  • Isolates the view of control groups (cgroups), preventing containers from seeing cgroups of the host or other containers.

cgroups (Control Groups)

cgroups are responsible for resource management and allocation. They limit, prioritize, and monitor system resources like CPU, memory, disk I/O, and network bandwidth for groups of processes.

Features of cgroups

Resource Limiting

  • Restrict the amount of CPU, memory, or other resources a container can use.
  • Example: A container can be restricted to only 512MB of RAM or 20% of CPU time.

Prioritization

  • Assign higher or lower priority to certain containers for resource access.
  • Example: A critical application container can be given more CPU cycles than a background job container.

Accounting

  • Track how much of each resource is being used by processes in a cgroup.
  • Example: Monitoring the memory usage of a container for debugging or billing purposes.

Isolation

  • Prevent containers from interfering with each other’s resource usage.
  • Example: Prevent a memory-intensive container from exhausting all available memory on the host.

Device Control

  • Restrict access to certain devices (e.g., block a container from accessing a specific disk).

How Namespaces and cgroups Are Used in Containers

Namespaces and cgroups together form the backbone of containerization. Here’s how they make containers efficient and effective:

1. Process Isolation with Namespaces

  • Each container runs in its own PID namespace, so it cannot see or interfere with processes outside its namespace. This ensures that each container has its isolated environment.

2. Filesystem Isolation with Namespaces

  • Containers have their mount namespace, allowing them to see only the directories and files relevant to their operation. For instance, the host’s /etc directory may be replaced with a container-specific /etc.

3. Network Isolation with Namespaces

  • Network namespaces ensure that each container has its network stack. This makes it possible to assign virtual network interfaces (e.g., veth), private IP addresses, and routing tables specific to each container.

4. Resource Management with cgroups

  • cgroups ensures that containers cannot consume excessive system resources, preventing a single container from starving other containers or the host. For example:
  • Memory limits prevent memory leaks in one container from crashing the host.
  • CPU shares ensure fair scheduling among containers.

5. Scalability and Efficiency

  • By isolating processes, filesystems, and network resources with namespaces and enforcing resource limits with cgroups, containers can run lightweight applications side-by-side without the overhead of full virtual machines.

6. Security

  • User namespaces ensure that even if a container thinks it’s running as root, it is mapped to an unprivileged user on the host, minimizing the impact of security breaches.

Real-Life Example: Docker

When you run a container using Docker:

  1. Docker uses namespaces to create an isolated environment for processes, filesystems, and networking.
  2. Docker uses cgroups to limit the amount of memory, CPU, or other resources the container can use.
  3. Combined, namespaces and cgroups allow Docker to run multiple containers on the same host without interference or resource contention.
Summary
  • Namespaces: Provide isolation (processes, filesystems, networks, etc.).
  • cgroups: Provide resource management (limits, priorities, and monitoring).
  • Together, they enable lightweight, efficient, and secure containerized environments.

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
×