详解Docker cpu限制分析
本文测试了,Docker容器限制cpu资源使用的几个配置参数。分别使用top和dstat命令分析了资源占有情况。
packagemain import( "flag" "runtime" "fmt" ) funcmain(){ cpunum:=flag.Int("cpunum",0,"cpunum") flag.Parse() fmt.Println("cpunum:",*cpunum) runtime.GOMAXPROCS(*cpunum) fori:=0;i<*cpunum-1;i++{ gofunc(){ for{ } }() } for{ } }
制作了一个测试cpu占用的镜像,镜像默认占满1个核心
FROMbusybox COPY./full_cpu/full_cpu RUNchmod+x/full_cpu ENTRYPOINT["/full_cpu","-cpunum"] CMD["1"] dockerbuild-tfangfenghua/cpuuseset. dockerpushfangfenghua/cpuuseset dockerinfo ... DefaultRuntime:runc SecurityOptions:seccomp KernelVersion:3.10.0-229.el7.x86_64 OperatingSystem:CentOSLinux7(Core) OSType:linux Architecture:x86_64 CPUs:4 TotalMemory:993.3MiB Name:localhost.localdomain ID:TU6M:E6WM:PZDN:ULJX:EWKS: ...
dockerrun-it--rm=truefangfenghua/cpuuseset [root@localhostsrc]#top top-07:23:52up1:23,2users,loadaverage:0.61,1.12,1.04 Tasks:154total,3running,145sleeping,6stopped,0zombie %Cpu(s):18.0us,0.1sy,0.0ni,81.8id,0.0wa,0.0hi,0.1si,0.0st KiBMem:1017144total,422120free,171676used,423348buff/cache KiBSwap:1040380total,1040284free,96used.688188availMem PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND 20196root2003048720460R101.70.10:37.56full_cpu 1root2004153640282380S0.00.40:02.60systemd 2root200000S0.00.00:00.04kthreadd 3root200000S0.00.00:00.48ksoftirqd/0 5root0-20000S0.00.00:00.00kworker/0:0H 7rootrt0000S0.00.00:00.69migration/0 dockerrun-it--rm=truefangfenghua/cpuuseset4 top-07:27:17up1:27,2users,loadaverage:2.41,1.47,1.18 Tasks:159total,3running,145sleeping,11stopped,0zombie %Cpu(s):99.6us,0.2sy,0.0ni,0.0id,0.0wa,0.0hi,0.3si,0.0st KiBMem:1017144total,402508free,190908used,423728buff/cache KiBSwap:1040380total,1040284free,96used.668608availMem PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND 20935root2003048720452R400.00.10:55.80full_cpu 1root2004162040882380S0.00.40:02.88systemd 2root200000S0.00.00:00.04kthreadd
在Linux系统上,可以用来限制docker容器资源占用的参数有:
- --cpu-periodint LimitCPUCFS(CompletelyFairScheduler)period
- --cpu-quotaint LimitCPUCFS(CompletelyFairScheduler)quota
- -c,--cpu-sharesint CPUshares(relativeweight)
- --cpuset-cpusstring CPUsinwhichtoallowexecution(0-3,0,1)
docker提供了–cpu-period、–cpu-quota两个参数控制容器可以分配到的CPU时钟周期。–cpu-period是用来指定容器对CPU的使用要在多长时间内做一次重新分配,而–cpu-quota是用来指定在这个周期内,最多可以有多少时间用来跑这个容器。跟–cpu-shares不同的是这种配置是指定一个绝对值,而且没有弹性在里面,容器对CPU资源的使用绝对不会超过配置的值。
cpu-period和cpu-quota的单位为微秒(μs)。cpu-period的最小值为1000微秒,最大值为1秒(10^6μs),默认值为0.1秒(100000μs)。cpu-quota的值默认为-1,表示不做控制。
举个例子,如果容器进程需要每1秒使用单个CPU的0.2秒时间,可以将cpu-period设置为1000000(即1秒),cpu-quota设置为200000(0.2秒)。当然,在多核情况下,如果允许容器进程需要完全占用两个CPU,则可以将cpu-period设置为100000(即0.1秒),cpu-quota设置为200000(0.2秒)。
使用本文制作的容器镜像来测试,cpu-period和cpu-quota两个参数吧。
在本文使用的4核心系统中,如果希望cpuusetest占满两个核心,在如何配置呢?从上文的分析中可以看到,如果将cpu-period设置为100000,那么期望占满4个核心,则需要将cpu-quota设置为4*100000,期望占满一个核心则可设置为2*100000。下面就测试一下吧:
dockerrun--namecpuuse-d--cpu-period=100000--cpu-quota=200000fangfenghua/cpuusetest4 top-07:46:31up1:46,2users,loadaverage:0.16,0.21,0.51 Tasks:168total,2running,142sleeping,24stopped,0zombie %Cpu(s):47.8us,0.1sy,0.0ni,51.9id,0.1wa,0.0hi,0.1si,0.0st KiBMem:1017144total,364724free,227816used,424604buff/cache KiBSwap:1040380total,1040284free,96used.631052availMem PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND 21766root2003048724464R193.30.11:00.37full_cpu 1root2004162040882380S0.00.40:03.13systemd 2root200000S0.00.00:00.05kthreadd 3root200000S0.00.00:00.52ksoftir top-07:47:17up1:47,2users,loadaverage:0.47,0.26,0.51 Tasks:172total,3running,144sleeping,25stopped,0zombie %Cpu(s):99.6us,0.1sy,0.0ni,0.3id,0.0wa,0.0hi,0.1si,0.0st KiBMem:1017144total,358760free,233292used,425092buff/cache KiBSwap:1040380total,1040284free,96used.625180availMem dockerrun--namecpuuse-d--cpu-period=100000--cpu-quota=400000fangfenghua/cpuusetest4 PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND 21976root2003048724456R398.30.10:16.81full_cpu 21297root200000S0.30.00:00.08kworker/0:2 1root2004162040882380S0.00.40:03.19systemd 2root200000S0.00.00:00.05kthreadd
使用上述两个参数可以,设置cpu的精确控制。还有一个参数cpu-share,是个相对值。假如设置A容器cpu-share为1536,设置B容器为512。那么,在容器B启动前,cpu占用情况为是什么呢?
top-07:56:10up1:56,2users,loadaverage:0.75,0.36,0.50 Tasks:153total,3running,140sleeping,10stopped,0zombie %Cpu(s):99.7us,0.1sy,0.0ni,0.0id,0.0wa,0.0hi,0.3si,0.0st KiBMem:1017144total,436300free,155616used,425228buff/cache KiBSwap:1040380total,1040284free,96used.703544availMem PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND 22216root2003048720456R399.30.10:55.03full_cpu 1root2004162040882380S0.00.40:03.29systemd 2root200000S0.00.00:00.05kthreadd 3root200000S0.00.00:00.54ksoftirqd/0
启动容器B:
top-07:57:09up1:57,2users,loadaverage:3.55,1.16,0.76 Tasks:162total,4running,148sleeping,10stopped,0zombie %Cpu(s):99.6us,0.2sy,0.0ni,0.0id,0.0wa,0.0hi,0.3si,0.0st KiBMem:1017144total,428772free,158304used,430068buff/cache KiBSwap:1040380total,1040284free,96used.700444availMem PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND 22216root2003048720456R305.70.14:40.78full_cpu 22336root2003048720460R95.30.10:09.02full_cpu 1root2004162040882380S0.00.40:03.31systemd
从上述测试结果不难看出。设置相对数值时,容器B启动之前,容器A仍然占满了cpu,而容器B启动后则,容器占3/4,容器B占1/4。
还有一个参数cpu-sets,指定容器使用的核心。使用上述测试容器测试,指定容器使用0,3核心:
dockerrun--namecpuuse-d--cpuset-cpus=0,3fangfenghua/cpuusetest4
0,3核心占用率:
[root@localhostsrc]#dstat-c-C0,3 -------cpu0-usage--------------cpu3-usage------ usrsysidlwaihiqsiq:usrsysidlwaihiqsiq 25966000:12187000 10000000:10000000 9900001:10000000 9910000:9910000 10000000:10000000 10000000:10000000
1,2核心占用率:
[root@localhostsrc]#dstat-c-C1,2 -------cpu1-usage--------------cpu2-usage------ usrsysidlwaihiqsiq:usrsysidlwaihiqsiq 21871000:10189000 00100000:00100000 00100000:00100000 00100000:00100000 00100000:00100000
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。