Docker第八回(docker资源限制和验证)
admin
2023-04-10 14:23:15
0

一、docker资源限制

docker能够运行起来要依赖于内核中的两个特性,namespaces和CGroups。默认情况下,容器是没有任何资源限制的,因此它能够耗尽主机上内核能分配给该容器的所有资源。因此,为了防止一个容器的运行中耗尽主机所有的资源,就需要用到资源限制。而资源限制的一些功能特性需要linux 内核支持 Linux  Capabilities,在docker 1.13版本之前,只支持CFS schedule(Completely Fair Scheduler 完全公平调度器),之后的版本还支持realtime schedule


CFS schedule:每个进程都有优先级,非实时进程的优先级从100-139,CSF schedule是用来调度这些非实时进程的调度器,优先级高的进程会先被cpu执行

realtime schedule:实时进程调度器,进程的优先级从0-99,realtime schedule是专门调度实时进程的调度器


二、docker的memory、cpu资源限制参数


1、cpu限制

--cpus=:指定一个容器可以使用多少可用cpu资源,如果是4核cpu,可以设置为1.5,那么该容器最多只能使用1.5核的cpu资源,如果没有设置--cpuset-cpus,那么可以使用的1.5核可以是任意一个核心的资源。此选项只能在docker1.3以上版本中使用

--cpu-shares:为容器按比例分配cpu资源,如果其他容器的cpu资源是空闲,那么容器1如果需要,将会使用所有cpu的资源,且将任务分配到任意核心处理

--cpuset-cpus:为容器指定可以使用的cpu核心是哪个,如果cpu是4和,那么按照编号0-3区分每一个核心,此参数设置为0,1即表示可以使用cpu的第一个和第二个核心。


2、memory以及swap限制

--memory=:为容器指定最多可以使用多少内存,如果一个进程使用的内存超过了限制,那么可能会被kill掉

--memory-swap:为容器指定最多可以使用多少swap空间,此选项必须要在使用了--memory参数的前提下才能使用,如果没有设置--memory参数,那么这个参数不会生效

Docker第八回(docker资源限制和验证)

-- memory-swappiness:设置容器使用swap的倾向性有多大,0-100。

-- memory-reservation:容器使用内存的软限制,这个指一定要设置的比--memory小,当系统内存紧张时,会回收掉此容器的memory值-reservation值的内存,让容器的内存使用降到reservation的标准

--oom-kill-disable:当容器内进程发生oom时,是否杀掉该容器


三、使用压测工具进行测试

[root@bogon ~]# 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

1、测试内存

1.1、不限制cpu使用

[root@bogon ~]# docker container run --name stress -it --rm lorel/docker-stress-ng:latest  --cpu 8
stress-ng: info: [1] defaulting to a 86400 second run per stressor
stress-ng: info: [1] dispatching hogs: 8 cpu

[root@bogon ~]# docker stats
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
92b0b8d916c1        stress              101.54%             15.81MiB / 983.3MiB   1.61%               648B / 0B           0B / 0B             9

[root@bogon ~]# top
top - 19:15:49 up 2 days,  2:38,  2 users,  load average: 7.02, 3.00, 1.15
Tasks: 131 total,  10 running, 121 sleeping,   0 stopped,   0 zombie
%Cpu(s): 99.7 us,  0.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1006892 total,   100680 free,   320704 used,   585508 buff/cache
KiB Swap:  2097148 total,  2096628 free,      520 used.   422732 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                     
40035 root      20   0    6908   4180    252 R 12.6  0.4   0:12.79 stress-ng-cpu                                                               
40037 root      20   0    6908   4180    252 R 12.6  0.4   0:12.78 stress-ng-cpu                                                               
40038 root      20   0    6908   2136    252 R 12.6  0.2   0:12.78 stress-ng-cpu                                                               
40040 root      20   0    6908   2136    252 R 12.6  0.2   0:12.78 stress-ng-cpu                                                               
40036 root      20   0    6908   2136    252 R 12.3  0.2   0:12.77 stress-ng-cpu                                                               
40039 root      20   0    6908   2136    252 R 12.3  0.2   0:12.78 stress-ng-cpu                                                               
40041 root      20   0    6908   4180    252 R 12.3  0.4   0:12.77 stress-ng-cpu                                                               
40042 root      20   0    6908   2136    252 R 12.3  0.2   0:12.77 stress-ng-cpu                                                               
    1 root      20   0  128484   7208   4196 S  0.0  0.7   0:10.12 systemd

