一、docker資源控制
1、資源控制工具
Docker 通過 Cgroup 來控制容器使用的資源配額,包括 CPU、內(nèi)存、磁盤三大方面, 基本覆蓋了常見的資源配額和使用量控制。
Cgroup 是 ControlGroups 的縮寫,是 Linux 內(nèi)核提供的一種可以限制、記錄、隔離進程組所使用的物理資源(如 CPU、內(nèi)存、磁盤 IO 等等) 的機制,被 LXC、docker 等很多項目用于實現(xiàn)進程資源控制。Cgroup 本身是提供將進程進行分組化管理的功能和接口的基礎(chǔ)結(jié)構(gòu),I/O 或內(nèi)存的分配控制等具體的資源管理是通過該功能來實現(xiàn)的。
2、Cgroups四大功能
資源限制:可以對任務(wù)使用的資源總額進行限制
優(yōu)先級分配:通過分配的cpu時間片數(shù)量以及磁盤IO帶寬大小,實際上相當于控制了任務(wù)運行優(yōu)先級
資源統(tǒng)計:可以統(tǒng)計系統(tǒng)的資源使用量,如cpu時長,內(nèi)存用量等
任務(wù)控制:cgroup可以對任務(wù)執(zhí)行掛起、恢復(fù)等操作
二、CPU 資源控制
1、設(shè)置CPU使用率上限
Linux通過CFS(Completely Fair Scheduler,完全公平調(diào)度器)來調(diào)度各個進程對CPU的使用。CFS默認的調(diào)度周期是100ms。
CPU周期:指的是CFS調(diào)度CPU遍歷處理一次容器所有的進程時長,默認是0.1s。設(shè)置范圍為1ms~1s
Cgroups限制時間:使用cpu.cfs_quota_us 即可設(shè)置在每個周期內(nèi)容器能使用的CPU的時長,默認是-1即不限制。
CPU利用率:Cgroups限制時間/CPU周期,默認Cgroups是-1而CPU周期為0.1s表示用滿CPU
查看CPU默認配置:
2、CPU壓力測試
docker run -itd --name c1 centos:7 /bin/bash
docker ps -a
#查看docker容器id
docker exec -it 容器唯一id/容器名 /bin/bash
#進入容器中
vi cpu.sh
#!/bin/sh
i=0
while true
do
let i++
done
#編輯腳本
chmod +x /test.sh
#添加執(zhí)行權(quán)限
./cpu.sh
#執(zhí)行腳本
新開一個shell腳本
top查看cpu占用率,按1查看使用的那個cpu
[root@localhost opt]# docker run -itd --name c1 centos:7 /bin/bash
078e66e73513524199fc489f49c37631441149bc3c78b61e520e2a98f194a49d
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
078e66e73513 centos:7 "/bin/bash" 7 seconds ago Up 6 seconds c1
b4283606c3ca centos:7 "/bin/bash" 24 minutes ago Up 24 minutes test2
b4612ad91dc2 centos:7 "/bin/bash" 59 minutes ago Up 59 minutes test1
[root@localhost opt]# docker exec -it 078e66e73513 /bin/bash
[root@078e66e73513 /]# vim /cpu.sh
bash: vim: command not found
[root@078e66e73513 /]# vim cpu.sh
bash: vim: command not found
[root@078e66e73513 /]# chmod +x /cpu.sh
chmod: cannot access '/cpu.sh': No such file or directory
[root@078e66e73513 /]# vi /cpu.sh
[root@078e66e73513 /]# chmod +x cpu.sh
[root@078e66e73513 /]# chmod +x /cpu.sh
[root@078e66e73513 /]# ./cpu.sh
3、Cgroups限制cpu使用率
[root@localhost opt]# docker run -itd --name c2 --cpu-quota 50000 centos:7 /bin/bash
b32af889eec23e57f908d050de615334f68ccdb4bab50c478785917adc4be79e
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b32af889eec2 centos:7 "/bin/bash" 51 seconds ago Up 50 seconds c2
078e66e73513 centos:7 "/bin/bash" 15 minutes ago Up 15 minutes c1
b4283606c3ca centos:7 "/bin/bash" 39 minutes ago Up 39 minutes test2
b4612ad91dc2 centos:7 "/bin/bash" About an hour ago Up About an hour test1
[root@localhost opt]# docker exec -it b32af889eec2 /bin/b
ash
[root@b32af889eec2 /]# vi /cpu.sh
#!/bin/bash
i=0
while true
do
let i++
done
[root@b32af889eec2 /]# chmod +x /cpu.sh
[root@b32af889eec2 /]# ./cpu.sh
4、設(shè)置CPU資源占用比(設(shè)置多個容器時才有效)
Docker 通過 --cpu-shares 指定 CPU 份額,默認值為1024,值為1024的倍數(shù)。
創(chuàng)建兩個容器為 c1 和 c2,若只有這兩個容器,設(shè)置容器的權(quán)重,使得c1和c2的CPU資源占比為1/3和2/3。
docker run -itd --name c1 --cpu-shares 1024 centos:7
docker run -itd --name c2 --cpu-shares 2048 centos:7
分別進入容器,進行壓力測試
yum install -y epel-release
yum install -y stress
stress -c 4 #產(chǎn)生四個進程,每個進程都反復(fù)不停的計算隨機數(shù)的平方根
[root@localhost opt]# docker run -itd --name test3 --cpu-shares 1024 centos:7
e73ea5e41483580cd5b818b719dd15adbaf1de39cbed98cef176fe2c40dde1db
[root@localhost opt]# docker run -itd --name test4 --cpu-shares 2048 centos:7
bf3224819662e932bfe1eb16de2cf1781c93741201108867e409b1ac52db9d80
[root@localhost opt]# yum install -y epel-release 安裝EPEL軟件倉庫
[root@localhost opt]# yum -y install stress
[root@localhost opt]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf3224819662 centos:7 "/bin/bash" 19 minutes ago Up 19 minutes test4
e73ea5e41483 centos:7 "/bin/bash" 19 minutes ago Up 19 minutes test3
#進入容器寫一個死循環(huán)腳本,運行起來
[root@localhost opt]# docker exec -it test4 bash
[root@bf3224819662 /]# vi /cpu.sh
#!/bin/bash
i=0
while true
do
let i++
done
[root@bf3224819662 /]# chmod +x /cpu.sh
[root@bf3224819662 /]# ./cpu.sh
[root@localhost ~]# docker exec -it test3 bash
[root@e73ea5e41483 /]# vi /cpu.sh
#!/bin/bash
i=0
while true
do
let i++
done
[root@e73ea5e41483 /]# chmod +x /cpu.sh
[root@e73ea5e41483 /]# ./cpu.sh
另外打開一個開端使用top命令查看或者使用docker stats查看cpu占比;
5、設(shè)置容器綁定指定的CPU
#先分配虛擬機4個CPU核數(shù)
[root@localhost opt]# docker run -itd --name test5 --cpuset-cpus 1,3 centos:7 /bin/bash
4a9fe0005c13d89411b87af79daff288953afa91237c8d02855ed8d26f0e15fb
#進入容器,進行壓力測試
[root@localhost opt]# docker exec -it test5 bash
[root@4a9fe0005c13 /]# yum install -y epel-release
[root@4a9fe0005c13 /]# yum -y install stress
[root@4a9fe0005c13 /]# stress -c 4
另外打開一個終端執(zhí)行 top 命令再按 1 查看CPU使用情況。
三、對內(nèi)存使用的限制
//-m(–memory=) 選項用于限制容器可以使用的最大內(nèi)存
[root@localhost opt]# docker run -itd --name test6 -m 512m centos:7 /bin/bash
docker stats
//限制可用的 swap 大小, --memory-swap
強調(diào)一下,--memory-swap 是必須要與 --memory 一起使用的。
正常情況下,--memory-swap 的值包含容器可用內(nèi)存和可用 swap。
所以 -m 300m --memory-swap=1g 的含義為:容器可以使用 300M 的物理內(nèi)存,并且可以使用 700M(1G - 300)的 swap。
如果 --memory-swap 設(shè)置為 0 或者 不設(shè)置,則容器可以使用的 swap 大小為 -m 值的兩倍。
如果 --memory-swap 的值和 -m 值相同,則容器不能使用 swap。
如果 --memory-swap 值為 -1,它表示容器程序使用的內(nèi)存受限,而可以使用的 swap 空間使用不受限制(宿主機有多少 swap 容器就可以使用多少)。
四、對磁盤IO配額控制(blkio)的限制
--device-read-bps:限制某個設(shè)備上的讀速度bps(數(shù)據(jù)量),單位可以是kb、mb(M)或者gb。
例:docker run -itd --name test7 --device-read-bps /dev/sda:1M centos:7 /bin/bash
--device-write-bps : 限制某個設(shè)備上的寫速度bps(數(shù)據(jù)量),單位可以是kb、mb(M)或者gb。
例:docker run -itd --name test8 --device-write-bps /dev/sda:1mb centos:7 /bin/bassh
--device-read-iops :限制讀某個設(shè)備的iops(次數(shù))
--device-write-iops :限制寫入某個設(shè)備的iops(次數(shù))
文章來源:http://www.zghlxwxcb.cn/news/detail-688266.html
#創(chuàng)建容器,并限制寫速度
[root@localhost opt]# docker run -itd --name test9 --device-write-bps /dev/sda:1MB centos:7 /bin/bash
#通過dd來驗證寫速度
[root@localhost opt]# docker exec -it test9 bash #進入容器限制容器的寫速度
dd if=/dev/zero of=test.out bs=1M count=10 oflag=direct #添加oflag參數(shù)以規(guī)避掉文件系統(tǒng)cache
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 10.0025 s, 1.0 MB/s
文章來源地址http://www.zghlxwxcb.cn/news/detail-688266.html
1、#清理docker占用的磁盤空間
docker system prune -a #可以用于清理磁盤,刪除關(guān)閉的容器、無用的數(shù)據(jù)卷和網(wǎng)絡(luò)
到了這里,關(guān)于Docker資源控制cgroups的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!