實操:docker資源控制


doker通常只針對CPU、內存、IO資源控制

一: docker資源控制——CPU

CPU使用率控制

1.1 查看當前容器CPU限制狀態

cat /sys/fs/cgroup/cpu/docker/容器ID/cpu.cfs_quota_us

-1代表cpu資源使用是不受限制的,上限是硬件

[root@kibana ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
dd5782cde89d        centos:7            "/bin/bash"         48 minutes ago      Up 48 minutes                           test3
de37ff32e2c4        centos:7            "/bin/bash"         2 hours ago         Up 2 hours                              test1
[root@kibana docker]# cd /sys/
[root@kibana sys]# ls
block  bus  class  dev  devices  firmware  fs  hypervisor  kernel  module  power
[root@kibana sys]# cd fs
[root@kibana fs]# ls
cgroup  pstore  selinux  xfs
[root@kibana fs]# cd cgroup/
[root@kibana cgroup]# ls
blkio  cpu  cpuacct  cpu,cpuacct  cpuset  devices  freezer  hugetlb  memory  net_cls  net_cls,net_prio  net_prio  perf_event  pids  systemd
[root@kibana cgroup]# ll cpu
lrwxrwxrwx. 1 root root 11 Apr 16 09:27 cpu -> cpu,cpuacct
[root@kibana cgroup]# cd cpu
[root@kibana cpu]# ls
cgroup.clone_children  cgroup.sane_behavior  cpuacct.usage_percpu  cpu.rt_period_us   cpu.stat       notify_on_release  tasks
cgroup.event_control   cpuacct.stat          cpu.cfs_period_us     cpu.rt_runtime_us  docker         release_agent      user.slice
cgroup.procs           cpuacct.usage         cpu.cfs_quota_us      cpu.shares         machine.slice  system.slice
[root@kibana cpu]# cat cpu.cfs_quota_us 
-1
[root@kibana cpu]# cd docker/
[root@kibana docker]# ls
cgroup.clone_children  cpuacct.usage         cpu.rt_period_us   dd5782cde89d7084de43e8c343f6cbbe99e5b12226e037b7e5529a35d05bce55
cgroup.event_control   cpuacct.usage_percpu  cpu.rt_runtime_us  de37ff32e2c45188a4fd5d87c20dbbb0db8eeea13583e515b89a90a66ad3e7b1
cgroup.procs           cpu.cfs_period_us     cpu.shares         notify_on_release
cpuacct.stat           cpu.cfs_quota_us      cpu.stat           tasks
[root@kibana docker]# cat cpu.cfs_quota_us 
-1
[root@kibana docker]# cd dd5782cde89d7084de43e8c343f6cbbe99e5b12226e037b7e5529a35d05bce55/
[root@kibana dd5782cde89d7084de43e8c343f6cbbe99e5b12226e037b7e5529a35d05bce55]# ls
cgroup.clone_children  cgroup.procs  cpuacct.usage         cpu.cfs_period_us  cpu.rt_period_us   cpu.shares  notify_on_release
cgroup.event_control   cpuacct.stat  cpuacct.usage_percpu  cpu.cfs_quota_us   cpu.rt_runtime_us  cpu.stat    tasks
[root@kibana dd5782cde89d7084de43e8c343f6cbbe99e5b12226e037b7e5529a35d05bce55]# cat cpu.cfs_quota_us 
-1

備註:

cpu週期 1s 100000

按照cpu時間週期分配

cpu在一個時刻,只能給一個進程佔用

cpu的衡量單位是秒

1.2 對容器cpu的限制方法

以20%爲例

1.2.1 創建時指定限制

[root@kibana ~]# docker run -itd --name tese13 --net net172.18/16 --ip 172.18.0.11 --cpu-quota 20000 centos:7 /bin/bash
7bee273b367147dd9ef7946229ad316f12b095be268ef2c1e8a5078ba47f7ed6
[root@kibana ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7bee273b3671        centos:7            "/bin/bash"         3 seconds ago       Up 2 seconds                            tese13
dd5782cde89d        centos:7            "/bin/bash"         About an hour ago   Up About an hour                        test3
de37ff32e2c4        centos:7            "/bin/bash"         2 hours ago         Up 2 hours                              test1

或者

1.2.2 修改文件參數

[root@kibana ~]# echo 20000 > /sys/fs/cgroup/cpu/docker/7bee273b367147dd9ef7946229ad316f12b095be268ef2c1e8a5078ba47f7ed6/cpu.cfs_quota_us 
[root@kibana ~]#echo 20000 》

可以使用–help查看命令

[root@kibana ~]# docker run --help

對容器CPU進行BC壓力測試,測試之前新開一臺終端,top,然後按1

[root@kibana ~]# docker exec -it tese13 /bin/bash
[root@7bee273b3671 /]# yum install bc -y 

在這裏插入圖片描述

[root@7bee273b3671 /]# echo "scale=5000;4*a(1)" | bc -l -q

解釋:a是bc的一個內置函數,代表反正切arctan,由於 tan(pi/4)=1,於是4*arctan(1) = pi

在這裏插入圖片描述

bc的cpu佔用率被限制在20

1.2.3 創建時按比例分配CPU資源

創建兩個容器爲C1和C2,若只有這兩個容器,設置容器的權重,使得C1和C2的CPU資源佔比爲33.3%和66.7%

[root@kibana ~]# docker run -itd --name c1 --network net172.18/16 --ip 172.18.0.21 --cpu-shares 512 centos:7 /bin/bash
9128e8cb60893a90eaf6575e7b36f104574bf22282311553a19b7dc56cf77045
[root@kibana ~]# docker run -itd --name c2 --network net172.18/16 --ip 172.18.0.22 --cpu-shares 512 centos:7 /bin/bash
b85c3fd567e8d30c4888243ee5f05167bf89e4cbb43a5b6d5499b81914775751
docker run -itd --name c1 --cpu-shares 512 centos:7

docker run -itd --name c2 --cpu-shares 1024 centos:7
[root@kibana ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
b85c3fd567e8        centos:7            "/bin/bash"         48 seconds ago       Up 47 seconds                           c2
9128e8cb6089        centos:7            "/bin/bash"         About a minute ago   Up About a minute                       c1
7bee273b3671        centos:7            "/bin/bash"         59 minutes ago       Up 59 minutes                           tese13
dd5782cde89d        centos:7            "/bin/bash"         2 hours ago          Up 2 hours                              test3
de37ff32e2c4        centos:7            "/bin/bash"         3 hours ago          Up 3 hours                              test1

分別進容器測試,在top查看,或者docker stats 查看狀態

[root@kibana ~]# docker stats
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
b85c3fd567e8        c2                  0.00%               392KiB / 1.781GiB   0.02%               648B / 0B           0B / 0B             1
9128e8cb6089        c1                  0.00%               392KiB / 1.781GiB   0.02%               648B / 0B           0B / 0B             1
7bee273b3671        tese13              0.00%               272KiB / 1.781GiB   0.01%               14.9MB / 123kB      0B / 0B             1
dd5782cde89d        test3               0.00%               944KiB / 1.781GiB   0.05%               15.1MB / 175kB      0B / 0B             3
de37ff32e2c4        test1               0.00%               80KiB / 1.781GiB    0.00%               15.1MB / 158kB      0B / 0B             1

進入c1,c2,安裝壓力測試軟件stress

[root@kibana ~]# docker exec -it c1 /bin/bash
[root@9128e8cb6089 /]# yum install epel-release -y
[root@9128e8cb6089 /]# yum install stress -y
[root@kibana ~]# docker exec -it c2 /bin/bash
[root@b85c3fd567e8 /]# yum install epel-release -y
[root@b85c3fd567e8 /]# yum install stress -y

c1,c2測試

[root@b85c3fd567e8 /]# stress -c 4
[root@9128e8cb6089 /]# stress -c 4

在這裏插入圖片描述

在這裏插入圖片描述

1.2.4 限制容器使用指定的CPU

當前cpu核數爲4核

驗證方法:top 然後按1檢查

在這裏插入圖片描述

[root@kibana ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
b85c3fd567e8        centos:7            "/bin/bash"         About an hour ago   Exited (137) 37 minutes ago                       c2
9128e8cb6089        centos:7            "/bin/bash"         About an hour ago   Exited (137) 37 minutes ago                       c1
7bee273b3671        centos:7            "/bin/bash"         2 hours ago         Exited (137) 37 minutes ago                       tese13
dd5782cde89d        centos:7            "/bin/bash"         4 hours ago         Exited (137) 37 minutes ago                       test3
de37ff32e2c4        centos:7            "/bin/bash"         4 hours ago         Exited (137) 37 minutes ago                       test1

批量刪除容器

[root@kibana ~]# docker ps -a | awk '{print "docker rm "$1}' | bash
Error: No such container: CONTAINER
b85c3fd567e8
9128e8cb6089
7bee273b3671
dd5782cde89d
de37ff32e2c4
[root@kibana ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

創建容器,指定cpuID給容器test02

[root@kibana ~]# docker run -idt --name test02 --cpuset-cpus 1,3 centos:7 /bin/bash
ad53ee764486d1b26ff664b62dc6b2056269d605967e45799f1b369d08cb4c4b
[root@kibana ~]# 

進入容器,壓測檢查

[root@ad53ee764486 /]# yum install stress epel-release -y
[root@ad53ee764486 /]# stress -c 4

在這裏插入圖片描述

top 然後按1檢查

只有指定的內核在運轉

二: docker資源控制——Mem

限制內存使用

docker run -itd --name testm -m 512m centos:7 /bin/bash

[root@kibana ~]# docker run -itd --name testm -m 512m centos:7 /bin/bash
[root@kibana ~]# docker stats

在這裏插入圖片描述

進入容器驗證

[root@fc4e53fcf5c9 /]# yum install epel* -y
[root@fc4e53fcf5c9 /]# yum install stress -y
[root@fc4e53fcf5c9 /]# stress -m 512m --vm 2

三:docker資源控制——IO

對block的IO進行限制

控制數據量用的多

控制IO次數用的不太多

–device-read-bps:限制讀某個設備的bps(數據量)

例:docker run -d --device-read-bps /dev/sda:30M centos:7

–device-write-bps:限制某個寫入設備的bs(數據量)

例:docker run -d --device-write-bps /dev/sda:30 centos:7

–device-read-iops:限制讀某個設備的iops(次數)

–device-write-iops:限制寫入某個設備的iops(次數)

[root@kibana ~]# docker run -tid --name testi --device-read-bps /dev/sda:30M centos:7 /bin/bash
[root@kibana ~]# docker stats

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章