028、限制容器對CPU的使用(2019-01-23 週三)

三角形發表於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   
 
 

相關文章