白话容器之CPU与内存资源限制测试(25)

2023年 7月 16日 49.3k 0

配置

在前面了解了容器之CPU与内存资源限制概述,我们进行简单的资源限制测试我们下载lorel/docker-stress-ng测试docker-58.png

我们在dockerhub上选择latest版本拉取到本地

[root@linuxEA-145 /data/harbor]$ docker pull lorel/docker-stress-ng
Using default tag: latest
latest: Pulling from lorel/docker-stress-ng
c52e3ed763ff: Pull complete 
a3ed95caeb02: Pull complete 
7f831269c70e: Pull complete 
Digest: sha256:c8776b750869e274b340f8e8eb9a7d8fb2472edd5b25ff5b7d55728bca681322
Status: Downloaded newer image for lorel/docker-stress-ng:latest
  • memory

可以使用 docker run --name stress --rm lorel/docker-stress-ng --help查看帮助

 -m N, --vm N             start N workers spinning on anonymous mmap
 (-m N,-  vm              N启动N个工作人员在匿名mmap上旋转)
 --vm-bytes N             allocate N bytes per vm worker (default 256MB)
 (--vm-bytes              N为每个vm worker分配N个字节(默认为256MB) )

我们先对内存进行压测

使用-m 指定配置大小为256m,而后使用 --vm 2 ,一个vm的默认是256M,压测观测下

[root@linuxEA-145 /data/harbor]$ docker run --name stress --rm -m 256m lorel/docker-stress-ng  --vm 2 
stress-ng: info: [1] defaulting to a 86400 second run per stressor
stress-ng: info: [1] dispatching hogs: 2 vm

观测内存的使用情况

[root@linuxEA-145 ~]$ docker top stress
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                12685               12670               0                   11:52               ?                   00:00:00            /usr/bin/stress-ng --vm 2
root                12716               12685               0                   11:52               ?                   00:00:00            /usr/bin/stress-ng --vm 2
root                12717               12685               0                   11:52               ?                   00:00:00            /usr/bin/stress-ng --vm 2
root                12799               12717               70                  11:52               ?                   00:00:00            /usr/bin/stress-ng --vm 2
root                12810               12716               5                   11:52               ?                   00:00:00            /usr/bin/stress-ng --vm 2

可以使用docker stats stress,内存最多使用在限制内的大小

[root@linuxEA-145 ~]$ docker stats stress


CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
3b96828e471a        stress              0.00%               255.9MiB / 256MiB   99.95%              712B / 42B          2.36GB / 20.5GB     5

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
3b96828e471a        stress              137.67%             255.8MiB / 256MiB   99.92%              712B / 42B          2.53GB / 21.8GB     5

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
3b96828e471a        stress              137.67%             255.8MiB / 256MiB   99.92%              712B / 42B          2.53GB / 21.8GB     5

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
3b96828e471a        stress              139.01%             255.8MiB / 256MiB   99.91%              712B / 42B          2.69GB / 23GB       5

可以看到这个内存一直在使用达99%

  • cpu

我们在验证下CPU

限制使用2核心,也就是最多使用200%,运行8个进程使用(无论起多少进程,只有2核心分配)

请注意,这样的限制只能使用2个cpu,那就意味着0-3的cpu任何两个都会被使用,并不固定在那一颗CPU

[root@linuxEA-145 /data/harbor]$ docker run --name stress --rm --cpus 2  lorel/docker-stress-ng  --cpu 8
stress-ng: info: [1] defaulting to a 86400 second run per stressor
stress-ng: info: [1] dispatching hogs: 8 cpu

使用docker top stress观测,8个进程已经运行

[root@linuxEA-145 ~]$ docker top stress
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                32201               32185               0                   14:19               ?                   00:00:00            /usr/bin/stress-ng --cpu 8
root                32232               32201               25                  14:19               ?                   00:00:01            /usr/bin/stress-ng --cpu 8
root                32233               32201               25                  14:19               ?                   00:00:01            /usr/bin/stress-ng --cpu 8
root                32234               32201               25                  14:19               ?                   00:00:01            /usr/bin/stress-ng --cpu 8
root                32235               32201               25                  14:19               ?                   00:00:01            /usr/bin/stress-ng --cpu 8
root                32236               32201               25                  14:19               ?                   00:00:01            /usr/bin/stress-ng --cpu 8
root                32237               32201               25                  14:19               ?                   00:00:01            /usr/bin/stress-ng --cpu 8
root                32238               32201               25                  14:19               ?                   00:00:01            /usr/bin/stress-ng --cpu 8
root                32239               32201               25                  14:19               ?                   00:00:01            /usr/bin/stress-ng --cpu 8

而CPU最多使用200

[root@linuxEA-145 ~]$ docker stats stress

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
d4b2a1641d67        stress              200.05%             18.77MiB / 3.848GiB   0.48%               0B / 42B            0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
d4b2a1641d67        stress              200.05%             18.77MiB / 3.848GiB   0.48%               0B / 42B            0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
d4b2a1641d67        stress              199.37%             18.77MiB / 3.848GiB   0.48%               0B / 42B            0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
d4b2a1641d67        stress              199.37%             18.77MiB / 3.848GiB   0.48%               0B / 42B            0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
d4b2a1641d67        stress              200.50%             18.77MiB / 3.848GiB   0.48%               0B / 42B            0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
d4b2a1641d67        stress              200.50%             18.77MiB / 3.848GiB   0.48%               0B / 42B            0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
d4b2a1641d67        stress              200.10%             18.77MiB / 3.848GiB   0.48%               0B / 42B            0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS

假如我们没有限制,将会跑近400%,也就是全部资源被使用

当然,也可以指定运行在那一颗之上。

比如,限制运行在0,2上

