Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Docker restricts the CPU available to the container

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you how Docker limits the CPU available for containers, I hope you have something to gain after reading this article, let's discuss it together!

Limit the number of CPUs available

On docker 1.13 and later, it is easy to limit the number of host CPUs a container can use. Simply specify the number of CPUs the container can use with the--cpus option, and you can also specify fractions such as 1.5. Next, let's perform a demo on a low-load mainframe with four CPUs:

Create a container with the following command, --cpus=2 means the container can use up to two CPUs on the host:

$ docker run -it --rm --cpus=2 u-stress:latest /bin/bash

Four busy processes are then created by the stress command that consume CPU resources:

# stress -c 4

Let's look at the docker stats command output:

Container CPU load is 200%, which means twice the load of a single CPU. We can also think of it as having two CPUs working 100% for it.

Let's look at the real load of the host CPU through the top command:

Haha, a bit surprising! The actual situation is not that two CPUs are 100% loaded and the other two are 0% loaded. The load of the four CPUs is 50%, and the total CPU consumption of the container adds up to 100% of the load of the two CPUs.

It seems that there is no concept of CPU number for a process, and the kernel can only count the percentage of CPU consumed by the process by CPU time slices. This is also why percentages are used to describe CPU usage in various tools we see.

To be precise, let's look at how docker's official documentation explains the--cpus option:

Specify how much of the available CPU resources a container can use.

Sure enough, people use "how much", uncountable! And--cpus option support set to decimal also from the side to explain the CPU measurement can only be a percentage.

It seems that the "CPU number" written by the author in this article is inaccurate. If it's inaccurate, why use it? For easy understanding, of course. Moreover, the author thinks that in the context of the--cpus option, it is understood as "the number of CPUs" and there is no problem (interested students can read the origin of the--cpus option, the original intention of others is also to indicate the number of CPUs).

Although the--cpus option is nice to use, it was only supported in 1.13. For earlier versions to do the same we need to use two options together: --cpu-period and--cpu-quota(both options are still supported in versions 1.13 and later). The following command achieves the same result:

The copy code is as follows:

$ docker run -it --rm --cpu-period=100000 --cpu-quota=200000 u-stress:latest /bin/bash

This configuration option is not very dumbfounded ah! What is 100000? What is 200000? They are measured in microseconds, with 100000 representing 100 milliseconds and 200000 representing 200 milliseconds. What they mean here is that for every 100 ms, the CPU time used to run a process is at most 200 ms (two CPUs are required to execute for 100 ms each). For those who want to understand these two options, please refer to CFS BandWith Control. We need to know that these two options are the truth, but the truth is often cruel! Fortunately, the--cpus option successfully saved us. In fact, it just wrapped--cpu-period and--cpu-quota.

Specify a fixed CPU

With the--cpus option we can't always have containers running on one or several CPUs, but with the--cpuset-cpus option we can! This makes sense because in today's multicore systems, each core has its own cache, and frequent scheduling processes on different cores will inevitably lead to overhead such as cache invalidation. Here we demonstrate how to set the container to use a fixed CPU. The following command sets the--cpuset-cpus option for the container and specifies the CPU number on which the container runs as 1:

$ docker run -it --rm --cpuset-cpus="1" u-stress:latest /bin/bash

Restart pressure test command:

# stress -c 4

Then look at the load on the host CPU:

This time only CPU1 reached 100%, the rest of the CPU was not used by the container. We can execute stress -c4 repeatedly, but CPU1 always does the work.

Looking at the CPU load of the container, it is also only 100%:

The--cpuset-cpus option can also specify multiple CPUs at once:

$ docker run -it --rm --cpuset-cpus="1,3" u-stress:latest /bin/bash

This time we specify two CPUs 1 and 3, run the stress -c 4 command, and then check the CPU load of the host:

Both Cpu1 and Cpu3 were loaded to 100%.

The CPU load of the container also reached 200%:

One disadvantage of the--cpuset-cpus option is that you must specify the CPU number in the operating system, which is inconvenient in a dynamically scheduled environment (you can't predict which hosts the container will run on, you can only dynamically detect the CPU number in the system through the program and generate the docker run command).

Set weights for CPU usage

When CPU resources are sufficient, it makes no sense to set CPU weights. CPU weights allow different containers to be allocated different CPU usage only if containers compete for CPU resources. -- The cpu-shares option is used to set the CPU weight, which defaults to 1024. We can set it to 2 for a very low weight, but to 0 for a default of 1024.

Let's run two containers separately, specifying that they both use Cpu0, and setting--cpu-shares to 512 and 1024, respectively:

$ docker run -it --rm --cpuset-cpus="0" --cpu-shares=512 u-stress:latest /bin/bash$ docker run -it --rm --cpuset-cpus="0" --cpu-shares=1024 u-stress:latest /bin/bash

Run stress -c 4 in both containers.

At this point the load on host CPU0 is 100%:

The load on the CPU in the container is:

Two containers share a CPU, so the total should be 100%. The specific load allocated to each container depends on the setting of the--cpu-shares option! Our settings are 512 and 1024, respectively, so they share a ratio of 1:2. In this case, if you want both containers to be 50% each, you just need to set the--cpu-shares option to the same value.

After reading this article, I believe you have a certain understanding of "Docker how to limit the CPU available for containers". If you want to know more about it, please pay attention to the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report