docker 的资源控制和数据管理
文章目錄
- 一: docker 的資源控制
- 1.1 CPU資源控制
- 1.1.1 設(shè)置cpu 使用率上限
- 1.1.2 進(jìn)行CPU 壓力測試
- 1.1.3 設(shè)置cpu 使用時間上限
- 1.1.4 設(shè)置cpu 資源占比(多個容器時才有效)
- 1.1.5 指定容器綁定cpu
- 1.2 對內(nèi)存使用的限制
- 1.3 對磁盤IO 配置控制的限制
- 二: 數(shù)據(jù)管理
- 2.1 數(shù)據(jù)卷
- 2.2 數(shù)據(jù)卷容器
- 2.3 容器互聯(lián)(使用centos 鏡像)
一: docker 的資源控制
1.1 CPU資源控制
cgroups ,是一個非常強(qiáng)大的 Linux 內(nèi)核工具,他不僅可以限制被 namespace 隔離起來的資源,還可以為資源設(shè)置權(quán)重,計算使用量,操控進(jìn)程啟停等等。
所有cgroups (Control groups) 實現(xiàn)了對資源的配額和度量。
cgroups 有四大功能
- 資源限制: 可以對任務(wù)使用的資源總額進(jìn)行限制
- **優(yōu)先級分配:**通過分配的 cpu 時間片數(shù)據(jù)以及磁盤IO 帶寬大小,實際上相當(dāng)于控制了任務(wù)運行優(yōu)先級
- **資源統(tǒng)計:**可以統(tǒng)計系統(tǒng)的資源使用量,如cpu 時長,內(nèi)存使用等
- 任務(wù)控制: cgroup 可以對任務(wù)執(zhí)行掛起,恢復(fù)等操作
1.1.1 設(shè)置cpu 使用率上限
Linux 通過CFS(Complete Fair Scheduler,完全公平調(diào)度器) 來調(diào)度各個進(jìn)程對cpu的使用。CFS默認(rèn)的調(diào)度器周期100ms,我們可以設(shè)置每個容器進(jìn)程的調(diào)度周期,以及在這個周期內(nèi)各個容器最多能使用多少CPU時間.
使用 --cpu-period 即可設(shè)置調(diào)度周期,使用–cpu-quota 即可設(shè)置在每個周期內(nèi)容器能使用的cpu 時間。兩者可以配合使用。而容器的CPU 配額必須不小于 1ms ,即 --cpu-quota 的額度必須 >=1000.
[root@host103 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ed597b354adf centos:7 "bash" 21 seconds ago Up 19 seconds test1 [root@host103 ~]# cd \ /sys/fs/cgroup/cpu/docker/ed597b354adf3d4cfd1b572cedb5d106077bcf62a31df44bc8aac3aa3deb5d1b/[root@host103 ed597.....]# cat cpu.cfs_quota_us -1 [root@host103 ed597.....]# cat cpu.cfs_period_us 100000cpu.cfs_quota_us: 表示 cgroups 限制占用時間(微秒)默認(rèn)為 -1 ,表示不限制
cpu.cfs_period_us: cpu 分配的周期(微秒,所以文件中用 us 表示),默認(rèn)為 100000
如果設(shè)置為 50000 ,表示占用 50000/100000= 50% cpu
1.1.2 進(jìn)行CPU 壓力測試
[root@host103 ~]# docker exec -it test1 /bin/bash [root@ed597b354adf /]# vi cpu.sh #!/bin/bash i=0 while true do let i++ done[root@ed597b354adf /]# chmod +x /cpu.sh [root@ed597b354adf /]# ./cpu.sh1.1.3 設(shè)置cpu 使用時間上限
[root@host103 ~]# cd /sys/fs/cgroup/cpu/docker/ed59...../ [root@host103 ed59.....]# echo 50000 > cpu.cfs_quota_us [root@host103 ed59.....]# top1.1.4 設(shè)置cpu 資源占比(多個容器時才有效)
docker 通過 --cpu-shares 指定CPU 份額,默認(rèn)值為1024 ,值為1024 的倍數(shù)。
#先停止和刪除之前容器 [root@host103 ~]# docker kill $(docker ps -aq) [root@host103 ~]# docker rm $(docker ps -aq)#創(chuàng)建兩個容器為c1 ,c2 若只有這兩個容器,設(shè)置容器的權(quán)重,使得c1和c2的cpu資源占比為 1/3和2/3 [root@host103 ~]# docker run -itd --name c1 --cpu-shares 1024 centos:7 47fa6089f568e616cba8cf05bf22279bd7ff0602cf31d4d685e3a37d4bd27d85 [root@host103 ~]# [root@host103 ~]# docker run -itd --name c2 --cpu-shares 2048 centos:7 25d0d2229f4ea05b5a290fd0a96f364b071826700826378b596e5cdf38f5e72c#分別進(jìn)入容器,進(jìn)行壓力測試 [root@host103 ~]# docker exec -it c1 bash [root@47fa6089f568 /]# yum -y install epel-release [root@47fa6089f568 /]# yum -y install stress #產(chǎn)生4個進(jìn)程,每個進(jìn)程都反復(fù)不聽的精算隨機(jī)數(shù)的平方根 [root@47fa6089f568 /]# stress -c 4[root@host103 ~]# docker exec -it c2 bash [root@25d0d2229f4e /]# yum -y install epel-release [root@25d0d2229f4e /]# yum -y install stress #產(chǎn)生4個進(jìn)程,每個進(jìn)程都反復(fù)不聽的精算隨機(jī)數(shù)的平方根 [root@25d0d2229f4e /]# stress -c 4#查看容器運行狀態(tài)(動態(tài)更新) [root@host103 ~]# docker stats CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 25d0d2229f4e c2 260.34% 137.2MiB / 1.938GiB 6.92% 27.8MB / 484kB 25.3MB / 25.3MB 7 47fa6089f568 c1 140.19% 111.8MiB / 1.938GiB 5.63% 27.5MB / 416kB 58.1MB / 25.3MB 71.1.5 指定容器綁定cpu
#配置容器使用編號為 1,3 的CPU(cpu編號從0開始) [root@host103 ~]# docker run -itd --name test3 --cpuset-cpus 1,3 centos:7 bash 90666bf2833d3b93bbcc043c66b5dff3f76ca43057aaacd3867d3d47f7e2d640#進(jìn)入容器,進(jìn)行壓力測試 [root@host103 ~]# docker exec -it test3 bash [root@90666bf2833d /]# yum -y install epel-release [root@90666bf2833d /]# yum -y install stress [root@90666bf2833d /]# stress -c 4#先使用top 命令,在按下1 ,查看CPU的使用情況 [root@host103 ~]# top1.2 對內(nèi)存使用的限制
-m (–memory=) 選項用于限制容器可以使用的最大內(nèi)存
–memory-swap 和 --memory 一起使用可以限制swap 的大小。
正常情況下,–memory-swap 的值包含容器的可用內(nèi)存和可用swap。
所以,-m 300m --memory-swap=1g 的含義為: 容器可以使用300M的物理內(nèi)存,并且可以使用 700M(1G-300M)的swap
- 如果 --memory-swap 設(shè)置為0或者 不設(shè)置,則容器可以使用swap 大小為 -m 值的兩倍
- 如果 --memory-swap 的值和 -m 值相同,則容器不能使用 swap
- 如果 --memory-swap 的值為 -1 ,它表示容器程序使用的內(nèi)存受限,而可以使用的swap空間不受限制(宿主機(jī)有多少swap,容器就可以使用多少)
1.3 對磁盤IO 配置控制的限制
–device-read-bps: 限制某個設(shè)備上的讀速度 bps(數(shù)據(jù)量),單位可以是kb,mb(M)或者gb。
eg: docker run -itd --name test --device-read-bps /dev/sda:1M centos:7 /bin/bash
–device-write-bps: 限制某個設(shè)備上的寫速度(數(shù)據(jù)量),單位可以是kb,mb(M)或者gb。
eg: docker run -itd --name test --device-write-bps /dev/sda:1M centos:7 /bin/bash
–device-read-iops : 限制讀某個設(shè)備的iops(次數(shù))
–device-write-iops: 限制寫入某個設(shè)備iops(次數(shù))
#創(chuàng)建容器,并限制寫速度 [root@host103 ~]# docker run -it --name test4 --device-write-bps /dev/sda:1mb centos:7 bash#通過dd來驗證寫速度 [root@98dbd982b24c /]# dd if=/dev/zero of=test.out bs=1M count=10 oflag=direct 10+0 records in 10+0 records out 10485760 bytes (10 MB) copied, 10.0035 s, 1.0 MB/s二: 數(shù)據(jù)管理
管理Docker 容器中數(shù)據(jù)主要有兩種方式:數(shù)據(jù)卷(Data Volumes) 和數(shù)據(jù)卷容器(DataVolumes Containers)。
2.1 數(shù)據(jù)卷
數(shù)據(jù)卷是一個供容器使用的特殊目錄,位于容器中,可將書主機(jī)的目錄掛載到數(shù)據(jù)卷上,對數(shù)據(jù)卷的修改操作立即可見 ,并且更新數(shù)據(jù)不會影響鏡像,從而實現(xiàn)數(shù)據(jù)在宿主機(jī)與容器之間的遷移。數(shù)據(jù)卷使用類似于Linux下對目錄進(jìn)行的mount操作。
#當(dāng)前宿主機(jī)沒有/var/www目錄 [root@host103 ~]# ls /var/www ls: 無法訪問/var/www: 沒有那個文件或目錄#宿主機(jī)目錄/var/www 掛載到容器中/data1. #注意:宿主機(jī)本地目錄的路徑必須是使用絕對路徑。如果路徑不存在,Docker 會自動創(chuàng)建相應(yīng)的路徑. #-v 選項可以在容器內(nèi)創(chuàng)建數(shù)據(jù)卷 [root@host103 ~]# docker run -v /var/www:/data1 --name web1 -it centos:7 bash #docker 容器里有了 data1目錄 [root@d66def26bb22 /]# ls anaconda-post.log data1 etc lib media opt root sbin sys usr bin dev home lib64 mnt proc run srv tmp var [root@d66def26bb22 /]# echo "this is web 1" >> /data1/web1.txt [root@d66def26bb22 /]# exit exit#宿主機(jī)有了/var/www,并且該目錄下有了web1.txt,且有內(nèi)容 [root@host103 ~]# cat /var/www/web1.txt this is web 12.2 數(shù)據(jù)卷容器
如果需要在容器之間共享一些數(shù)據(jù),最簡單的方法就是使用數(shù)據(jù)卷容器。數(shù)據(jù)卷容器是一個普通的容器,專門提供數(shù)據(jù)卷給其他容器掛載使用
#創(chuàng)建一個容器作為數(shù)據(jù)卷容器 [root@host103 ~]# docker run --name web2 -v /data1 -v /data2 -it centos:7 bash [root@63ec486d5683 /]# ls anaconda-post.log bin data1 data2 dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@63ec486d5683 /]# echo "this is web2 data1" > /data1/abc.txt [root@63ec486d5683 /]# [root@63ec486d5683 /]# echo "this is web2 data2" > /data2/ABC.txt [root@63ec486d5683 /]# [root@63ec486d5683 /]# cat /data1/abc.txt this is web2 data1 [root@63ec486d5683 /]# cat /data2/ABC.txt this is web2 data2#使用 --volumes-from 掛載web2 容器中的數(shù)據(jù)卷到新容器 [root@host103 ~]# docker run -it --volumes-from web2 --name web3 centos:7 bash [root@bdbbd457daea /]# ls anaconda-post.log bin data1 data2 dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@bdbbd457daea /]# ls data1 data2 data1: abc.txtdata2: ABC.txt [root@bdbbd457daea /]# cat data1/abc.txt this is web2 data1 [root@bdbbd457daea /]# cat data2/ABC.txt this is web2 data2.3 容器互聯(lián)(使用centos 鏡像)
容器互聯(lián)是通過容器的名稱在容器間建立一條專門的網(wǎng)絡(luò)通信隧道。簡單點說,就是會在源容器和接收器之間建立一條隧道,接收容器可以看到源容器指定的信息
#創(chuàng)建并運行容器,取名為web4 [root@host103 ~]# docker run -itd -P --name web4 centos:7 bash bc1caf5b2f441c834e70eb3307dab72bdb597568e1af6070f2b45f1cf8d9196e [root@host103 ~]# #創(chuàng)建并運行容器,取名為web5. --link 容器名:連接的別名 [root@host103 ~]# docker run -itd --name web5 --link web4:WEB4 centos:7 bash 107f5c583c1db9c456c47952081a084efd66de762a905f471776dd65949bb892 [root@host103 ~]# #進(jìn)入容器,ping 容器名,ping 別名,都可以ping通 [root@host103 ~]# docker exec -it web5 bash [root@107f5c583c1d /]# ping web4 [root@107f5c583c1d /]# ping WEB4總結(jié)
以上是生活随笔為你收集整理的docker 的资源控制和数据管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: docker的容器管理和网络模式
- 下一篇: docker的镜像创建与Dockefil