What are cgroups v2?

cgroups v2 is the next generation of the Linux control groups feature. It provides a unified hierarchy and improved resource management capabilities compared to cgroups v1. cgroups v2 offers better performance and more fine-grained control over resource allocation in containerized environments.

In the realm of software engineering, containerization and orchestration are key concepts that are integral to the development, deployment, and management of applications. One of the most important tools in this domain is cgroups v2, a Linux kernel feature that limits, accounts for, and isolates the resource usage (CPU, memory, disk I/O, network, etc.) of process groups. This article will delve into the intricacies of cgroups v2, exploring its definition, history, use cases, and specific examples.

Understanding cgroups v2 requires a good grasp of containerization and orchestration. Containerization is the process of encapsulating an application and its dependencies into a container, which can run uniformly across different computing environments. Orchestration, on the other hand, involves managing the lifecycles of containers, particularly in large, dynamic environments. Now, let's dive into the world of cgroups v2.

Definition of cgroups v2

cgroups v2, also known as control groups version 2, is a Linux kernel feature that provides a mechanism for aggregating/partitioning sets of tasks, and all their future children, into hierarchical groups with specialized behaviour. In simpler terms, it's a way to manage and monitor the distribution of system resources among different processes running on a Linux system.

One of the key aspects of cgroups v2 is its hierarchical nature. This means that groups can have subgroups, which can have their own subgroups, and so on. Each group and subgroup can have different resource limits and isolation settings, providing a high degree of flexibility and control over system resources.

Components of cgroups v2

cgroups v2 is composed of several components, each serving a specific function. The primary components include the cgroup hierarchy, cgroup controllers, and the cgroup interface.

The cgroup hierarchy is a tree-like structure where each node represents a cgroup. Each cgroup in the hierarchy can have its own resource limits and isolation settings. The cgroup controllers, on the other hand, are kernel components that enforce the resource limits and isolation settings for each cgroup. Finally, the cgroup interface is the means by which userspace programs interact with cgroups v2, typically via the file system.

History of cgroups v2

The concept of cgroups was first introduced in the Linux kernel 2.6.24, released in 2008. The initial version, now referred to as cgroups v1, was designed to provide a way to limit and isolate resource usage of process groups. However, cgroups v1 had several shortcomings, including a complex and inconsistent interface, and lack of a unified hierarchy.

In response to these issues, cgroups v2 was proposed and eventually integrated into the Linux kernel 4.5, released in 2016. cgroups v2 addressed many of the shortcomings of its predecessor, introducing a simplified and consistent interface, a unified hierarchy, and improved resource management capabilities.

Evolution of cgroups v2

Since its introduction, cgroups v2 has undergone several improvements and enhancements. These have primarily focused on improving resource management capabilities, enhancing the interface, and adding new features.

For instance, one of the major improvements in cgroups v2 is the introduction of pressure stall information (PSI), which provides a way to measure and monitor resource pressure on a system. This has been particularly useful in detecting and mitigating resource starvation scenarios.

Use Cases of cgroups v2

cgroups v2 has a wide range of use cases, particularly in the realm of containerization and orchestration. It is used by container runtimes, such as Docker and Kubernetes, to limit and isolate resource usage of containers, and to provide a way to monitor and manage system resources.

For instance, in a Kubernetes cluster, cgroups v2 can be used to ensure that each pod (a group of one or more containers) gets its fair share of system resources, and that no single pod can monopolize the resources of the entire system. This is crucial in maintaining the stability and performance of the cluster.

Container Runtimes

Container runtimes, such as Docker and containerd, use cgroups v2 to manage the resource usage of containers. This includes setting resource limits for each container, isolating the resource usage of different containers, and monitoring the resource usage of containers.

For example, when you run a Docker container with a specific memory limit, Docker uses cgroups v2 to enforce this limit. If the container tries to use more memory than the limit, it will be throttled or even killed, depending on the configuration.

Orchestration Systems

Orchestration systems, such as Kubernetes and Mesos, use cgroups v2 to manage the resource usage of pods and services. This includes setting resource limits for each pod or service, isolating the resource usage of different pods or services, and monitoring the resource usage of pods and services.

For instance, in a Kubernetes cluster, the kubelet (the agent that runs on each node in the cluster) uses cgroups v2 to enforce the resource limits specified in the pod specifications. This ensures that each pod gets its fair share of system resources, and that no single pod can monopolize the resources of the entire node.

Examples of cgroups v2 Usage

Let's delve into some specific examples of how cgroups v2 is used in practice. These examples will illustrate how cgroups v2 is used to manage and monitor system resources in the context of containerization and orchestration.

Consider a scenario where you're running a Docker container with a specific CPU and memory limit. When you run the container, Docker uses cgroups v2 to create a new cgroup for the container, and sets the CPU and memory limits for the cgroup based on the limits specified in the Docker command. Docker then starts the container process inside this cgroup. As the container runs, the Linux kernel enforces the CPU and memory limits based on the settings of the cgroup.

Example: Docker

Let's consider a more specific example with Docker. Suppose you're running a Docker container with the following command:

docker run -d --cpus="1.5" --memory="1g" my-app

In this command, the --cpus and --memory options specify the CPU and memory limits for the container, respectively. When you run this command, Docker uses cgroups v2 to create a new cgroup for the container, and sets the CPU and memory limits for the cgroup based on these options. Docker then starts the container process inside this cgroup, and as the container runs, the Linux kernel enforces the CPU and memory limits based on the settings of the cgroup.

Example: Kubernetes

Now, let's consider an example with Kubernetes. Suppose you have a Kubernetes pod specification with the following resource limits:

resources:  limits:    cpu: "2"    memory: "1Gi"

In this specification, the cpu and memory fields under the limits section specify the CPU and memory limits for the pod, respectively. When you create this pod, the kubelet uses cgroups v2 to create a new cgroup for the pod, and sets the CPU and memory limits for the cgroup based on these fields. The kubelet then starts the pod's containers inside this cgroup, and as the containers run, the Linux kernel enforces the CPU and memory limits based on the settings of the cgroup.

Conclusion

cgroups v2 is a powerful tool for managing and monitoring system resources in the context of containerization and orchestration. It provides a flexible and efficient way to limit and isolate resource usage of process groups, and to monitor the resource usage of these groups. Whether you're a software engineer working with Docker and Kubernetes, or a system administrator managing a Linux server, understanding and leveraging cgroups v2 can be immensely beneficial.

As we've seen, cgroups v2 is used extensively in container runtimes and orchestration systems to manage the resource usage of containers and pods. It's also used in many other contexts, such as in cloud computing platforms to manage the resource usage of virtual machines, and in high-performance computing environments to manage the resource usage of compute jobs. With its powerful capabilities and wide range of use cases, cgroups v2 is a key component of the Linux kernel, and a cornerstone of modern computing infrastructure.

High-impact engineers ship 2x faster with Graph
Ready to join the revolution?
High-impact engineers ship 2x faster with Graph
Ready to join the revolution?

Code happier

Join the waitlist