Docker中的Cgroup资源配置方法

一、Cgroup简介
Cgroup是Control Groups的缩写,是Linux内核提供的一种可以限制、记录、隔离进程组所使用的物理资源(如CPU、内存、磁盘IO等待)的机制,被LXC、docker等很多项目用于实现进程资源控制。Cgroup本身是提供将进程进行分组化管理的功能和接口的基础结构,I/O或内存的分配控制等具体的资源管理是通过该功能来实现的。这些具体的资源管理功能称为Cgroup子系统,有以下几大子系统实现:

blkio:设置限制每个块设备的输入输出控制。例如:磁盘,光盘以及usb等等

CPU:使用调度程序为cgroup任务提供CPU的访问

cpuacct:产生cgroup任务的CPU资源报告

cpuset:如果是多核心的CPU,这个子系统会为cgroup任务分配单独的CPU和内存

devices:允许或拒绝cgroup任务对设备的访问

freezer:暂停和恢复cgroup任务

memory:设置每个cgroup的内存限制以及产生内存资源报告

net_cls:标记每个网络包以供cgroup方便使用

ns:命名空间子系统

perf_event:增加了对每个group的监测跟踪的能力,可以监测属于某个特定的group的所有线程以及运行在特定CPU上的线程

下面开始使用stress压力测试工具来测试CPU和内存使用状况

二、安装stress工具

创建stress目录
[root@server1 ~]# mkdir /opt/stress
[root@server1 ~]# cd /opt/stress/
编写Dockerfile文件
[root@server1 stress]# vim Dockerfile
FROM centos:7
MAINTAINER xxy "xxy123"
RUN yum install -y wget
RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN yum install -y stress
[root@server1 stress]# docker build -t centos:stress .
[root@server1 stress]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              stress              a366d25af2fd        7 hours ago         417MB
centos              7                   8652b9f0cb4c        2 weeks ago         204MB

三、CPU资源分配
默认情况下,每个Docker容器的CPU份额都是1024。单独一个容器的份额是没有意义的。只有在同时运行多个容器时,容器的CPU加权的效果才能体现出来。例如,两个容器A、B的CPU份额分别为1000和500,在CPU进行时间片分配的时候,容器A比容器B多一倍的机会获得CPU的时间片。但分配的结果取决于当时主机和其他容器的运行状态,实际上也无法保证容器A一定能获得CPU时间片。比如容器A的进程一直时空闲的,那么容器B是可以获取比容器A更多的CPU时间片的,极端情况下,例如主机上只运行了一个容器,即使它的CPU份额只有50,它也可以独占整个主机的CPU资源

Cgroup只在容器分配的资源紧缺时,即在需要对容器使用的资源进行限制时,才会生效。因此,无法单纯根据某个容器的CPU份额来确定有多少CPU资源分配给它,资源分配结果取决于同时运行的其他容器的CPU分配和容器中进程运行情况。

可以通过cpu share可以设置容器使用CPU的优先级,比如,启动了两个容器及运行查看CPU使用百分比

3.1、运行两个容器

[root@server1 stress]# docker run -dit --name cpu500 --cpu-shares 500 centos:stress stress -c 10
2d8c7bdaf1c1781afea31a9710b80a1f74c1cc58b9974620453e6cd63b72c8a4
[root@server1 stress]# docker run -dit --name cpu1000 --cpu-shares 1000 centos:stress stress -c 10
099d68458bfef55c9d1f99472e2d54b35e27068f0151cbece710e3893fef8db7
[root@server1 stress]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
099d68458bfe        centos:stress       "stress -c 10"      About a minute ago   Up About a minute                       cpu1000
2d8c7bdaf1c1        centos:stress       "stress -c 10"      About a minute ago   Up About a minute                       cpu500

3.2、查看内存使用情况

