By default, the host CPU resources that the container can use are unlimited. As with the use of memory resources, if the CPU resources that can be used by the container are not limited, once the program in the container abnormally uses the CPU, it is likely to exhaust the CPU resources of the entire host, leading to greater disasters. This article will introduce how to limit the CPU resources that the container can use

1. Install sysstat, htop tool

yum -y install sysstat epel-release htop

2. Test (all cpu samples are sampled 10 times every 3 seconds)

mpstat -P ALL 3 10

image.png

Indicator description:

-user: In the internal time period, the CPU time in user mode (%), excluding nice processes with negative values, and the value is (usr/total)100
-nice: In the internal time period, the nice value is the CPU time (%) of the negative process, and the value is (nice/total)
100
-system: In the internal time period, the core time (%), the value is (system/total)100
-iowait: In the internal time period, the hard disk IO waiting time (%), the value is (iowait/total)
100
-irq: In the internal time period, the hard interrupt time (%), the value is (irq/total)100
-soft: In the internal time period, the soft interrupt time (%), the value is (softirq/total)
100
-idle: In the internal time period, the CPU is idle for any reason except waiting for disk IO operations. Idle time (%), the value is (idle/total)100
-intr/s: In the internal time period, the number of interrupts received by the CPU per second, the value is (intr/total)
100

htop view system indicators

image.png

3. Pull the agileek/cpuset-test mirror

agileek/cpuset-test gives an image for testing the CPU, the function is to use up the specified CPU resources

docker pull agileek/cpuset-test

image.png

4. Simulation test Docker limit container CPU usage

Generally use these two parameters: –cpu-period + –cpu-quota

These two setting items appear in pairs. Before you understand these two concepts, you need to know how to calculate the CPU proportion of the process in the Linux system. It can be simply understood as: in a unit of time (also called refresh time) , The time ratio of the process occupied by the CPU, here is only the ratio of a single CPU (the ratio of the process CPU usage), if it is a multi-core CPU, that is, the process CPU usage ratio/CPU core number = system CPU ratio

-cpu-period: refers to the refresh time, the unit is microseconds (us), the default value is 0.1 second, which is 100,000us
-cpu-quota: The occupancy time of the container, the unit is microseconds (us), the default is -1, that is, no limit

Pass the test description:

Experiment using 2 core CPU

The container cpu occupies 50%, because the 2-core CPU is used, the proportion of process CPU in the top is 50%, and the proportion of system CPU usage is 25%.

docker run -it --rm --cpu-quota=50000 --name=test agileek/cpuset-test

image.png

image.png

image.png

The container cpu occupies 100%, and the system CPU uses 50%

docker run -it --rm --cpu-quota=100000 --name=test agileek/cpuset-test

image.png

image.png

The CPU occupancy ratio of the container is 500 000/1 000 000=50%; the CPU can be used up to 0.5 seconds in 1 second; the maximum is not more than 50% of a core, and the system usage ratio is 25%

docker run -it --rm --cpu-period=1000000 --cpu-quota=500000 --name=test agileek/cpuset-test
点赞(0)

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部