本文测试了,Docker容器限制cpu资源使用的几个配置参数。分别使用top和dstat命令分析了资源占有情况。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package main import ( "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个核心
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
FROM busybox COPY . /full_cpu /full_cpu RUN chmod +x /full_cpu ENTRYPOINT [ "/full_cpu" , "-cpunum" ] CMD [ "1" ] docker push fangfenghua /cpuuseset docker info ... Default Runtime: runc Security Options: seccomp Kernel Version: 3.10.0-229.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 4 Total Memory: 993.3 MiB Name: localhost.localdomain ID: TU6M:E6WM:PZDN:ULJX:EWKS: ... |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
docker run -it -- rm = true fangfenghua /cpuuseset [root@localhost src] # top top - 07:23:52 up 1:23, 2 users , load average: 0.61, 1.12, 1.04 Tasks: 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 st KiB Mem : 1017144 total, 422120 free , 171676 used, 423348 buff /cache KiB 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 4 top - 07:27:17 up 1:27, 2 users , load average: 2.41, 1.47, 1.18 Tasks: 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 st KiB Mem : 1017144 total, 402508 free , 190908 used, 423728 buff /cache KiB 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)
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。下面就测试一下吧:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
docker run --name cpuuse -d --cpu-period=100000 --cpu- quota =200000 fangfenghua /cpuusetest 4 top - 07:46:31 up 1:46, 2 users , load average: 0.16, 0.21, 0.51 Tasks: 168 total, 2 running, 142 sleeping, 24 stopped, 0 zombie %Cpu(s): 47.8 us, 0.1 sy, 0.0 ni, 51.9 id , 0.1 wa, 0.0 hi, 0.1 si, 0.0 st KiB Mem : 1017144 total, 364724 free , 227816 used, 424604 buff /cache KiB Swap: 1040380 total, 1040284 free , 96 used. 631052 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 21766 root 20 0 3048 724 464 R 193.3 0.1 1:00.37 full_cpu 1 root 20 0 41620 4088 2380 S 0.0 0.4 0:03.13 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.05 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.52 ksoftir top - 07:47:17 up 1:47, 2 users , load average: 0.47, 0.26, 0.51 Tasks: 172 total, 3 running, 144 sleeping, 25 stopped, 0 zombie %Cpu(s): 99.6 us, 0.1 sy, 0.0 ni, 0.3 id , 0.0 wa, 0.0 hi, 0.1 si, 0.0 st KiB Mem : 1017144 total, 358760 free , 233292 used, 425092 buff /cache KiB Swap: 1040380 total, 1040284 free , 96 used. 625180 avail Mem docker run --name cpuuse -d --cpu-period=100000 --cpu- quota =400000 fangfenghua /cpuusetest 4 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 21976 root 20 0 3048 724 456 R 398.3 0.1 0:16.81 full_cpu 21297 root 20 0 0 0 0 S 0.3 0.0 0:00.08 kworker /0 :2 1 root 20 0 41620 4088 2380 S 0.0 0.4 0:03.19 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.05 kthreadd |
使用上述两个参数可以,设置cpu的精确控制。还有一个参数cpu-share,是个相对值。假如设置A容器cpu-share为1536,设置B容器为512。那么,在容器B启动前,cpu占用情况为是什么呢?
1
2
3
4
5
6
7
8
9
10
11
|
top - 07:56:10 up 1:56, 2 users, load average: 0.75, 0.36, 0.50 Tasks: 153 total, 3 running, 140 sleeping, 10 stopped, 0 zombie %Cpu(s): 99.7 us, 0.1 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st KiB Mem : 1017144 total, 436300 free, 155616 used, 425228 buff/cache KiB Swap: 1040380 total, 1040284 free, 96 used. 703544 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 22216 root 20 0 3048 720 456 R 399.3 0.1 0:55.03 full_cpu 1 root 20 0 41620 4088 2380 S 0.0 0.4 0:03.29 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.05 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.54 ksoftirqd/0 |
启动容器B:
1
2
3
4
5
6
7
8
9
10
|
top - 07:57:09 up 1:57, 2 users , load average: 3.55, 1.16, 0.76 Tasks: 162 total, 4 running, 148 sleeping, 10 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 st KiB Mem : 1017144 total, 428772 free , 158304 used, 430068 buff /cache KiB Swap: 1040380 total, 1040284 free , 96 used. 700444 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 22216 root 20 0 3048 720 456 R 305.7 0.1 4:40.78 full_cpu 22336 root 20 0 3048 720 460 R 95.3 0.1 0:09.02 full_cpu 1 root 20 0 41620 4088 2380 S 0.0 0.4 0:03.31 systemd |
从上述测试结果不难看出。设置相对数值时,容器B启动之前,容器A仍然占满了cpu,而容器B启动后则,容器占3/4,容器B占1/4。
还有一个参数cpu-sets,指定容器使用的核心。使用上述测试容器测试,指定容器使用0,3核心:
1
|
docker run --name cpuuse -d --cpuset-cpus=0,3 fangfenghua /cpuusetest 4 |
0,3核心占用率:
1
2
3
4
5
6
7
8
9
|
[root@localhost src] # dstat -c -C 0,3 -------cpu0-usage--------------cpu3-usage------ usr sys idl wai hiq siq:usr sys idl wai hiq siq 25 9 66 0 0 0: 12 1 87 0 0 0 100 0 0 0 0 0:100 0 0 0 0 0 99 0 0 0 0 1:100 0 0 0 0 0 99 1 0 0 0 0: 99 1 0 0 0 0 100 0 0 0 0 0:100 0 0 0 0 0 100 0 0 0 0 0:100 0 0 0 0 0 |
1,2核心占用率:
1
2
3
4
5
6
7
8
|
[root@localhost src] # dstat -c -C 1,2 -------cpu1-usage--------------cpu2-usage------ usr sys idl wai hiq siq:usr sys idl wai hiq siq 21 8 71 0 0 0: 10 1 89 0 0 0 0 0 100 0 0 0: 0 0 100 0 0 0 0 0 100 0 0 0: 0 0 100 0 0 0 0 0 100 0 0 0: 0 0 100 0 0 0 0 0 100 0 0 0: 0 0 100 0 0 0 |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/asd05txffh/article/details/52267818