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

1、docker資源限制linux

docker可以運行起來要依賴於內核中的兩個特性,namespaces和CGroups。默認狀況下,容器是沒有任何資源限制的,所以它可以耗盡主機上內核能分配給該容器的全部資源。所以,爲了防止一個容器的運行中耗盡主機全部的資源,就須要用到資源限制。而資源限制的一些功能特性須要linux 內核支持 Linux  Capabilities,在docker 1.13版本以前,只支持CFS schedule(Completely Fair Scheduler 徹底公平調度器),以後的版本還支持realtime scheduledocker


CFS schedule:每一個進程都有優先級,非實時進程的優先級從100-139,CSF schedule是用來調度這些非實時進程的調度器,優先級高的進程會先被cpu執行bash

realtime schedule:實時進程調度器,進程的優先級從0-99,realtime schedule是專門調度實時進程的調度器
app


2、docker的memory、cpu資源限制參數ide


一、cpu限制工具

--cpus=<value>:指定一個容器可使用多少可用cpu資源,若是是4核cpu,能夠設置爲1.5,那麼該容器最多隻能使用1.5核的cpu資源,若是沒有設置--cpuset-cpus,那麼可使用的1.5核能夠是任意一個核心的資源。此選項只能在docker1.3以上版本中使用
測試

--cpu-shares:爲容器按比例分配cpu資源,若是其餘容器的cpu資源是空閒,那麼容器1若是須要,將會使用全部cpu的資源,且將任務分配到任意核心處理spa

--cpuset-cpus:爲容器指定可使用的cpu核心是哪一個,若是cpu是4和,那麼按照編號0-3區分每個核心,此參數設置爲0,1即表示可使用cpu的第一個和第二個核心。
blog


二、memory以及swap限制進程

--memory=<value>:爲容器指定最多可使用多少內存,若是一個進程使用的內存超過了限制,那麼可能會被kill掉

--memory-swap:爲容器指定最多可使用多少swap空間,此選項必需要在使用了--memory參數的前提下才能使用,若是沒有設置--memory參數,那麼這個參數不會生效

image.png

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

-- memory-reservation:容器使用內存的軟限制,這個指必定要設置的比--memory小,當系統內存緊張時,會回收掉此容器的memory值-reservation值的內存,讓容器的內存使用降到reservation的標準

--oom-kill-disable:當容器內進程發生oom時,是否殺掉該容器


3、使用壓測工具進行測試

[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.一、不限制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.二、從新啓動容器加入內存限制參數

[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個內存,每一個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.二、從新啓動容器,加入內存限制

--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
相關文章
相關標籤/搜索