#开两个窗口,查看CPU使用情况
[root@server1 stress]# docker exec -it 099d68458bfe bash
[root@099d68458bfe /]# top
top - 10:37:18 up  6:38,  0 users,  load average: 18.98, 8.94, 3.50
Tasks:  13 total,  11 running,   2 sleeping,   0 stopped,   0 zombie
%Cpu(s): 99.9 us,  0.1 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  7994076 total,  6084452 free,   651432 used,  1258192 buff/cache
KiB Swap:  2098172 total,  2047924 free,    50248 used.  7058748 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                              
     9 root      20   0    7312    100      0 R  28.6  0.0   0:45.63 stress                               
    11 root      20   0    7312    100      0 R  28.6  0.0   0:45.30 stress                               
    14 root      20   0    7312    100      0 R  28.6  0.0   0:45.41 stress                               
     6 root      20   0    7312    100      0 R  28.2  0.0   0:44.31 stress                               
    10 root      20   0    7312    100      0 R  28.2  0.0   0:46.36 stress                               
     7 root      20   0    7312    100      0 R  24.9  0.0   0:43.39 stress                               
     8 root      20   0    7312    100      0 R  24.9  0.0   0:43.62 stress                               
    12 root      20   0    7312    100      0 R  24.9  0.0   0:44.70 stress                               
    13 root      20   0    7312    100      0 R  24.9  0.0   0:45.55 stress                               
    15 root      20   0    7312    100      0 R  24.9  0.0   0:43.04 stress                               
     1 root      20   0    7312    632    532 S   0.0  0.0   0:00.02 stress                               
    16 root      20   0   11828   1896   1488 S   0.0  0.0   0:00.02 bash                                 
    29 root      20   0   56164   1948   1440 R   0.0  0.0   0:00.02 top                                  




[root@server1 stress]# docker exec -it 2d8c7bdaf1c1 bash
[root@2d8c7bdaf1c1 /]# top
top - 10:38:41 up  6:40,  0 users,  load average: 19.83, 11.57, 4.87
Tasks:  13 total,  11 running,   2 sleeping,   0 stopped,   0 zombie
%Cpu(s):100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  7994076 total,  6086444 free,   649424 used,  1258208 buff/cache
KiB Swap:  2098172 total,  2047924 free,    50248 used.  7060748 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                              
     9 root      20   0    7312    100      0 R  14.3  0.0   0:39.45 stress                               
    11 root      20   0    7312    100      0 R  14.3  0.0   0:38.76 stress                               
     8 root      20   0    7312    100      0 R  14.0  0.0   0:39.50 stress                               
    14 root      20   0    7312    100      0 R  14.0  0.0   0:38.76 stress                               
     6 root      20   0    7312    100      0 R  13.0  0.0   0:38.01 stress                               
    15 root      20   0    7312    100      0 R  13.0  0.0   0:38.24 stress                               
     7 root      20   0    7312    100      0 R  12.6  0.0   0:37.44 stress                               
    12 root      20   0    7312    100      0 R  12.6  0.0   0:38.51 stress                               
    10 root      20   0    7312    100      0 R  12.3  0.0   0:37.55 stress                               
    13 root      20   0    7312    100      0 R  12.3  0.0   0:37.90 stress                               
     1 root      20   0    7312    632    532 S   0.0  0.0   0:00.02 stress                               
    16 root      20   0   11828   1896   1488 S   0.0  0.0   0:00.02 bash                                 
    29 root      20   0   56164   1952   1440 R   0.0  0.0   0:00.00 top                                  

###由此可见cpu1000的容器cpu使用率大概是另一个的两倍

四、CPU周期限制
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,表示不做控制。cpu-period和cpu-quota参数一般联合使用
例如:容器进程需要每1秒使用单个CPU的0.2秒时间,可以将cpu-period设置为1000000(即1秒),cpu-quota 设置为 200000(0.2 秒)。当然,在多核情况下,如果允许容器进程完全占用两个CPU,则可以将cpu-period设置为100000(即0.1秒),cpu-quota设置为200000(0.2秒)。

[root@server1 stress]# docker run -dit --cpu-period 100000 --cpu-quota 200000 centos:stress
631d46e59d97edb9173ecb445b4ed10c122932c4b308f5ec1cf877838089d845
[root@server1 stress]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
631d46e59d97        centos:stress       "/bin/bash"         7 seconds ago       Up 7 seconds                            clever_booth
[root@server1 stress]# docker exec -it 631d46e59d97 bash
[root@631d46e59d97 /]# cat /sys/fs/cgroup/cpu/cpu.cfs_period_us 
100000
[root@631d46e59d97 /]# cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us
200000

五、CPU Core控制
对多核 CPU 的服务器,Docker 还可以控制容器运行使用哪些 CPU 内核,即使用–cpuset-cpus 参数。这对具有多 CPU 的服务器尤其有用,可以对需要高性能计算的容器进行性能最优的配置。

5.1、进行CPU Core控制配置

[root@server1 stress]# docker run -dit --name cpu1 --cpuset-cpus 0-1 centos:stress 
18f97e1ab4c989d96bebb97c2aef8eed10132076a55f883f38e76a25e376e12a
[root@server1 stress]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
18f97e1ab4c9        centos:stress       "/bin/bash"         2 seconds ago        Up 1 second                             cpu1
[root@server1 stress]# docker run -dit --name cpu1 --cpuset-cpus 0-1 centos:stress 
18f97e1ab4c989d96bebb97c2aef8eed10132076a55f883f38e76a25e376e12a
执行以上命令需要宿主机为双核,表示创建的容器只能用 01两个内核。最终生成 的 cgroup 的 CPU 内核配置如下:
[root@server1 stress]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
18f97e1ab4c9        centos:stress       "/bin/bash"         2 seconds ago        Up 1 second                             cpu1
[root@server1 stress]# docker exec -it 18f97e1ab4c9 bash
[root@18f97e1ab4c9 /]# cat /sys/fs/cgroup/cpuset/cpuset.cpus
0-1
通过下面指令可以看到容器中进程与 CPU 内核的绑定关系,达到绑定 CPU 内核的目的。
[root@server1 stress]# docker exec 18f97e1ab4c9 taskset -c -p 1 
//容器内部第一个进程号pid为1被绑定到指定CPU上运行pid 1's current affinity list: 0,1

六、CPU配额参数的混合使用
通过 cpuset-cpus 参数指定容器 A 使用 CPU 内核 1,容器 B 只是用 CPU 内核 3。在主机上只有这两个容器使用对应 CPU 内核的情况,它们各自占用全部的内核资源,cpu-shares 没有明显效果。

cpuset-cpus、cpuset-mems 参数只在多核、多内存节点上的服务器上有效,并且必须与实际的物理配置匹配,否则也无法达到资源控制的目的。

在系统具有多个 CPU 内核的情况下,需要通过 cpuset-cpus 参数为设置容器 CPU 内核才能方便地进行测试

[root@server1 stress]# docker run -dit --name cpu3 --cpuset-cpus 1 --cpu-shares 500 centos:stress stress -c 1
6c80ad911cfb0badbd014cee336646a4f2d135478289658f019e14ff4a742eb0
[root@server1 stress]# docker run -dit --name cpu4 --cpuset-cpus 3 --cpu-shares 1000 centos:stress stress -c 1
b116f236a4efc9fc8f0b83a87fa86226985fc83c9be8ebe4d62abf434b6598ad
[root@server1 stress]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
b116f236a4ef        centos:stress       "stress -c 1"       5 seconds ago       Up 4 seconds                            cpu4
6c80ad911cfb        centos:stress       "stress -c 1"       17 seconds ago      Up 17 seconds                           cpu3
[root@server1 stress]# docker exec -it b116f236a4ef bash
[root@b116f236a4ef /]# top
top - 10:50:56 up  6:52,  0 users,  load average: 1.38, 2.28, 3.64
Tasks:   4 total,   2 running,   2 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  7994076 total,  6086200 free,   649780 used,  1258096 buff/cache
KiB Swap:  2098172 total,  2047924 free,    50248 used.  7060440 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                              
     6 root      20   0    7312     96      0 R 100.0  0.0   1:03.42 stress                               
     1 root      20   0    7312    424    344 S   0.0  0.0   0:00.01 stress                               
     7 root      20   0   11828   1892   1488 S   0.0  0.0   0:00.02 bash                                 
    20 root      20   0   56156   1940   1440 R   0.0  0.0   0:00.00 top                                  


[root@server1 stress]# docker exec -it 6c80ad911cfb bash
[root@6c80ad911cfb /]# top
top - 10:52:14 up  6:53,  0 users,  load average: 1.89, 2.23, 3.51
Tasks:   4 total,   2 running,   2 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  7994076 total,  6084224 free,   651752 used,  1258100 buff/cache
KiB Swap:  2098172 total,  2047924 free,    50248 used.  7058468 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                              
     6 root      20   0    7312    100      0 R 100.0  0.0   2:34.28 stress                               
     1 root      20   0    7312    428    344 S   0.0  0.0   0:00.01 stress                               
     7 root      20   0   11828   1892   1488 S   0.0  0.0   0:00.03 bash                                 
    20 root      20   0   56156   1940   1440 R   0.0  0.0   0:00.00 top                                  

