Docker resource control management

1. CPU control

cgroups is a very powerful Linux kernel tool. It can not only limit resources isolated by namespace, but
also set weights for resources, calculate usage, control process start and stop, and so on. 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 period. The two can be used together.

The valid 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 test5 centos:7 /bin/bash
 
docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED         STATUS         PORTS     NAMES
85c7a6860f72   centos:7   "/bin/bash"   4 seconds ago   Up 4 seconds             test5
 
cd /sys/fs/cgroup/cpu/docker/85c7a6860f72d66e36c924977891c170c111fb86a6f9b263c2bce36b0415e61c/
 
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.
insert image description here
Run a CPU stress test

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

insert image description here
insert image description here
insert image description here
Set CPU usage

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

insert image description here
insert image description here

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
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O          BLOCK I/O        PIDS
2e71bd7f3c4c   test2     32.96%     1.562MiB / 976.3MiB   0.16%     25.5MB / 137kB   179MB / 25.2MB   1
ffbdd0ef2781   test1     65.86%     12.2MiB / 976.3MiB    1.25%     25.5MB / 102kB   122MB / 25.2MB   1

insert image description here
insert image description here

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使用情况。

insert image description here
insert image description here
insert image description here

2. Limit memory usage

1. Create a container with specified physical memory

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

insert image description here

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容器就可以使用多少)

insert image description here

3. Restrictions on disk IO quota control (blkio)

3.1 Setting restrictions

--device-read-bps:限制某个设备上的读速度bps(数据量),单位可以是kb、mb(M)或者gb。
例:docker run -itd --name gxd8 --device-read-bps /dev/sda:1M  centos:7 /bin/bash
 
--device-write-bps : 限制某个设备上的写速度bps(数据量),单位可以是kb、mb(M)或者gb。
例:docker run -itd --name gxd9 --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
insert image description here
3.3 Verify the writing speed through dd

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

insert image description here

Guess you like

Origin blog.csdn.net/weixin_59325762/article/details/130329305