溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Docker第八回(docker資源限制和驗證)

發(fā)布時間:2020-07-17 07:04:07 來源:網(wǎng)絡 閱讀:861 作者:yacai1990 欄目:云計算

一、docker資源限制

docker能夠運行起來要依賴于內(nèi)核中的兩個特性,namespaces和CGroups。默認情況下,容器是沒有任何資源限制的,因此它能夠耗盡主機上內(nèi)核能分配給該容器的所有資源。因此,為了防止一個容器的運行中耗盡主機所有的資源,就需要用到資源限制。而資源限制的一些功能特性需要linux 內(nèi)核支持 Linux  Capabilities,在docker 1.13版本之前,只支持CFS schedule(Completely Fair Scheduler 完全公平調(diào)度器),之后的版本還支持realtime schedule


CFS schedule:每個進程都有優(yōu)先級,非實時進程的優(yōu)先級從100-139,CSF schedule是用來調(diào)度這些非實時進程的調(diào)度器,優(yōu)先級高的進程會先被cpu執(zhí)行

realtime schedule:實時進程調(diào)度器,進程的優(yōu)先級從0-99,realtime schedule是專門調(diào)度實時進程的調(diào)度器


二、docker的memory、cpu資源限制參數(shù)


1、cpu限制

--cpus=<value>:指定一個容器可以使用多少可用cpu資源,如果是4核cpu,可以設(shè)置為1.5,那么該容器最多只能使用1.5核的cpu資源,如果沒有設(shè)置--cpuset-cpus,那么可以使用的1.5核可以是任意一個核心的資源。此選項只能在docker1.3以上版本中使用

--cpu-shares:為容器按比例分配cpu資源,如果其他容器的cpu資源是空閑,那么容器1如果需要,將會使用所有cpu的資源,且將任務分配到任意核心處理

--cpuset-cpus:為容器指定可以使用的cpu核心是哪個,如果cpu是4和,那么按照編號0-3區(qū)分每一個核心,此參數(shù)設(shè)置為0,1即表示可以使用cpu的第一個和第二個核心。


2、memory以及swap限制

--memory=<value>:為容器指定最多可以使用多少內(nèi)存,如果一個進程使用的內(nèi)存超過了限制,那么可能會被kill掉

--memory-swap:為容器指定最多可以使用多少swap空間,此選項必須要在使用了--memory參數(shù)的前提下才能使用,如果沒有設(shè)置--memory參數(shù),那么這個參數(shù)不會生效

Docker第八回(docker資源限制和驗證)

-- memory-swappiness:設(shè)置容器使用swap的傾向性有多大,0-100。

-- memory-reservation:容器使用內(nèi)存的軟限制,這個指一定要設(shè)置的比--memory小,當系統(tǒng)內(nèi)存緊張時,會回收掉此容器的memory值-reservation值的內(nèi)存,讓容器的內(nèi)存使用降到reservation的標準

--oom-kill-disable:當容器內(nèi)進程發(fā)生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、測試內(nèi)存

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使用已經(jīng)滿了


1.2、重新啟動容器加入內(nèi)存限制參數(shù)

[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

設(shè)置的參數(shù)生效


2、測試內(nèi)存

2.1、不限制內(nèi)存使用,壓測指定了2個內(nèi)存,每個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內(nèi)存


2.2、重新啟動容器,加入內(nèi)存限制

--memory限制容器只能使用128m內(nèi)存

[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


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI