In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Understand the sample analysis of Docker cpu restrictions? This problem may be often seen in our daily study or work. I hope you can gain a lot from this question. The following is the reference content that the editor brings to you, let's take a look at it!
This article tested several configuration parameters that the docker container restricts the use of cpu resources. The resource occupancy is analyzed by using top and dstat commands respectively.
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 For + {go func () {for {}} ()} for {}}
A test cpu image is created. By default, the image occupies 1 core.
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: 154total, 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 full_cpu 1 root 20 0 41536 4028 2380 S 0.0 0.4 0:02.60 systemd 2 root 20 00 00 S 0.0 0.0 0:00.04 kthreadd 3 root 20 00 00 S 0.0 0.0 0:00.48 ksoftirqd/0 5 root 0-20 000 S 0.0 0.00: 00.00 kworker/0:0H 7 root rt 00 00 S 0.0 0.00: 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: 159total, 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 452R 400.0 0.1 0V 55.80 full_cpu 1 root 20 0 41620 4088 2380 S 0.0 0.4 0:02.88 systemd 2 root 20 00 00 S 0.0 0.0 0:00.04 kthreadd
On linux systems, the parameters that can be used to limit the resource consumption of the docker container are:
-- 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,0L1)
Docker provides two parameters-cpu-period and-cpu-quota to control the CPU clock cycle to which the container can be allocated. -cpu-period is used to specify how often the container redistributes the use of CPU, while-cpu-quota is used to specify the maximum amount of time that can be spent running the container during this period. Unlike-cpu-shares, this configuration specifies an absolute value, and there is no flexibility in it, so the container's use of CPU resources will never exceed the configured value.
Cpu-period and cpu-quota are in microseconds (μ s). The minimum value of cpu-period is 1000 microseconds, the maximum is 1 second (10 ^ 6 μ s), and the default value is 0.1 second (100000 μ s). The default value of cpu-quota is-1, which means that there is no control.
For example, if the container process needs to use a single CPU for 0.2 seconds every second, you can set cpu-period to 1000000 (that is, 1 second) and cpu-quota to 200000 (0.2 seconds). Of course, in a multicore case, if you allow the container process to fully occupy two CPU, you can set cpu-period to 100000 (that is, 0.1s) and cpu-quota to 200000 (0.2s).
Use the container image created in this article to test the two parameters cpu-period and cpu-quota.
In the 4-core system used in this article, if you want cpuusetest to occupy two cores, how to configure it? As you can see from the above analysis, if you set cpu-period to 100000 and expect to occupy 4 cores, you need to set cpu-quota to 4 to 100000, and expectations to fill one core to 2 to 100, 000. Let's test it:
Docker run-- name cpuuse-d-- cpu-period=100000-- cpu-quota=200000 fangfenghua/cpuusetest 4top-07:46:31 up 1:46, 2 users, load average: 0.16,0.21, 0.51Tasks: 168total, 2 running, 142sleeping, 24 stopped, 0 zombie%Cpu (s): 47.8us, 0.1sy, 0.0 ni, 51.9id, 0.1wa, 0.0hi, 0.1si, 0.0stKiB Mem: 1017144 total, 364724 free 227816 used, 424604 buff/cacheKiB 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 MEM TIME+ COMMAND 00.37 full_cpu 1 root 20 0 41620 4088 2380 S 0.0 0.4 0:03.13 systemd 2 root 20 00 00 S 0.0 0.0 0:00.05 kthreadd 3 root 20 00 00 S 0.0 0 up 00.52 ksoftirtop-07:47:17 up 1:47 2 users, load average: 0.47,0.26, 0.51Tasks: 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 stKiB Mem: 1017144 total, 358760 free, 233292 used, 425092 buff/cacheKiB 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 200 3048 724 456 R 398.3 0.1 cpu-period=100000 16.81 full_cpu 21297 root 20 00 00 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 00 00 S 0.0 0.0 0:00.05 kthreadd
Using the above two parameters, you can set the precise control of cpu. There is also a parameter cpu-share, which is a relative value. If you set the A container cpu-share to 1536, set the B container to 512. So, what is the cpu usage before Container B starts?
Top-07:56:10 up 1:56, 2 users, load average: 0.75,0.36, 0.50Tasks: 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 stKiB Mem: 1017144 total, 436300 free, 155616 used, 425228 buff/cacheKiB 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 MEM TIME+ COMMAND 55.03 full_cpu 1 root 20 0 41620 4088 2380 S 0.0 0.4 0:03.29 systemd 2 root 20 00 00 S 0.0 0.0 0:00.05 kthreadd 3 root 20 00 00 S 0.0 0.0 0:00.54 ksoftirqd/0
Start Container B:
Top-07:57:09 up 1:57, 2 users, load average: 3.55, 1.16, 0.76Tasks: 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 stKiB Mem: 1017144 total, 428772 free, 158304 used, 430068 buff/cacheKiB 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.14 MEM TIME+ COMMAND 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
It is not difficult to see from the above test results. When setting the relative value, before container B starts, container A still occupies full cpu, but after container B starts, container accounts for 3 big 4 and container B accounts for 1 big 4.
There is also a parameter cpu-sets that specifies the core used by the container. Use the above test container to test, and specify that the container uses the 0Permy 3 core:
Docker run-name cpuuse-d-cpuset-cpus=0,3 fangfenghua/cpuusetest 4
0p3 Core occupancy rate:
[root@localhost src] # dstat-c-C 0Magne3 words CPU 0 words 3 words sys idl wai hiq siq:usr sys idl wai hiq siq 25 9 66 00 0: 12 1 87 00 0100 00 00 00 0 99 00 00 1 100 00 00 0 99 100 00: 99 100 0 0100 00 00 0:100 00 00 0100 00 00 0:100 00 00 0
1par 2 core occupancy rate:
[root@localhost src] # dstat-c-C 1Magne2 words CPU 1 words 2 words words sys idl wai hiq siq:usr sys idl wai hiq siq 21 8 71 00 0: 10 1 89 00 00 00 0: 00 00 00 0: 00 1000 0 00 00 100 00 0: 00 100 00 00 00 0: 00 1000 0 thank you for your reading! After reading the above, do you have a general understanding of the example analysis of Docker cpu restrictions? I hope the content of the article will be helpful to all of you. If you want to know more about the relevant articles, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.