本文测试了,Docker容器限制cpu资源使用的几个配置参数。分别使用top和dstat命令分析了资源占有情况。
package mainimport ( "flag" "runtime" "fmt")func main() { cpunum := flag.Int("cpunum", 0, "cpunum") flag.Parse() fmt.Println("cpunum:", *cpunum) runtime.GOMAXPROCS(*cpunum) for i := 0; i < *cpunum - 1; i++ { go func() { for { } }() } for { }}
制作了一个测试cpu占用的镜像,镜像默认占满1个核心
FROM busyboxCOPY ./full_cpu /full_cpuRUN chmod +x /full_cpuENTRYPOINT ["/full_cpu", "-cpunum"]CMD ["1"]docker build -t fangfenghua/cpuuseset .docker push fangfenghua/cpuusesetdocker info...Default Runtime: runcSecurity Options: seccompKernel Version: 3.10.0-229.el7.x86_64Operating System: CentOS Linux 7 (Core)OSType: linuxArchitecture: x86_64CPUs: 4Total Memory: 993.3 MiBName: localhost.localdomainID: TU6M:E6WM:PZDN:ULJX:EWKS: ...
docker run -it --rm=true fangfenghua/cpuuseset [root@localhost src]# toptop - 07:23:52 up 1:23, 2 users, load average: 0.61, 1.12, 1.04Tasks: 154 total, 3 running, 145 sleeping, 6 stopped, 0 zombie%Cpu(s): 18.0 us, 0.1 sy, 0.0 ni, 81.8 id, 0.0 wa, 0.0 hi, 0.1 si, 0.0 stKiB Mem : 1017144 total, 422120 free, 171676 used, 423348 buff/cacheKiB Swap: 1040380 total, 1040284 free, 96 used. 688188 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 20196 root 20 0 3048 720 460 R 101.7 0.1 0:37.56 full_cpu 1 root 20 0 41536 4028 2380 S 0.0 0.4 0:02.60 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.48 ksoftirqd/0 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H 7 root rt 0 0 0 0 S 0.0 0.0 0:00.69 migration/0 docker run -it --rm=true fangfenghua/cpuuseset 4top - 07:27:17 up 1:27, 2 users, load average: 2.41, 1.47, 1.18Tasks: 159 total, 3 running, 145 sleeping, 11 stopped, 0 zombie%Cpu(s): 99.6 us, 0.2 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 stKiB Mem : 1017144 total, 402508 free, 190908 used, 423728 buff/cacheKiB Swap: 1040380 total, 1040284 free, 96 used. 668608 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 20935 root 20 0 3048 720 452 R 400.0 0.1 0:55.80 full_cpu 1 root 20 0 41620 4088 2380 S 0.0 0.4 0:02.88 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd
在Linux 系统上,可以用来限制docker容器资源占用的参数有:
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota -c, --cpu-shares int CPU shares (relative weight) --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)新闻热点
疑难解答