可以看到,cpu使用已经满了


1.2、重新启动容器加入内存限制参数

[root@bogon ~]# docker container run --name stress --cpus=0.5 -it --rm lorel/docker-stress-ng:latest  --cpu 8
stress-ng: info: [1] defaulting to a 86400 second run per stressor
stress-ng: info: [1] dispatching hogs: 8 cpu

[root@bogon ~]# docker stats
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
845220ef9982        stress              51.57%              20.05MiB / 983.3MiB   2.04%               648B / 0B           0B / 0B             9

设置的参数生效


2、测试内存

2.1、不限制内存使用,压测指定了2个内存,每个128m

[root@bogon ~]# docker container run --name stress -it --rm lorel/docker-stress-ng:latest  --vm 2 --vm-bytes 128m
stress-ng: info: [1] defaulting to a 86400 second run per stressor
stress-ng: info: [1] dispatching hogs: 2 vm

[root@bogon ~]# docker stats
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
beb3cfa10748        stress              99.29%              256.2MiB / 983.3MiB   26.05%              648B / 0B           0B / 0B             5

而实际使用了256M内存


2.2、重新启动容器,加入内存限制

--memory限制容器只能使用128m内存

[root@bogon ~]# docker container run --name stress -it --memory=128m --rm lorel/docker-stress-ng:latest  --vm 2 --vm-bytes 128m
stress-ng: info: [1] defaulting to a 86400 second run per stressor
stress-ng: info: [1] dispatching hogs: 2 vm

[root@bogon ~]# docker stats
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
decee18cb471        stress              99.47%              126.4MiB / 128MiB   98.77%              648B / 0B           3.19MB / 461MB      5


相关内容

热门资讯

两架小型飞机在德国上空发生碰撞... 【环球网报道 记者 闫珮云】据俄罗斯《消息报》当地时间11日援引德新社消息报道,两架小型飞机于10日...
190元榴莲遭“仅退款”,卖家... 近日,河南榴莲卖家程先生驱车1600公里跨省维权引发舆论关注。买家收到190元榴莲后,以“发霉”为由...
日本恩格尔系数创1980年以来... 日本总务省12日公布的数据显示,反映家庭消费支出中食品支出占比的“恩格尔系数”在2025年度达到28...
特朗普时隔9年再访华,回顾中美... 澎湃新闻记者 彭玉洁
特朗普要来了,我们且淡定 世间大事,从来都是于风云变幻中悄然落笔,于相逢相聚时定格走向。接下来几天的北京,将格外热闹。我们已正...
“蔡英文御用设计师”靠标案捞钱... 海峡导报综合报道 台电换掉书法名家于右任的“台湾电力公司”草书LOGO(标志),换成亲绿设计师聂永真...
客厅墙面受潮鼓起怎么修复 1、如果想要修补墙面鼓包,首先需要将鼓包的地方全部铲除干净,露出里面的水泥墙。接下来可以用树脂的填料...
客厅地面如何翻新 1、假如是由于灰尘污染等而导致家里地砖陈旧的话,那么应该使用清洁剂把污渍清洗掉。若地砖边缘很黑的话,...
led灯维修大概多少钱 LED 灯维修的费用取决于多种因素,包括灯具的类型、故障的性质和严重程度、维修服务的提供商以及所在地...