How to improve Docker performance with resource control using cgroups
Docker performance can be affected by a single container that abuses the CPU or memory, which can eventually end up crashing other containers or the entire machine.
Most hosting providers implement QoS (Quality of Service) to improve Docker performance. This includes limiting resources per container and monitoring their usage.
How resource usage impacts Docker performance
The main resources that are shared among containers in a Docker host includes memory, CPU and block I/O.
The resource usage of a container can be seen with the command ‘docker stats container-ID‘. It will list the CPU, memory and I/O usage for that container.
During less traffic hours, the resources allotted for the containers would be sufficient to manage the load in them and the Docker performance would be fine.
But when traffic peaks or one Docker container suddenly start abusing the resources in the host, the Docker performance would start getting affected.
To improve performance, its vital to limit the resources per container for QoS (Quality of Service). This can be achieved using the ‘cgroups’ (control groups) feature.
Today, we’ll discuss how to improve Docker performance by limiting the memory and CPU usage in containers using cgroups.
How to improve CPU performance for Docker containers
CPU usage plays a vital role in determining the Docker performance. The CPU share that is allotted to all containers by default is 1024.
If all the containers have this default value set, the available CPU will be shared among them equally. Without CPU limits, one container can use up all the available CPU.
Suppose you need to allot more CPU share to a container or limit the CPU for one container, based on their plans. This can be achieved by setting CPU weights.
In a machine with 3 containers, if one is assigned 1024 CPU share and others are assigned 512 CPU share, then first one will get 50% CPU and others 25% each.
The CPU shares would be divided among the containers based on the total number of containers and the CPU share value assigned to each.
To assign a CPU share to a container during creation or run-time, the ‘-c’ flag can be used with ‘docker run’ command. Here, a CPU share of 512 is assigned:
In multi-core machines, the CPU share has to be calculated by considering the number of CPUs also, along with the container count and usage plans.
It is also possible to allocate the CPU usage of a container to a certain CPU core, in a multi-core machine. Many more tweaking can be done based on the requirements.
The cgroups CPU metrics and stats are stored in the files inside the folder ‘/sys/fs/cgroup/cpuacct/docker/container-ID/’ as seen:
[ Are your spending too much time managing your Docker containers? Our Docker experts take care of your infrastructure and ensure its smooth functioning. ]
Bobcares provides Outsourced Hosting Support and Outsourced Server Management for online businesses. Our services include Hosting Support Services, server support, help desk support, live chat support and phone support.