docker resource control cgroup

1. CPU control

cgroups is a very powerful linux kernel tool. It can not only limit the resources isolated by namespace,

You can also set weights for resources, calculate usage, control process start and stop, and more. So cgroups (Control groups) realize the quota and measurement of resources.

Cgroups have four major functions:

  1. Resource limit: You can limit the total amount of resources used by the task;

  2. Priority allocation: through the number of allocated cpu time slices and the size of disk I0 bandwidth, it is actually equivalent to controlling the task running priority;

  3. Resource statistics: You can count the resource usage of the system, such as cpu duration, memory usage, etc.;

  4. Task control: cgroup can perform operations such as suspending and resuming tasks.

1. Set the upper limit of CPU usage

Linux uses CFS (Completely Fair Scheduler, Completely Fair Scheduler) to schedule the use of the CPU by each process. The default scheduling period of CFS is 100ms.

You can set the scheduling cycle of each container process, and how much CPU time each container can use at most during this cycle.

Use --cpu-period to set the scheduling period, and use --cpu-quota to set the CPU time that the container can use in each cycle. The two can be used together.

The effective range of the CFS period is 1ms~1s, and the corresponding value range of --cpu-period is 1000~1000000.

The CPU quota of the container must not be less than 1ms, that is, the value of --cpu-quota must be >= 1000.

View CPU usage

docker run -itd --name test centos:7 /bin/bash

cd /sys/fs/cgroup/cpu/docker/98804287283ce0f6abe8a19c9884c6b5149acc5a050a767e                      9f5f4bac5472ef56/

cat cpu.cfs_period_us cpu.cfs_quota_us
100000
-1

#cpu.cfs_period_us: The period of cpu allocation (microseconds, so the file name is represented by us), the default is 100000.

#cpu.cfs_quota_us: Indicates the time (in microseconds) taken by the control group limit, the default is -1, which means no limit. If it is set to 50000, it means that 50000/100000=50% of the CPU is occupied.

Run a CPU stress test

docker exec -it 98804287283c /bin/bash
vim /cpu.sh
#!/bin/bash
i=0
while true
do
let i++
done
 
chmod +x /cpu.sh
./cpu.sh
exit
 
top  


 

Set CPU usage

#设置50%的比例分配CPU使用时间上限
docker run -itd --name test2 --cpu-quota 50000 centos:7 /bin/bash   #可以重新创建一个容器并设置限额
或者
cd /sys/fs/cgroup/cpu/docker/98804287283ce0f6abe8a19c9884c6b5149acc5a050a767e9f5f4bac5472ef56
echo 50000 > cpu.cfs_quota_us
docker exec -it 98804287283c /bin/bash
./cpu.sh
exit
 
top                                                #可以看到cpu占用率接近50%,cgroups对cpu的控制起了效果

2. Set the CPU resource usage ratio (only valid when multiple containers are set)

Docker specifies the CPU share through --cpu-shares, the default value is 1024, and the value is a multiple of 1024.

#Create two containers as c1 and c2. If there are only these two containers, set the weight of the container so that the CPU resources of c1 and c2 account for 1/3 and 2/3.

docker run -itd --name test1 --cpu-shares 1024 centos:7
docker run -itd --name test2 --cpu-shares 512 centos:7
 
#分别进入容器,进行压力测试
docker exec -it 2e71bd7f3c4c bash
yum install -y epel-release
yum install stress -y
stress -c 4                 #产生四个进程,每个进程都反复不停的计算随机数的平方根
 
#查看容器的运行状态(动态更新)
docker stats

3. Set the container to bind the specified CPU

#先分配虚拟机4个CPU核数
docker run -itd --name test2 --cpuset-cpus 1,3 centos:7 /bin/bash
 
#进入容器,进行压力测试
yum install -y epel-release
yum install stress -y
stress -c 4
exit
 
#退出容器,执行 top 命令再按 1 查看CPU使用情况。

2. Limit memory usage

1. Create a container with specified physical memory

-m(--memory=)选项用于限制容器可以使用的最大内存
docker run -itd --name test4 -m 512m centos:7 /bin/bash
 
docker stats

2. Create a container with specified physical memory and swap

docker run -itd --name gxd7 -m 512m --memory-swap 1g centos:7 /bin/bash
 
强调一下,--memory-swap是必须要与--memory一起使用的
 
正常情况下,--memory-swap的值包含容器可用内存和可用swap
所以-m 300m --memory-swap=1g 的含义为:容器可以使用300M 的物理内存,并且可以使用700M (1G - 300M)的swap
 
如果--memory-swap设置为0或者不设置,则容器可以使用的swap大小为-m值的两倍
如果--memory-swap的值和-m值相同,则容器不能使用swap
如果--memory-Swap值为-1,它表示容器程序使用的内存受限,而可以使用的swap空间使用不受限制(宿主机有多少,swap容器就可以使用多少)

3. Restrictions on disk IO quota control (blkio)

3.1 Setting restrictions

--device-read-bps:限制某个设备上的读速度bps(数据量),单位可以是kb、mb(M)或者gb。
例:docker run -itd --name test1 --device-read-bps /dev/sda:1M  centos:7 /bin/bash
 
--device-write-bps : 限制某个设备上的写速度bps(数据量),单位可以是kb、mb(M)或者gb。
例:docker run -itd --name test1 --device-write-bps /dev/sda:1mb centos:7 /bin/bash
 
--device-read-iops :限制读某个设备的iops(次数)
  
--device-write-iops :限制写入某个设备的iops(次数)

3.2 Create a container and limit the writing speed

3.3 Verify write speed by dd

docker exec -it test1 bash                       #进入容器
dd if=/dev/zero of=test.out bs=1M count=50 oflag=direct #添加oflag参数以规避掉文件系统cache

4. Clean up the disk space occupied by docker

docker system prune -a  #可以用于清理磁盘,删除关闭的容器、无用的数据卷和网络

Guess you like

Origin blog.csdn.net/weixin_69148277/article/details/131143310