总结:上面的 centos:stress 镜像安装了 stress 工具,用来测试 CPU 和内存的负载。通过 在两个容器上分别执行 stress -c 1 命令,将会给系统一个随机负载,产生 1 个进程。这 个进程都反复不停的计算由 rand() 产生随机数的平方根,直到资源耗尽。观察到宿主机上的 CPU 使用率,第三个内核的使用率接近 100%, 并且一批进程的 CPU 使用率明显存在 2:1 的使用比例的对比。

七、内存限额
与操作系统类似,容器可使用的内存包括两部分:物理内存和Swap,Docker通过下面两组参数来控制容器内存的使用量

-m或–memory:设置内存的使用限额,例如100M、1024M。
–memory-swap:设置内存+swap的使用限额
执行如下命令允许该容器最多使用200M的内存和300M的内存+swap

[root@server1 stress]# docker run -it -m 200M --memory-swap=300M progrium/stress --vm 1 --vm-bytes 280M
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [6] forked
stress: dbug: [6] allocating 293601280 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: dbug: [6] freed 293601280 bytes
stress: dbug: [6] allocating 293601280 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: dbug: [6] freed 293601280 bytes
stress: dbug: [6] allocating 293601280 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
###会一直运行下去
[root@server1 stress]# docker run -it -m 200M --memory-swap=300M progrium/stress --vm 1 --vm-bytes 310M
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [6] forked
stress: dbug: [6] allocating 325058560 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: FAIL: [1] (416) <-- worker 6 got signal 9
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (422) kill error: No such process
stress: FAIL: [1] (452) failed run completed in 1s
###显示报错,工作线程分配的内存超过300M,分配的内存超过限额,stress线程报错,容器自动退出

八、Block IO的限制
默认情况下,所有容器能平等地读写磁盘,可以通过设置–blkio-weight参数来改变容器block IO的优先级。–blkio-weight与–cpu-shares类似,设置的是相对权重值,默认为500。

在下面的例子中,容器A读写磁盘的带宽是容器B的两倍

[root@server1 stress]# docker run -dit --name container_A --blkio-weight 400 centos:stress 
d88028226030bd447bab4a4a15e00de4ef6a414a78f9da3dccc1d54484d359a2
[root@server1 stress]# docker run -dit --name container_B --blkio-weight 200 centos:stress
87beb13c38ff095a0386266b4a3cf06d782668f0e02cde5dec803e7ed5b373d4
[root@server1 stress]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
87beb13c38ff        centos:stress       "/bin/bash"              10 seconds ago      Up 9 seconds                                   container_B
d88028226030        centos:stress       "/bin/bash"              24 seconds ago      Up 23 seconds                                  container_A

[root@server1 stress]# docker exec -it 87beb13c38ff bash
[root@87beb13c38ff /]# cat /sys/fs/cgroup/blkio/blkio.weight
200
[root@87beb13c38ff /]# exit
exit
[root@server1 stress]# docker exec -it d88028226030 bash
[root@d88028226030 /]# cat /sys/fs/cgroup/blkio/blkio.weight
400

九、bps和iops的限制
bps是byte per second,每秒读写的数据量

iops是io per second,每秒IO的次数

可通过以下参数控制容器的bps和iops:

–device-read-bps,限制读某个设备的bps
–device-write-bps,限制写某个设备的bps
–device-read-iops,限制读某个设备的iops
–device-write-iops,限制写某个设备的iops
下面的示例是限制容器写/dev/sda的速率为5MB/s

[root@server1 stress]# docker run -dit --device-write-bps /dev/sda:5MB centos:stress 
86680ec23f0e22e4b7f7c6409cba0428cf1fe07eb247029ee0d7287126b7d233
[root@server1 stress]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
86680ec23f0e        centos:stress       "/bin/bash"         4 seconds ago       Up 3 seconds                            nervous_wilson
[root@server1 stress]# docker exec -it 86680ec23f0e bash
[root@86680ec23f0e /]# dd if=/dev/zero of=text bs=1M count=1024 oflag=direct
^C38+0 records in
38+0 records out
39845888 bytes (40 MB) copied, 7.60504 s, 5.2 MB/s

[root@server1 stress]# docker run -it centos:stress 
[root@72503552aef2 /]# dd if=/dev/zero of=text bs=1M count=1024 oflag=direct
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 11.6859 s, 91.9 MB/s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值