Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze Docker cpu restrictions

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to analyze the limitations of Docker cpu. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The docker container restricts several configuration parameters that cpu resources use. The resource occupancy is analyzed by using top and dstat commands respectively.

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; iTunes + {

Go func () {

For {

}

} ()

}

For {

}

}

A test cpu image is created. By default, the image occupies 1 core.

FROM busybox

COPY. / full_cpu / full_cpu

RUN chmod + x / full_cpu

ENTRYPOINT ["/ full_cpu", "- cpunum"]

CMD ["1"]

Docker build-t fangfenghua/cpuuseset.

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:

...

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 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.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 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,0-1)

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).

For details, please refer to: http://blog.csdn.net/horsefoot/article/details/51731543

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 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 00 00 S 0.0 0.0 0:00.05 kthreadd

3 root 20 00 00 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 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.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 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.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

seven

eight

nine

ten

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 0penol 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 00 00 0:100 00 00 0

99 00 00 1:100 00 00 0

99 1 0 0 0: 99 1 0 0 0

100 00 00 0:100 00 00 0

100 00 00 0:100 00 00 0

1par 2 core occupancy rate:

[root@localhost src] # dstat-c-C 1pm 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

00 100 00 0: 00 100 00 0

00 100 00 0: 00 100 00 0

00 100 00 0: 00 100 00 0

00 100 00 0: 00 100 00 0

-

The above is the editor for you to share how to analyze Docker cpu restrictions, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report