前言:Cgroup是C ontrol group的简写,是Linux内核提供的一种限制使用物理资源的机制,这些资源主要包括CPU、内存、blkio。
docker run --cpu-quota 20000 容器名 //CPU的使用率限定为20%
或者
eco 20000 > /sys/fs/cgroup/容器ID/cou.cfs_quota_us
[root@promote ~]# docker ps -a //查看有一个容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7a05e06be4a nginx "nginx -g 'daemon of…" 6 minutes ago Up 5 minutes 80/tcp nice_agnesi
[root@promote ~]# cd /sys/fs/cgroup/cpu/docker/
[root@promote docker]# ls //可以看到ID就是之前查看的容器
c7a05e06be4a0b0916c3771dca9fa714dd5aa8f150438ee6275e7a28b8491e87 cpuacct.usage cpu.rt_runtime_us
cgroup.clone_children cpuacct.usage_percpu cpu.shares
cgroup.event_control cpu.cfs_period_us cpu.stat
cgroup.procs cpu.cfs_quota_us notify_on_release
cpuacct.stat
[root@promote docker]# cd c7a05e06be4a0b0916c3771dca9fa714dd5aa8f150438ee6275e7a28b8491e87/ //进入这个容器文件
[root@promote c7a05e06be4a0b0916c3771dca9fa714dd5aa8f150438ee6275e7a28b8491e87]# ls //可以看到配置文件cpu.cfs_quota_us
cgroup.clone_children cpuacct.stat cpu.cfs_period_us cpu.rt_runtime_us notify_on_release
cgroup.event_control cpuacct.usage cpu.cfs_quota_us cpu.shares tasks
cgroup.procs cpuacct.usage_percpu cpu.rt_period_us cpu.stat
[root@promote c7a05e06be4a0b0916c3771dca9fa714dd5aa8f150438ee6275e7a28b8491e87]# cat cpu.cfs_quota_us
-1 //此时可以看到值为-1表示没有限制,可以更改值来改变cpu的使用率
[root@promote ~]# docker run -itd --name test3 --cpu-quota 20000 centos:latest /bin/bash //创建一个容器并配置cpu使用率
67708ad38fb61f5b9d0e29bf5d7e18d773f1bc2c31f609854ac151d482ab35f6
[root@promote ~]# docker ps -a //查看容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
67708ad38fb6 centos:latest "/bin/bash" 5 seconds ago Up 5 seconds test3
[root@promote ~]# docker exec -it 67708ad38fb6 /bin/bash //进入容器
[root@67708ad38fb6 /]# yum install bc -y //安装计算工具
[root@67708ad38fb6 /]# echo "scale=5000;4*a(1)" | bc -l -q
3.141592653589793238462643383279502884197169399375105820974944592307\
。。。省略部分内容
//a是bc的一个内置函数,代表反正切arctan,由于tan(pi/4)=1,于是4*arctan(1)=pi -l 使用标准数据库 -q 不输出在界面
docker run --cpu-shares 1024 容器A
docker run --cpu-shares 1024 容器B
docker run --cpu-shares 2048 容器C
[root@promote ~]# docker run -itd --name c1 --cpu-shares 512 centos:7 /bin/bash
8b86214f3109234adb0dbef8d71ec22d45a6126f175d99deb9a844378e50fdc1
[root@promote ~]# docker run -itd --name c2 --cpu-shares 1024 centos:7 /bin/bash
2eaf798a0996fac872a83e2606da9f9f1d356bc5564878294899d263a83692d7
[root@promote ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2eaf798a0996 centos:7 "/bin/bash" 18 seconds ago Up 17 seconds c2
8b86214f3109 centos:7 "/bin/bash" 36 seconds ago Up 35 seconds c1
[root@promote ~]# docker stats
[root@promote ~]# docker exec -it d652bfde0835 /bin/bash
[root@d652bfde0835 /]# yum install epel-release -y //epel源用来装压测工具
[root@d652bfde0835 /]# yum install stress -y //产生线程的压测工具
//再开一个终端登录
[root@promote ~]# docker exec -it 2eaf798a0996 /bin/bash
[root@2eaf798a0996 /]# yum install epel-release -y
[root@8b86214f3109 /]# stress -c 4
stress: info: [100] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd
[root@2eaf798a0996 /]# stress -c 4
stress: info: [117] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd
[root@promote ~]# docker run -itd --name c3 --cpu-shares 1024 centos:7 /bin/bash
69ba84c65c9800ffff1a3536e7eb634c9878b0dc433287543c68827d7c70c908
[root@promote ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69ba84c65c98 centos:7 "/bin/bash" 4 seconds ago Up 3 seconds c3
2eaf798a0996 centos:7 "/bin/bash" 10 minutes ago Up 10 minutes c2
8b86214f3109 centos:7 "/bin/bash" 11 minutes ago Up 11 minutes c1
[root@promote ~]# docker exec -it 69ba84c65c98 /bin/bash
[root@69ba84c65c98 /]# yum install epel-release -y
[root@69ba84c65c98 /]# yum install stress -y
[root@69ba84c65c98 /]# stress -c 4
stress: info: [99] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd
[root@promote ~]# cd /sys/fs/cgroup/cpuset/docker/c7a05e06be4a0b0916c3771dca9fa714dd5aa8f150438ee6275e7a28b8491e87/
[root@promote c7a05e06be4a0b0916c3771dca9fa714dd5aa8f150438ee6275e7a28b8491e87]# cat cpuset.cpus //查看内容,有四个内核
0-3
docker run --cpuset-cpus 0,1,2,3 容器名
[root@promote ~]# docker run -itd --name test1 --cpuset-cpus 1,3 centos:7 /bin/bash
888fa015ac6635d762d63c602ceeb4c485a51d1cea43c95bdb47efb7e593776e
[root@promote ~]# docker exec -it 888fa015ac66 /bin/bash
[root@888fa015ac66 /]# yum install epel-release -y
[root@888fa015ac66 /]# yum install stress -y
[root@888fa015ac66 /]# stress -c 4
stress: info: [94] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd
[root@promote ~]# cd /sys/fs/cgroup/memory/docker/c7a05e06be4a0b0916c3771dca9fa714dd5aa8f150438ee6275e7a28b8491e87/
[root@promote c7a05e06be4a0b0916c3771dca9fa714dd5aa8f150438ee6275e7a28b8491e87]# cat memory.limit_in_bytes
9223372036854771712
docker run -m 512M 容器名
[root@promote ~]# docker run -itd --name test2 -m 512m centos:7 /bin/bash
b318bb08886ec778fa454cc0bf834224cbc6dda8215af3103f45955132a71313
[root@promote ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b318bb08886e centos:7 "/bin/bash" 5 seconds ago Up 4 seconds test2
888fa015ac66 centos:7 "/bin/bash" 10 minutes ago Up 9 minutes test1
[root@promote ~]# cd /sys/fs/cgroup/blkio/docker/c7a05e06be4a0b0916c3771dca9fa714dd5aa8f150438ee6275e7a28b8491e87/
[root@promote c7a05e06be4a0b0916c3771dca9fa714dd5aa8f150438ee6275e7a28b8491e87]# ls
blkio.throttle.write_iops_device
。。。省略部分内容
//格式
--device-read-bps:限制读某个设备的bps(数据量)
例:docker run -itd --device-read-bps /dev/sda:30M centos:7
--device-write-bps:限制写入某个设备的bps(数据量)
例:docker run -itd --device-write-bps /dev/sda:30M centos:7
--device-read-iops:限制读某个设备的iops(次数)
--device-write-iops:限制读某个设备的iops(次数)
[root@promote ~]# docker run -it --name test3 --device-write-bps /dev/sda:1mb centos:7 /bin/bash
[root@c918d1bf6f72 /]# dd if=/dev/zero of=test.out bs=1M count=100 oflag=direct
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 100.064 s, 1.0 MB/s //可以得知写入的平均速度是 1.0 MB/s
[root@promote ~]# docker run -it --name test4 --device-write-iops /dev/sda:5 centos:7 /bin/bash
[root@90efd5a9fb07 /]# dd if=/dev/zero of=test.out bs=1M count=100 oflag=direct
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 40.0031 s, 2.6 MB/s