028、限制容器对CPU的使用(2019-01-23 周三)
默认情况下,所有容器都可以平等的使用host cpu资源,没有限制
docker 可以通过 -c 或者 --cpu-shares 设置容器使用cpu的权重,如果不指定,默认为1024
与内存限额不同,通过 -c 设置的cpu share 并不是cpu资源的绝对数量,而是一个相对的权重值。某个容器最终能分配到的cpu资源取决于他的cpu share 占所有容器 cpu share 总和的比例。
总结一句话:通过 cpu share 可以设置容器使用 cpu 的权重(优先级),需要注意的是只有CPU资源出现竞争的时候才会按照权重分配CPU资源。
比如:10个容器,有9个容器处于空闲状态,那么剩下的那个容器就可以使用掉全部cpu资源
1、启动容器A,CPU权重1024,可以看到容器A使用了全部CPU资源(docker host cpu有8核,所有使用 --cpu 8 消耗掉所有cpu资源,出现资源竞争)
root@docker-lab:~# docker run --name containerA -it -c 1024 progrium/stress --cpu 8
stress: info: [1] dispatching hogs: 8 cpu, 0 io, 0 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 24000us
stress: dbug: [1] --> hogcpu worker 8 [7] forked
stress: dbug: [1] using backoff sleep of 21000us
stress: dbug: [1] --> hogcpu worker 7 [8] forked
stress: dbug: [1] using backoff sleep of 18000us
stress: dbug: [1] --> hogcpu worker 6 [9] forked
stress: dbug: [1] using backoff sleep of 15000us
stress: dbug: [1] --> hogcpu worker 5 [10] forked
stress: dbug: [1] using backoff sleep of 12000us
stress: dbug: [1] --> hogcpu worker 4 [11] forked
stress: dbug: [1] using backoff sleep of 9000us
stress: dbug: [1] --> hogcpu worker 3 [12] forked
stress: dbug: [1] using backoff sleep of 6000us
stress: dbug: [1] --> hogcpu worker 2 [13] forked
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogcpu worker 1 [14] forked
root@docker-lab:~# top
top - 23:22:09 up 223 days, 6:44, 4 users, load average: 7.08, 3.33, 1.30
Tasks: 190 total, 9 running, 181 sleeping, 0 stopped, 0 zombie
%Cpu0 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 99.7 us, 0.3 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 :100.0 us, 0.0 sy, 0.0 ni, 0.0 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
%Cpu4 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu5 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu6 : 99.7 us, 0.3 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu7 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8174448 total, 2693664 free, 2020948 used, 3459836 buff/cache
KiB Swap: 2097148 total, 291892 free, 1805256 used. 5418476 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
16017 root 20 0 7316 96 0 R 100.0 0.0 0:31.11 stress
16021 root 20 0 7316 96 0 R 100.0 0.0 0:31.12 stress
16014 root 20 0 7316 96 0 R 100.0 0.0 0:31.09 stress
16015 root 20 0 7316 96 0 R 100.0 0.0 0:31.10 stress
16016 root 20 0 7316 96 0 R 100.0 0.0 0:31.10 stress
16018 root 20 0 7316 96 0 R 100.0 0.0 0:31.15 stress
16019 root 20 0 7316 96 0 R 100.0 0.0 0:31.04 stress
16020 root 20 0 7316 96 0 R 99.7 0.0 0:31.11 stress
2、启动容器B,CPU权重512,可以看到全部CPU资源都已被使用,容器A可以使用 66%,容器B可以使用33%(可以使用PID分辨容器A和容器B)
root@docker-lab:~# docker run --name containerB -it -c 512 progrium/stress --cpu 8
stress: info: [1] dispatching hogs: 8 cpu, 0 io, 0 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 24000us
stress: dbug: [1] --> hogcpu worker 8 [8] forked
stress: dbug: [1] using backoff sleep of 21000us
stress: dbug: [1] --> hogcpu worker 7 [9] forked
stress: dbug: [1] using backoff sleep of 18000us
stress: dbug: [1] --> hogcpu worker 6 [10] forked
stress: dbug: [1] using backoff sleep of 15000us
stress: dbug: [1] --> hogcpu worker 5 [11] forked
stress: dbug: [1] using backoff sleep of 12000us
stress: dbug: [1] --> hogcpu worker 4 [12] forked
stress: dbug: [1] using backoff sleep of 9000us
stress: dbug: [1] --> hogcpu worker 3 [13] forked
stress: dbug: [1] using backoff sleep of 6000us
stress: dbug: [1] --> hogcpu worker 2 [14] forked
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogcpu worker 1 [15] forked
root@docker-lab:~# top
top - 23:22:46 up 223 days, 6:45, 4 users, load average: 9.26, 4.24, 1.67
Tasks: 201 total, 17 running, 184 sleeping, 0 stopped, 0 zombie
%Cpu0 : 99.7 us, 0.3 sy, 0.0 ni, 0.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 :100.0 us, 0.0 sy, 0.0 ni, 0.0 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
%Cpu4 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu5 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu6 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu7 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8174448 total, 2678812 free, 2035572 used, 3460064 buff/cache
KiB Swap: 2097148 total, 291892 free, 1805256 used. 5403752 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
16014 root 20 0 7316 96 0 R 66.8 0.0 1:02.51 stress
16015 root 20 0 7316 96 0 R 66.8 0.0 1:01.78 stress
16017 root 20 0 7316 96 0 R 66.8 0.0 1:01.67 stress
16018 root 20 0 7316 96 0 R 66.8 0.0 1:01.83 stress
16020 root 20 0 7316 96 0 R 66.8 0.0 1:02.05 stress
16021 root 20 0 7316 96 0 R 66.8 0.0 1:01.44 stress
16016 root 20 0 7316 96 0 R 66.4 0.0 1:01.52 stress
16019 root 20 0 7316 96 0 R 66.4 0.0 1:01.75 stress
16183 root 20 0 7316 100 0 R 33.2 0.0 0:05.83 stress
16184 root 20 0 7316 100 0 R 33.2 0.0 0:05.84 stress
16185 root 20 0 7316 100 0 R 32.9 0.0 0:05.83 stress
16186 root 20 0 7316 100 0 R 32.9 0.0 0:05.79 stress
16187 root 20 0 7316 100 0 R 32.9 0.0 0:05.76 stress
16188 root 20 0 7316 100 0 R 32.9 0.0 0:05.75 stress
16190 root 20 0 7316 100 0 R 32.9 0.0 0:05.81 stress
16191 root 20 0 7316 100 0 R 32.9 0.0 0:05.82 stress
3、使用 CTRL + C 停止容器A,可以看到容器B使用了全部的CPU资源
root@docker-lab:~# docker run --name containerA -it -c 1024 progrium/stress --cpu 8
stress: info: [1] dispatching hogs: 8 cpu, 0 io, 0 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 24000us
stress: dbug: [1] --> hogcpu worker 8 [7] forked
stress: dbug: [1] using backoff sleep of 21000us
stress: dbug: [1] --> hogcpu worker 7 [8] forked
stress: dbug: [1] using backoff sleep of 18000us
stress: dbug: [1] --> hogcpu worker 6 [9] forked
stress: dbug: [1] using backoff sleep of 15000us
stress: dbug: [1] --> hogcpu worker 5 [10] forked
stress: dbug: [1] using backoff sleep of 12000us
stress: dbug: [1] --> hogcpu worker 4 [11] forked
stress: dbug: [1] using backoff sleep of 9000us
stress: dbug: [1] --> hogcpu worker 3 [12] forked
stress: dbug: [1] using backoff sleep of 6000us
stress: dbug: [1] --> hogcpu worker 2 [13] forked
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogcpu worker 1 [14] forked
CTRL + C
^Cstress: FAIL: [1] (416) <-- worker 12 got signal 2
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (416) <-- worker 14 got signal 2
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (416) <-- worker 8 got signal 2
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (416) <-- worker 7 got signal 2
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (416) <-- worker 11 got signal 2
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (416) <-- worker 10 got signal 2
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (416) <-- worker 9 got signal 2
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (416) <-- worker 13 got signal 2
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (422) kill error: No such process
stress: FAIL: [1] (452) failed run completed in 80s
root@docker-lab:~# top
top - 23:23:19 up 223 days, 6:45, 4 users, load average: 9.97, 5.03, 2.03
Tasks: 191 total, 9 running, 182 sleeping, 0 stopped, 0 zombie
%Cpu0 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 99.7 us, 0.3 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 :100.0 us, 0.0 sy, 0.0 ni, 0.0 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
%Cpu4 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu5 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu6 :100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu7 : 99.7 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.3 st
KiB Mem : 8174448 total, 2692192 free, 2022216 used, 3460040 buff/cache
KiB Swap: 2097148 total, 291892 free, 1805256 used. 5417144 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
16183 root 20 0 7316 100 0 R 100.0 0.0 0:30.32 stress
16185 root 20 0 7316 100 0 R 100.0 0.0 0:30.25 stress
16186 root 20 0 7316 100 0 R 100.0 0.0 0:30.30 stress
16190 root 20 0 7316 100 0 R 100.0 0.0 0:30.28 stress
16184 root 20 0 7316 100 0 R 100.0 0.0 0:30.32 stress
16187 root 20 0 7316 100 0 R 100.0 0.0 0:30.24 stress
16188 root 20 0 7316 100 0 R 100.0 0.0 0:30.25 stress
16191 root 20 0 7316 100 0 R 100.0 0.0 0:30.30 stress
16273 root 20 0 40528 3780 3108 R 0.3 0.0 0:00.01 top