这样--cpuset-cpus 0,2 只会运行在0,2不会运行在其他的CPU核心上。

[root@linuxEA-145 /data/harbor]$ docker run --name stress --rm --cpuset-cpus 0,2  lorel/docker-stress-ng  --cpu 8
stress-ng: info: [1] defaulting to a 86400 second run per stressor
stress-ng: info: [1] dispatching hogs: 8 cpu

仍然在限制内

[root@linuxEA-145 ~]$ docker stats stress

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
21537336658b        stress              200.76%             21.71MiB / 3.848GiB   0.55%               0B / 42B            0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
21537336658b        stress              200.76%             21.71MiB / 3.848GiB   0.55%               0B / 42B            0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
21537336658b        stress              200.29%             21.71MiB / 3.848GiB   0.55%               0B / 42B            0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
21537336658b        stress              200.29%             21.71MiB / 3.848GiB   0.55%               0B / 42B            0B / 0B             9

而后我们在宿主机查看top,0,2cpu已经跑满,说明限制是ok的

[root@linuxEA-145 /data/harbor]$ top
top - 14:25:02 up 85 days, 23:51,  4 users,  load average: 2.37, 3.31, 3.04
Tasks: 167 total,   9 running,  91 sleeping,   0 stopped,   0 zombie
%Cpu0  :  99.3/0.3   100[||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ]
%Cpu1  :   0.7/0.7     1[||                                                                                                  ]
%Cpu2  : 100.0/0.0   100[||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||]
%Cpu3  :   1.0/1.0     2[||                                                                                                  ]
KiB Mem : 29.4/4034596  [|||||||||||||||||||||||||||||                                                                       ]
KiB Swap:  1.9/4190204  [||                                                                                                  ]

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                                                                                                                 
  582 root      20   0    6924   2444    620 R  25.2  0.1   0:04.17 stress-ng-cpu                                                                                                                                                                                           
  590 root      20   0    6924   2444    620 R  25.2  0.1   0:04.16 stress-ng-cpu                                                                                                                                                                                           
  583 root      20   0    6924   3960    620 R  24.9  0.1   0:04.16 stress-ng-cpu                                                                                                                                                                                           
  584 root      20   0    6924   3960    620 R  24.9  0.1   0:04.16 stress-ng-cpu                                                                                                                                                                                           
  585 root      20   0    6924   2444    620 R  24.9  0.1   0:04.16 stress-ng-cpu                                                                                                                                                                                           
  587 root      20   0    6924   2444    620 R  24.9  0.1   0:04.16 stress-ng-cpu                              
  • --cpu-shares共享

那如果是共享的方式就不同了

共享的方式--cpu-shares,在没有使用的时候都会被吃掉,如果有其他的容器运行则分配给别人

[root@linuxEA-145 /data/harbor]$ docker run --name stress --rm --cpu-shares 1024  lorel/docker-stress-ng  stress --cpu 8
stress-ng: info: [1] defaulting to a 86400 second run per stressor
stress-ng: info: [1] dispatching hogs: 8 cpu

只运行一个容器的时候,CPU 400%

[root@linuxEA-145 ~]$ docker stats stress

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
dc3c61506fb9        stress              399.67%             17.14MiB / 3.848GiB   0.44%               0B / 42B            0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
dc3c61506fb9        stress              399.67%             17.14MiB / 3.848GiB   0.44%               0B / 42B            0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
dc3c61506fb9        stress              400.42%             17.14MiB / 3.848GiB   0.44%               0B / 42B            0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
dc3c61506fb9        stress              400.42%             17.14MiB / 3.848GiB   0.44%               0B / 42B            0B / 0B             9

我们在运行一个容器,也做限制为512

[root@linuxEA-145 /data/harbor]$  docker run --name stress1 --rm --cpu-shares 512  lorel/docker-stress-ng  stress --cpu 8
stress-ng: info: [1] defaulting to a 86400 second run per stressor
stress-ng: info: [1] dispatching hogs: 8 cpu

在来观测此前运行一个容器时候的stress,现在已经掉到266%左右

[root@linuxEA-145 ~]$ docker stats stress

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
dc3c61506fb9        stress              265.84%             17.14MiB / 3.848GiB   0.44%               1.07kB / 42B        0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
dc3c61506fb9        stress              267.23%             17.14MiB / 3.848GiB   0.44%               1.07kB / 42B        0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
dc3c61506fb9        stress              267.23%             17.14MiB / 3.848GiB   0.44%               1.07kB / 42B        0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
dc3c61506fb9        stress              265.52%             17.14MiB / 3.848GiB   0.44%               1.07kB / 42B        0B / 0B            

而stress1的使用是133%

[root@linuxEA-145 ~]$ docker stats stress1

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
2db9d935886c        stress1             133.23%             17.16MiB / 3.848GiB   0.44%               1.07kB / 42B        0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
2db9d935886c        stress1             132.16%             17.16MiB / 3.848GiB   0.44%               1.07kB / 42B        0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
2db9d935886c        stress1             132.16%             17.16MiB / 3.848GiB   0.44%               1.07kB / 42B        0B / 0B             9

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
2db9d935886c        stress1             133.08%             17.16MiB / 3.848GiB   0.44%               1.07kB / 42B        0B / 0B             

CPU资源被两个容器动态瓜分

在资源紧张的情况下,可以调整--oom-kill-disadble禁用被kill,和--oom-score-adj调整优先级来尽量避免优先被kill

相关文章

LeaferJS 1.0 重磅发布:强悍的前端 Canvas 渲染引擎
10分钟搞定支持通配符的永久有效免费HTTPS证书
300 多个 Microsoft Excel 快捷方式
一步步配置基于kubeadmin的kubevip高可用
istio全链路传递cookie和header灰度
REST Web 服务版本控制

发布评论