1. 程式人生 > 實用技巧 >第7章 docker的資源限制

第7章 docker的資源限制

  docker容器技術底層是通過Cgroup實現容器對物理資源使用的限制,限制資源包括CPU,記憶體,磁碟三個 方面,基本覆蓋了常見的資源配額和使用控制。

  我們需要用docker來建立一個基於centos的stress工具映象,這個工具包可以實現centos的一些測試,並且在容器中需要有一個centos的映象

docker製作映象

  1.製作映象

  • 建立一個目錄,為映象做基礎環境,製作映象
  • [root@localhost ~]# mkdir stress/
    [root@localhost ~]# cd stress/
    [root@localhost stress]# cat Dockerfile 
    FROM centos
    MAINTAINER Carrie "[email protected]"
    RUN yum -y install wget
    RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    
    FROM centos ------>從哪裡獲取
    MAINTAINER Carrie "[email protected]" ----->作者資訊
    RUN起來的都是映象中帶的命令 RUN yum -y install wget RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  • 建立映象 並指定名字 (./ 意思為當前環境進行構建)

  • [root@localhost stress]# docker build -t centos:stress ./
    
  • 建立成功之後檢視一下
  • [root@localhost stress]# docker images
    REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
    centos                     stress              2ce5a732f6f3        5 minutes ago       311MB
    

CPU資源限制

  1.CPU資源(使用率)限制

  在centos7中可以通過修改對應的cpu.crs_quota_us引數進行實現

    基於剛才建立好的映象執行一個容器,將其的CPU使用設定成20000,設定CPU的使用率為20%。

  • root@localhost ~]# echo "20000" > /sys/fs/cgroup/cpu,cpuacct/docker/73a49b54a9e0f25270a607af78c14ca275a16cde97ef974e43208c1d6d75c9e1/cpu.cfs_quota_us
    
  • 進行CPU限制的壓力測試
  • [root@localhost ~]# docker attach 73a
    [root@73a49b54a9e0 /]# stress -c 10      ------->開啟10個程序
    stress: info: [14] dispatching hogs: 10 cpu, 0 io, 0 vm, 0 hdd
    

  • 開啟10個

  • 開啟20 個

  • CPU的平均使用會降低

  2. cpu資源(cpu共享比例)

    當執行多個容器,很難技術算CPU的使用率,為了使容器合理的使用CPU的資源。可以通過--cpu-shares選項設定容器按比例共享CPU資源

  •  資源分配 stress-1:stress-2:stress-3=1:1:2
    [root@localhost ~]# docker run --name stress-1 -itd --cpu-shares 1024 centos:stress /bin/bash
    f0d5608696c01b0e0df34a30b8c8d664d1679e3f167b74bf23e763bf8229df5b
    [root@localhost ~]# docker run --name stress-2 -itd --cpu-shares 1024 centos:stress /bin/bash
    b336740b3fbf4c224fe40b200e17960fa770f1cd3a94f5b683864dd5b4f990cb
    [root@localhost ~]# docker run --name stress-3 -itd --cpu-shares 2048 centos:stress /bin/bash
    8eba7ba535df1a1f853cd03529daa3cb37e6c81675b63bc1a2bb43eb253f3a35
    [root@localhost ~]# 
    
  • 刪除
  • [root@localhost ~]# docker rm -f $(docker ps -a | awk '/centos:stress/ {print $1}')
    8eba7ba535df
    b336740b3fbf
    f0d5608696c0
    916c47bd4133
    73a49b54a9e0

  3.CPU週期性限制

  • docker 提供了--cpu-period,--cpu-quota 兩個引數控制容器可以分配CPU時鐘週期。
  • --cpu-period 是用來指定容器對cpu的使用要在多長時間進行重新分配。
  • --cpu-quota 是用來指定在這個週期內,最多可以多長時間來跑這個容器。
  • 優點是指定的值,沒有彈性

  4.CPU核心限制

  多核cpu的伺服器,docker還能控制容器執行限制使用那些CPU核心,可以使用--cpuset-cpus選項來使得某些程式獨享CPU核心。如果是16核,那CPU標號是0-15.

記憶體限制

  容器可使用的記憶體有兩種,實體記憶體核和交換分割槽,docker通過下面的兩組引數來控制容器記憶體的使用量。

  • -m :設定記憶體使用限額。例如100M
  • --memory-swap:設定記憶體swap的使用限額(--vm 1啟動一個工作執行緒,--vm-bytes 280M大小為280M)
  • [root@localhost ~]# docker run -it -m 200M --memory-swap=300M progrium/stress --vm 1 --vm-bytes 280M

Block IO 限制

  預設情況下,所有的容器能平等的讀寫磁碟,可以通過--blkio-weight引數來改變block IO的優先順序

  • --blkio-weight 設定權重值,預設為500
  • [root@localhost ~]# docker run -it --name=container_A --blkio-weight 300 centos:stress
    
    [root@localhost ~]# docker run -it --name=container_B --blkio-weight 600 centos:stress
    

bps和iops限制

  如果在一臺伺服器上進行容器的混合部署,那麼會存在同時幾個程式寫磁碟資料的情況,這時可以通過--device-write-iops選項來限制每秒的IO次數來限制制定裝置的寫速度,相應的還有--device-read-iops選項限制讀取IO的速度.

  • --device-write-bps
  • --device-read-bps
  • --device-write-iops
  • --device-read-iops