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 to restrict the use of CPU by containers

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article to share with you is about how to limit the use of containers on the CPU, Xiaobian feel quite practical, so share to everyone to learn, I hope you can read this article after some gains, not much to say, follow Xiaobian to see it.

By default, all containers can use host CPU resources equally and without restrictions.

Docker can set the weight of CPU usage for containers via-c or--cpu-shares. If not specified, the default value is 1024.

Unlike memory quotas, cpu share set by-c is not an absolute amount of CPU resources, but a relative weight. The amount of CPU resources a container can eventually allocate depends on the ratio of its CPU share to the total CPU share of all containers.

In other words: CPU share allows you to set the priority of CPU usage for containers.

For example, two containers are started in host:

docker run --name "container_A" -c 1024 ubuntu

docker run --name "container_B" -c 512 ubuntu

The cpu share of container_A is 1024, which is twice that of container_B. When both containers require CPU resources, container_A gets twice as much CPU as container_B.

It is important to note that this weighted CPU allocation only occurs when CPU resources are tight. If container_A is idle, container_B can also be allocated to all available CPUs in order to make full use of CPU resources.

Let's continue with the experiment with progress/stress.

Start container_A, cpu share is 1024

CPU is used to set the number of worker threads. Because the current host has only one CPU, one worker thread can overload the CPU. If the host has multiple CPUs, increase the number of--CPUs accordingly.

Start container_B, cpu share is 512:

Run top in host to see how the container uses CPU

Container_A consumes twice as much CPU as container_B.

Now pause container_A

top shows that container_B can use up the entire CPU when container_A is idle:

The above is how to limit the use of containers on CPU, Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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