docker通过cgroup控制容器使用资源配额,包括CPU,内存,磁盘三大方面
[root@node1 ~]# mkdir /opt/stress
[root@node1 ~]# vim /opt/stress/dockerfile
[root@node1 ~]# cat /opt/stress/dockerfile
FROM centos:7
RUN yum install -y wget
RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN yum install -y stress
[root@node1 stress]# docker build -f dockerfile -t centos:stress .
[root@node1 stress]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos stress 9fc6d16d05f3 2 minutes ago 520MB
[root@node1 stress]# docker run -itd --name cpu512 --cpu-shares 512 centos:stress stress -c 10
961fc4f64d2f054e4ff18384257b5269a7be038a88451c45f0241b0fb5506f6f
[root@node1 stress]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
961fc4f64d2f centos:stress "stress -c 10" 5 seconds ago Up 4 seconds cpu512
[root@node1 stress]# docker exec -it cpu512 bash
[root@961fc4f64d2f /]# top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
9 root 20 0 7312 100 0 R 14.3 0.0 2:00.17 stress
13 root 20 0 7312 100 0 R 14.3 0.0 2:01.29 stress
8 root 20 0 7312 100 0 R 14.0 0.0 2:00.94 stress
17 root 20 0 7312 100 0 R 14.0 0.0 2:01.83 stress
15 root 20 0 7312 100 0 R 13.3 0.0 2:02.49 stress
11 root 20 0 7312 100 0 R 13.0 0.0 2:00.46 stress
10 root 20 0 7312 100 0 R 12.7 0.0 1:59.36 stress
14 root 20 0 7312 100 0 R 12.7 0.0 2:01.38 stress
12 root 20 0 7312 100 0 R 12.3 0.0 2:00.06 stress
16 root 20 0 7312 100 0 R 12.3 0.0 2:02.44 stress
[root@node1 stress]# docker run -itd --name cpu1024 --cpu-shares 1024 centos:stress stress -c 10
b744606300935b27f0533aea178fb1bc6b7f9af42b83f0b60334ff2c03eec4ee
[root@node1 stress]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b74460630093 centos:stress "stress -c 10" 7 seconds ago Up 5 seconds cpu1024
961fc4f64d2f centos:stress "stress -c 10" 4 minutes ago Up 4 minutes cpu512
[root@node1 stress]# docker exec -it cpu1024 bash
[root@b74460630093 /]# top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15 root 20 0 7312 100 0 R 31.6 0.0 0:41.33 stress
11 root 20 0 7312 100 0 R 28.9 0.0 0:41.01 stress
13 root 20 0 7312 100 0 R 28.9 0.0 0:42.37 stress
12 root 20 0 7312 100 0 R 28.6 0.0 0:40.67 stress
16 root 20 0 7312 100 0 R 28.2 0.0 0:40.31 stress
10 root 20 0 7312 100 0 R 27.9 0.0 0:42.71 stress
14 root 20 0 7312 100 0 R 24.9 0.0 0:41.31 stress
7 root 20 0 7312 100 0 R 24.6 0.0 0:41.13 stress
8 root 20 0 7312 100 0 R 24.6 0.0 0:41.31 stress
9 root 20 0 7312 100 0 R 24.6 0.0 0:40.49 stress
//第二个大概是第一个的两倍
[root@node1 ~]# docker stats
通过cpu-quota和cpu-period控制
[root@node1 ~]# docker run -itd --cpu-period 100000 --cpu-quota 200000 centos:stress
648a3efc76a4ae21f2f1eee7537244432a2bc2580ab94ea9502caedaa24d8977
[root@node1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
648a3efc76a4 centos:stress "/bin/bash" 15 seconds ago Up 14 seconds nice_raman
[root@node1 ~]# docker exec -it nice_raman bash
[root@648a3efc76a4 /]# cd /sys/fs/cgroup/cpu
[root@648a3efc76a4 cpu]# ls
cgroup.clone_children cgroup.procs cpu.cfs_quota_us cpu.rt_runtime_us cpu.stat cpuacct.usage notify_on_release
cgroup.event_control cpu.cfs_period_us cpu.rt_period_us cpu.shares cpuacct.stat cpuacct.usage_percpu tasks
[root@648a3efc76a4 cpu]# cat cpu.cfs_period_us
100000
[root@648a3efc76a4 cpu]# cat cpu.cfs_quota_us
200000
//或者
[root@node1 ~]# docker inspect 648a3efc76a4
"CpuPeriod": 100000,
"CpuQuota": 200000,
使用–cpuset-cpus参数控制容器运行那些cpu内核
[root@node1 ~]# docker run -itd --name cpu1 --cpuset-cpus 0-1 centos:stress
8ce16919be650418519a5065bfd22f035597d1af278fac7621b02f827215794e
//表示宿主机为双核,表示创建的容器只能用0,1两个内核
[root@node1 ~]# docker exec -it cpu1 bash
[root@8ce16919be65 /]# cat /sys/fs/cgroup/cpuset/cpuset.cpus
0-1
[root@node1 ~]# docker exec cpu1 taskset -c -p 1
pid 1's current affinity list: 0,1
[root@node1 ~]# docker run -itd --name cpu3 --cpuset-cpus 1 --cpu-shares 512 centos:stress stress -c 1
37e1ba3f9829084c3e554f9beed4a1cb4034026e174aa029988ffc9474ded8c4
[root@node1 ~]# top //只占用第二个cpu
Tasks: 192 total, 2 running, 190 sleeping, 0 stopped, 0 zombie
%Cpu0 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 0.3 us, 0.0 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 0.3 us, 0.0 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
[root@node1 ~]# docker run -itd --name cpu4 --cpuset-cpus 3 --cpu-shares 1024 centos:stress stress -c 1
75f5ca1bbaf89c352d5898c54b8af740693dbf05ee5a59cf70443d00ac7e006f
[root@node1 ~]# top //只占用第四个cpu
Tasks: 195 total, 5 running, 190 sleeping, 0 stopped, 0 zombie
%Cpu0 : 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
[root@node1 ~]# docker run -it -m 200M --memory-swap=300M centos:stress
1
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
631c2d0eeee5 laughing_wilson 0.00% 408KiB / 200MiB 0.20% 656B / 0B 0B / 0B 1
[root@node1 ~]# docker run -it --name container_a --blkio-weight 600 centos:stress
[root@b09f3d36626b /]# cat /sys/fs/cgroup/blkio/blkio.weight
600
[root@node1 ~]# docker run -it --name container_b --blkio-weight 300 centos:stress
[root@dbfc640cdab0 /]# cat /sys/fs/cgroup/blkio/blkio.weight
300
[root@2a3bf404a512 /]# dd if=/dev/zero of=test bs=1M count=10 oflag=direct //限制写速率为5MB
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 2.00127 s, 5.2 MB/s
[root@node1 ~]# docker run -it centos:stress
[root@358d4ade35b1 /]# dd if=/dev/zero of=test bs=1M count=100 oflag=direct //不限制时的速率
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.120941 s, 867 MB/s