Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 17 of 8021. Container FundamentalsFree lesson

Limiting CPU Usage with Cgroup Subsystems

Learners will configure CPU Cgroup controllers to enforce strict limits on the processing power allocated to specific container workloads.

In this lesson, you will learn how to use Cgroup (Control Group) CPU subsystems to prevent a single container from monopolizing your host’s processing power.

The Problem: The "Noisy Neighbor"

In our previous lesson, we learned that Cgroups allow us to group processes together to track their resource consumption. Without active management, a single container running a heavy task—like video encoding or a complex data calculation—could consume 100% of your CPU cycles. This would cause other containers on the same host to slow down or crash, creating a "noisy neighbor" effect.

The Solution: CPU Cgroup Controllers

The Linux kernel provides a cpu controller within the Cgroup framework that acts as a "traffic cop" for your processor. By configuring this controller, you tell the kernel exactly how much time a specific group of processes is allowed to spend on the CPU over a set period.

When you launch a container, the container engine interacts with this subsystem to set a "quota." Even if a program inside the container is written to use every available core, the kernel will pause that container once it hits its limit, ensuring that other processes on your host remain responsive.

Practical Application

When you run a container, you can define these limits using simple flags. For example, if you want to limit a container to use exactly 50% of a single CPU core, you would use a command structure similar to this:

# Limiting a container to 50% of one CPU cycle
docker run --cpus="0.5" my-container-app

Internally, Docker creates a directory under /sys/fs/cgroup/cpu/ for your container. It then writes values into the cpu.cfs_quota_us file. By adjusting these kernel-level files, the Cgroup subsystem forces the container to wait for the next time slice once it has exhausted its allocated allowance, effectively "throttling" its speed. This ensures your infrastructure remains predictable, regardless of what the application code is doing inside the container.

Key Takeaway: CPU Cgroup subsystems allow you to enforce strict processing limits on containers, preventing any single workload from starving your host system of necessary compute resources.