推廣網(wǎng)站的方法有杭州優(yōu)化公司哪家好
目錄
六、創(chuàng)建私有倉庫
七、Docker資源限制
7.1、CPU使用率
7.2、CPU共享比例
7.3、CPU周期限制
7.4、CPU核心限制
7.5、CPU 配額控制參數(shù)的混合案例
7.6、內(nèi)存限制
7.7、Block IO 的限制
7.8、限制bps 和iops
8、Docker數(shù)據(jù)持久化
8.1、數(shù)據(jù)持久化介紹
8.2、Volume的基本使用
8.2.1、管理卷
8.2.2、創(chuàng)建使用指定卷的容器
8.2.3、清理卷
8.3、Bind Mounts的基本使用
8.3.1 使用卷創(chuàng)建一個容器
8.3.2、驗(yàn)證綁定
8.3.3、清理
8.4、數(shù)據(jù)卷容器
8.4.1、數(shù)據(jù)卷容器概述
8.4.2、創(chuàng)建數(shù)據(jù)卷容器
六、創(chuàng)建私有倉庫
倉庫(Repository)是集中存放鏡像的地方。
倉庫注冊服務(wù)器才是存放倉庫具體的服務(wù)器(Registry),每個服務(wù)器上都可以放置多個倉庫,而每個倉庫下可以放置多個鏡像,每個鏡像上可以運(yùn)行多個容器,每個容器上可以跑一個應(yīng)用或應(yīng)用組。
倉庫自身可以分為:公共倉庫和私有倉庫
安裝docker后,可以通過官方提供的registry鏡像部署一套本地的私有倉庫環(huán)境
[root@localhost ~]# mkdir -p /opt/data/registry[root@localhost ~]# docker run -d --restart=always -p 5000:5000 -v /opt/data/registry:/tmp/registry registryUnable to find image 'registry:latest' locally
latest: Pulling from library/registry
79e9f2f55bf5: Pull complete
0d96da54f60b: Pull complete
5b27040df4a2: Pull complete
e2ead8259a04: Pull complete
3790aef225b9: Pull complete
Digest: sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375
Status: Downloaded newer image for registry:latest
f5b026b082957e416f2facf2fb8739ee5002d31e44ffa05c71eff08c588b0f1f[root@localhost ~]# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf5b026b08295 registry "/entrypoint.sh /etc…" 17 seconds ago Up 16 seconds 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp hardcore_cori
?準(zhǔn)備測試鏡像
[root@localhost ~]# docker run -d -p 800:80 nginx #將宿主機(jī)800端口映射給容器的業(yè)務(wù)端口Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
bc0698d60239d036f737d18370a81eb84a0a7f69ec536a8b208118aace4fa79a[root@localhost ~]# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESbc0698d60239 nginx "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 0.0.0.0:800->80/tcp, :::800->80/tcp zealous_darwin
宿主機(jī)(192.168.2.118)訪問8000端口測試:
[root@localhost ~]# docker logs bc0698d60239 #查看日志/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/08/02 03:56:56 [notice] 1#1: using the "epoll" event method
2023/08/02 03:56:56 [notice] 1#1: nginx/1.21.5
2023/08/02 03:56:56 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/08/02 03:56:56 [notice] 1#1: OS: Linux 3.10.0-1160.92.1.el7.x86_64
2023/08/02 03:56:56 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/08/02 03:56:56 [notice] 1#1: start worker processes
2023/08/02 03:56:56 [notice] 1#1: start worker process 32
2023/08/02 03:56:56 [notice] 1#1: start worker process 33
2023/08/02 03:56:56 [notice] 1#1: start worker process 34
2023/08/02 03:56:56 [notice] 1#1: start worker process 35
192.168.2.1 - - [02/Aug/2023:03:58:40 +0000] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.188" "-"
2023/08/02 03:58:40 [error] 34#34: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.2.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.2.118:800", referrer: "http://192.168.2.118:800/"
192.168.2.1 - - [02/Aug/2023:03:58:40 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.2.118:800/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.188" "-"
?將修改過的nginx鏡像做標(biāo)記封裝,準(zhǔn)備上傳到私有倉庫
[root@localhost ~]# docker tag nginx 192.168.2.118:5000/nginx-test[root@localhost ~]# docker push 192.168.2.118:5000/nginx-testUsing default tag: latest
The push refers to repository [192.168.2.118:5000/nginx-test]
Get "https://192.168.2.118:5000/v2/": http: server gave HTTP response to HTTPS client[root@localhost ~]# cat /etc/docker/daemon.json {"registry-mirrors":[ "https://nyakyfun.mirror.aliyuncs.com" ],"insecure-registries":["192.168.2.118:5000"]
}[root@localhost ~]# systemctl daemon-reload[root@localhost ~]# systemctl restart docker
將鏡像上傳到私有倉庫
[root@localhost ~]# docker push 192.168.2.118:5000/nginx-testUsing default tag: latest
The push refers to repository [192.168.2.118:5000/nginx-test]
d874fd2bc83b: Pushed
32ce5f6a5106: Pushed
f1db227348d0: Pushed
b8d6e692a25e: Pushed
e379e8aedd4d: Pushed
2edcec3590a4: Pushed
latest: digest: sha256:ee89b00528ff4f02f2405e4ee221743ebc3f8e8dd0bfd5c4c20a2fa2aaa7ede3 size: 1570
從私有倉庫中下載鏡像到本地
[root@localhost ~]# docker rmi 192.168.2.118:5000/nginx-testUntagged: 192.168.2.118:5000/nginx-test:latest
Untagged: 192.168.2.118:5000/nginx-test@sha256:ee89b00528ff4f02f2405e4ee221743ebc3f8e8dd0bfd5c4c20a2fa2aaa7ede3[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 19 months ago 141MB
registry latest b8604a3fe854 20 months ago 26.2MB[root@localhost ~]# docker pull 192.168.2.118:5000/nginx-testUsing default tag: latest
latest: Pulling from nginx-test
Digest: sha256:ee89b00528ff4f02f2405e4ee221743ebc3f8e8dd0bfd5c4c20a2fa2aaa7ede3
Status: Downloaded newer image for 192.168.2.118:5000/nginx-test:latest
192.168.2.118:5000/nginx-test:latest[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 19 months ago 141MB
192.168.2.118:5000/nginx-test latest 605c77e624dd 19 months ago 141MB
registry latest b8604a3fe854 20 months ago 26.2MB
?
七、Docker資源限制
????????Docker容器技術(shù)底層是通過Cgroup(Control?Group?控制組)實(shí)現(xiàn)容器對物理資源使用的限制,限制的資源包括CPU、內(nèi)存、磁盤三個方面。基本覆蓋了常見的資源配額和使用量控制。
????????Cgroup?是Linux 內(nèi)核提供的一種可以限制、記錄、隔離進(jìn)程組所使用的物理資源的機(jī)制,被LXC及Docker等很多項(xiàng)目用于實(shí)現(xiàn)進(jìn)程的資源控制。
????????Cgroup 是提供將進(jìn)程進(jìn)行分組化管理的功能和接口的基礎(chǔ)結(jié)構(gòu),Docker中I/O 或內(nèi)存的分配控制等具體的資源管理功能都是通過Cgroup功能來實(shí)現(xiàn)的。這些具體的資源管理功能稱為Cgroup子系統(tǒng),以下是對幾大子系統(tǒng)的介紹。
- blkio:限制塊設(shè)備的輸入輸出控制。如:磁盤、光盤、USB等。
- cpu:限制CPU資源的訪問
- cpuacct:產(chǎn)生Cgroup 任務(wù)的CPU資源報告。
- cpuset:限制分配單獨(dú)的cpu 和內(nèi)存資源。
- devices:允許或拒絕對設(shè)備的訪問。
- freezer:暫停和恢復(fù)Cgroup 任務(wù)。
- memory:設(shè)置每個Cgroup 的內(nèi)存限制以及產(chǎn)生內(nèi)存資源報告。
- net_cls:用于標(biāo)記每個網(wǎng)絡(luò)包。
- ns:命名空間子系統(tǒng)。
- perf_event:增加了對每group 的監(jiān)測跟蹤的能力,可以監(jiān)測屬于某個特定的group 的所有線程以及運(yùn)行在特定CPU 上的線程。
?centos-7-x86_64.tar提取鏈接:https://pan.baidu.com/s/155YCVkJr6rXKf7VP-ZXFwQ?pwd=e12x?
提取碼:e12x
使用下面的Dockerfile 來創(chuàng)建一個基于CentOS的stress工具鏡像。
[root@localhost ~]# cat centos-7-x86_64.tar.gz | docker import - centos:7sha256:bd0569f5d7da160cecb1437baf48b28d31a303777785053f4189ff1af97e7342[root@localhost ~]# mkdir stress[root@localhost ~]# cd stress[root@localhost stress]# vim DockerfileFROM centos:7
MAINTAINER crushlinux "crushlinux@163.com"
RUN yum -y install wget
RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN yum -y install stress[root@localhost stress]# docker build -t centos:stress .[+] Building 48.6s (8/8) FINISHED docker:default=> [internal] load build definition from Dockerfile 0.0s=> => transferring dockerfile: 288B 0.0s=> [internal] load .dockerignore 0.0s=> => transferring context: 2B 0.0s=> [internal] load metadata for docker.io/library/centos:7 0.0s=> [1/4] FROM docker.io/library/centos:7 0.0s=> [2/4] RUN yum -y install wget 31.9s=> [3/4] RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 1.8s=> [4/4] RUN yum -y install stress 14.1s => exporting to image 0.7s => => exporting layers 0.6s => => writing image sha256:4907ba4a7a63ba68cab027bcc5191665bad0898ad7229435b528e364e154d399 0.0s => => naming to docker.io/library/centos:stress
7.1、CPU使用率
????????在CentOS7中可以通過修改對應(yīng)的Cgroup配置文件cpu.cfs_quota_us的值來實(shí)現(xiàn),直接執(zhí)行echo命令將設(shè)定值導(dǎo)入到文件中就會立即生效。
例如將容器ddb8e55d1c09的CPU使用設(shè)置為20000,設(shè)置CPU的使用率限定為20%。
[root@localhost ~]# docker run -itd centos:stress /bin/bashddb8e55d1c094fe9734994ffe3a61fafebf2237eded6ebffa507f61d32921010[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESddb8e55d1c09 centos:stress "/bin/bash" 9 seconds ago Up 8 seconds dazzling_euclid[root@localhost ~]# echo "20000" > /sys/fs/cgroup/cpu,cpuacct/docker/ddb8e55d1c094fe9734994ffe3a61fafebf2237eded6ebffa507f61d32921010/cpu.cfs_quota_us
7.2、CPU共享比例
????????當(dāng)多個容器任務(wù)運(yùn)行時,很難計(jì)算CPU的使用率,為了使容器合理使用CPU資源,可以通過--cpu-shares選項(xiàng)設(shè)置容器按比例共享CPU資源,這種方式還可以實(shí)現(xiàn)CPU使用率的動態(tài)調(diào)整。
????????命令中的--cpu-shares 選項(xiàng)值不能保證可以獲得1 個vcpu 或者多少GHz 的CPU 資源,僅僅只是一個彈性的加權(quán)值。
????????默認(rèn)情況下,每個docker容器的cpu份額都是1024。單獨(dú)一個容器的份額是沒有意義的。只有在同時運(yùn)行多個容器時,容器的CPU加權(quán)的效果才能體現(xiàn)出來。例如,兩個容器A、B的CPU份額分別為1000和500,在CPU進(jìn)行時間片分配的時候,容器A比容器B多一倍的機(jī)會獲得CPU的時間片。但分配的結(jié)果取決于當(dāng)時主機(jī)和其他容器的運(yùn)行狀態(tài),實(shí)際上也無法保證容器A一定能獲得CPU時間片。比如容器A的進(jìn)程一直是空閑的,那么容器B 是可以獲取比容器A更多的CPU時間片的。極端情況下,比如說主機(jī)上只運(yùn)行了一個容器,即使它的CPU份額只有50,它也可以獨(dú)占整個主機(jī)的CPU資源。
????????Cgroups 只在容器分配的資源緊缺時,也就是說在需要對容器使用的資源進(jìn)行限制時才會生效。因此無法單純根據(jù)某個容器的CPU份額來確定有多少CPU 資源分配給它,資源分配結(jié)果取決于同時運(yùn)行的其他容器的CPU分配和容器中進(jìn)程運(yùn)行情況。
????????換句話說,可以通過cpu shares可以設(shè)置容器使用CPU的優(yōu)先級,比如啟動了兩個容器及運(yùn)行查看CPU使用百分比。
[root@localhost ~]# docker run -tid --name cpu512 --cpu-shares 512 centos:stress stress -c 10b2d3e64065f09a683e352722d187c097e2b4bf161a4cc08444a304700a101e23[root@localhost ~]# docker run -tid --name cpu1024 --cpu-shares 1024 centos:stress stress -c 105c59dc0f47d3f6bde8003d8b6f684e6fc8268ef792d282e1c335abbbcc3edfa8[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES5c59dc0f47d3 centos:stress "stress -c 10" 10 seconds ago Up 8 seconds
cpu1024b2d3e64065f0 centos:stress "stress -c 10" About a minute ago Up About a minute cpu512ddb8e55d1c09 centos:stress "/bin/bash" 53 minutes ago Up 53 minutes dazzling_euclidbc0698d60239 nginx "/docker-entrypoint.…" 4 hours ago Exited (0) 2 hours ago zealous_darwinf5b026b08295 registry "/entrypoint.sh /etc…" 4 hours ago Up About an hour 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp hardcore_cori[root@localhost ~]# docker exec -it b2d3e64065f0 /bin/bash[root@b2d3e64065f0 /]# toptop - 03:31:32 up 5:48, 0 users, load average: 19.89, 12.06, 5.18
Tasks: 14 total, 11 running, 3 sleeping, 0 stopped, 0 zombie
%Cpu(s): 99.9 us, 0.1 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3861080 total, 1333692 free, 350952 used, 2176436 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 3107816 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 9 root 20 0 7260 96 0 R 14.3 0.0 1:00.20 stress 11 root 20 0 7260 96 0 R 14.3 0.0 0:59.15 stress 12 root 20 0 7260 96 0 R 14.3 0.0 1:00.03 stress 15 root 20 0 7260 96 0 R 14.3 0.0 1:01.25 stress 13 root 20 0 7260 96 0 R 14.0 0.0 0:59.02 stress 16 root 20 0 7260 96 0 R 14.0 0.0 1:01.54 stress 8 root 20 0 7260 96 0 R 12.7 0.0 0:59.61 stress 10 root 20 0 7260 96 0 R 12.7 0.0 0:59.41 stress 7 root 20 0 7260 96 0 R 12.3 0.0 1:00.28 stress 14 root 20 0 7260 96 0 R 12.3 0.0 0:59.47 stress 1 root 20 0 7260 644 548 S 0.0 0.0 0:00.03 stress 23 root 20 0 11772 1804 1448 S 0.0 0.0 0:00.01 bash 39 root 20 0 11772 1808 1448 S 0.0 0.0 0:00.01 bash 55 root 20 0 51864 1920 1408 R 0.0 0.0 0:00.00 top
[root@localhost ~]# docker exec -it 5c59dc0f47d3 /bin/bash[root@5c59dc0f47d3 /]# toptop - 03:34:52 up 5:51, 0 users, load average: 20.41, 16.06, 8.10
Tasks: 13 total, 11 running, 2 sleeping, 0 stopped, 0 zombie
%Cpu(s):100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3861080 total, 1338616 free, 345936 used, 2176528 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 3112744 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 11 root 20 0 7260 92 0 R 30.9 0.0 1:56.49 stress 12 root 20 0 7260 92 0 R 28.2 0.0 1:55.71 stress 16 root 20 0 7260 92 0 R 28.2 0.0 1:57.39 stress 14 root 20 0 7260 92 0 R 27.6 0.0 1:53.74 stress 13 root 20 0 7260 92 0 R 25.2 0.0 1:57.68 stress 7 root 20 0 7260 92 0 R 24.9 0.0 1:55.63 stress 8 root 20 0 7260 92 0 R 24.9 0.0 1:55.41 stress 9 root 20 0 7260 92 0 R 24.9 0.0 1:54.88 stress 15 root 20 0 7260 92 0 R 24.9 0.0 1:59.09 stress 10 root 20 0 7260 92 0 R 24.6 0.0 1:56.37 stress 1 root 20 0 7260 640 548 S 0.0 0.0 0:00.01 stress 17 root 20 0 11772 1808 1448 S 0.0 0.0 0:00.04 bash 33 root 20 0 51860 1924 1408 R 0.0 0.0 0:00.00 top
????????開啟了10 個stress 進(jìn)程,目的是充分讓系統(tǒng)資源變得緊張。只有這樣競爭資源,設(shè)定的資源比例才可以顯現(xiàn)出來。如果只運(yùn)行一個進(jìn)行,會自動分配到空閑的CPU,這樣比例就無法看出來。由于案例的環(huán)境不一樣,可能導(dǎo)致上面兩張圖中占用CPU 百分比會不同,但是從cpu share 來看兩個容器總比例一定會是1:2。
7.3、CPU周期限制
docker 提供了--cpu-period、--cpu-quota 兩個參數(shù)控制容器可以分配到的CPU 時鐘周期。
- --cpu-period 是用來指定容器對CPU的使用要在多長時間內(nèi)做一次重新分配。
- --cpu-quota 是用來指定在這個周期內(nèi),最多可以有多少時間用來跑這個容器。與--cpu-shares 不同的是:這種配置是指定一個絕對值,而且沒有彈性在里面,容器對CPU 資源的使用絕對不會超過配置的值。
????????cpu-period 和cpu-quota 的單位為微秒(μs)。cpu-period 的最小值為1000 微秒,
最大值為1 秒(10^6 μs),默認(rèn)值為0.1 秒(100000 μs)。cpu-quota 的值默認(rèn)為-1,
表示不做控制。cpu-period、cpu-quota 這兩個參數(shù)一般聯(lián)合使用。
????????舉個例子,如果容器進(jìn)程需要每1 秒使用單個CPU 的0.2 秒時間,可以將cpu-period 設(shè)置為1000000(即1 秒),cpu-quota 設(shè)置為200000(0.2 秒)。當(dāng)然,在多核情況下,如果允許容器進(jìn)程需要完全占用兩個CPU,則可以將cpu-period 設(shè)置為100000(即0.1 秒),cpu-quota 設(shè)置為200000(0.2 秒)。
[root@localhost ~]# docker run -it --cpu-period 10000 --cpu-quota 20000 centos:stress /bin/bash[root@aa6136f8cb7b /]# cat /sys/fs/cgroup/cpu/cpu.cfs_period_us 10000[root@aa6136f8cb7b /]# cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us 20000
7.4、CPU核心限制
????????多核CPU的服務(wù)器Docker還可以控制容器運(yùn)行限定使用哪些CPU內(nèi)核,可以使用--cpuset-cpus選項(xiàng)來使某些程序獨(dú)享CPU核心,以便提高其處理速度,對應(yīng)的Cgroup文件為/sys/fs/cgroup/cpuset/docker/容器ID號/cpuset.cpus。選項(xiàng)后直接跟參數(shù)0、1、2……表示第1個內(nèi)核,第2個內(nèi)核,第3個內(nèi)核,與/proc/cpuinfo中的標(biāo)號相同。
如果服務(wù)器有16個核心,那么CPU編號為0~15,使新建容器綁定第1~4的核心使用:
[root@localhost ~]# docker run -itd --cpuset-cpus 0,1,2,3 centos:stress /bin/bash
7f26046560e1f1e1d91dfe3d16bded32d37ffcaef964725a68eaf16b99722fa7或[root@localhost ~]# docker run -itd --cpuset-cpus 0-3 centos:stress /bin/bash
1b69dabb453be291dcd87dbeca4f15f6761c0868feae0d53ea80097fbbd210b6
那么該容器內(nèi)的進(jìn)程只會在0,1,2,3的CPU上運(yùn)行。
通過下面指令也可以看到容器中進(jìn)程與CPU 內(nèi)核的綁定關(guān)系,可以認(rèn)為達(dá)到了綁定CPU 內(nèi)核的目的。
[root@localhost ~]# docker exec 1b69dabb453b taskset -c -p 1
pid 1's current affinity list: 0-3 #容器內(nèi)部第一個進(jìn)程編號一般為1
盡量使用綁定內(nèi)核的方式分配CPU資源給容器進(jìn)程使用,然后在配合--cpu-shares選項(xiàng)動態(tài)調(diào)整CPU使用資源的比例。
7.5、CPU 配額控制參數(shù)的混合案例
????????通過--cpuset-cpus 指定容器A 使用CPU 內(nèi)核0,容器B 只是用CPU 內(nèi)核1。在主機(jī)上只有這兩個容器使用對應(yīng)CPU 內(nèi)核的情況,它們各自占用全部的內(nèi)核資源,--cpu-shares 沒有明顯效果。
????????--cpuset-cpus、--cpuset-mems 參數(shù)只在多核、多內(nèi)存節(jié)點(diǎn)上的服務(wù)器上有效,并且必須與實(shí)際的物理配置匹配,否則也無法達(dá)到資源控制的目的。
????????在系統(tǒng)具有多個CPU 內(nèi)核的情況下,需要通過cpuset-cpus 為容器CPU 內(nèi)核才能比較方便地進(jìn)行測試。
用下列命令創(chuàng)建測試用的容器:
[root@localhost ~]# docker run -itd --name cpu3 --cpuset-cpus 3 --cpu-shares 512 centos:stress stress -c 1004cb93f1d76e4b30e001ad7b2afc51dfcedef818970edcf0763aba56a45cea2[root@localhost ~]# docker exec -it 004cb93f1d76 /bin/bash[root@004cb93f1d76 /]# toptop - 03:57:24 up 6:14, 0 users, load average: 5.43, 15.35, 15.68
Tasks: 4 total, 2 running, 2 sleeping, 0 stopped, 0 zombie
%Cpu(s): 8.0 us, 0.1 sy, 0.0 ni, 91.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3861080 total, 1396020 free, 291912 used, 2173148 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 3170028 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 7 root 20 0 7260 96 0 R 100.0 0.0 1:18.70 stress 1 root 20 0 7260 436 352 S 0.0 0.0 0:00.01 stress 8 root 20 0 11772 1800 1444 S 0.0 0.0 0:00.01 bash 24 root 20 0 51880 1872 1388 R 0.0 0.0 0:00.00 top
[root@localhost ~]# docker run -itd --name cpu4 --cpuset-cpus 3 --cpu-shares 1024 centos:stress stress -c 18998d47911ade2227fe71fce69a125be34a5714868cb7d4fbf3fe40a9fae4164[root@localhost ~]# docker exec -it 8998d47911ad /bin/bash[root@8998d47911ad /]# toptop - 04:00:08 up 6:16, 0 users, load average: 2.03, 9.50, 13.38
Tasks: 4 total, 2 running, 2 sleeping, 0 stopped, 0 zombie
%Cpu(s): 24.8 us, 0.0 sy, 0.0 ni, 74.7 id, 0.0 wa, 0.0 hi, 0.5 si, 0.0 st
KiB Mem : 3861080 total, 1383908 free, 302924 used, 2174248 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 3158056 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 7 root 20 0 7260 92 0 R 66.4 0.0 0:48.96 stress 1 root 20 0 7260 432 352 S 0.0 0.0 0:00.01 stress 8 root 20 0 11772 1804 1448 S 0.0 0.0 0:00.01 bash 24 root 20 0 51852 1912 1408 R 0.0 0.0 0:00.00 top
????????上面的centos:stress 鏡像安裝了stress 工具,用來測試CPU 和內(nèi)存的負(fù)載。通過在兩個容器上分別執(zhí)行stress -c 1 命令,將會給系統(tǒng)一個隨機(jī)負(fù)載,產(chǎn)生1 個進(jìn)程。這個進(jìn)程都反復(fù)不停的計(jì)算由rand() 產(chǎn)生隨機(jī)數(shù)的平方根,直到資源耗盡。
????????觀察到宿主機(jī)上的CPU 試用率,第三個內(nèi)核的使用率接近100%,并且一批進(jìn)程的CPU 使用率明顯存在2:1 的使用比例的對比。
7.6、內(nèi)存限制
與操作系統(tǒng)類似,容器可使用的內(nèi)存包括兩部分:物理內(nèi)存和swap。Docker 通過下面兩組參數(shù)來控制容器內(nèi)存的使用量。
- -m 或--memory:設(shè)置內(nèi)存的使用限額,例如100M, 1024M。
- --memory-swap:設(shè)置內(nèi)存swap 的使用限額。
當(dāng)執(zhí)行如下命令:
其含義是允許該容器最多使用200M 的內(nèi)存和300M 的swap。
[root@localhost ~]# docker run -it -m 200M --memory-swap=300M progrium/stress --vm 1 --vm-bytes 280Mstress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [7] forked
stress: dbug: [7] allocating 293601280 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...
stress: dbug: [7] freed 293601280 bytes
stress: dbug: [7] allocating 293601280 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...
stress: dbug: [7] freed 293601280 bytes
- --vm 1:啟動1 個內(nèi)存工作線程。
- --vm-bytes 280M:每個線程分配280M 內(nèi)存。
默認(rèn)情況下,容器可以使用主機(jī)上的所有空閑內(nèi)存。與CPU 的cgroups 配置類似,docker 會自動為容器在目錄/sys/fs/cgroup/memory/docker/<容器的完整長ID>中創(chuàng)建相應(yīng)cgroup 配置文件。
因?yàn)?80M 在可分配的范圍(300M)內(nèi),所以工作線程能夠正常工作,其過程是:
- 分配280M 內(nèi)存。
- 釋放280M 內(nèi)存。
- 再分配280M 內(nèi)存。
- 再釋放280M 內(nèi)存。
- 一直循環(huán)......
如果讓工作線程分配的內(nèi)存超過300M,分配的內(nèi)存超過限額,stress 線程報錯,容器退出。
[root@localhost ~]# docker run -it -m 200M --memory-swap=300M progrium/stress --vm 1 --vm-bytes 380Mstress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [7] forked
stress: dbug: [7] allocating 398458880 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...
stress: FAIL: [1] (416) <-- worker 7 got signal 9
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (422) kill error: No such process
stress: FAIL: [1] (452) failed run completed in 0s
7.7、Block IO 的限制
默認(rèn)情況下,所有容器能平等地讀寫磁盤,可以通過設(shè)置--blkio-weight 參數(shù)來改變?nèi)萜鱞lock IO 的優(yōu)先級。
--blkio-weight 與--cpu-shares 類似,設(shè)置的是相對權(quán)重值,默認(rèn)為500。在下面的例子中,容器A 讀寫磁盤的帶寬是容器B 的兩倍。
[root@localhost ~]# docker run -it --name container_A --blkio-weight 600 centos:stress /bin/bash[root@9a1ade37e4a2 /]# cat /sys/fs/cgroup/blkio/blkio.weight600[root@9a1ade37e4a2 /]# cat /sys/fs/cgroup/blkio/blkio.weight[root@localhost ~]# [root@localhost ~]# docker run -it --name container_B --blkio-weight 300 centos:stress /bin/bash[root@82d39cad4b92 /]# cat /sys/fs/cgroup/blkio/blkio.weight300
7.8、限制bps 和iops
如果在一臺服務(wù)器上進(jìn)行容器的混合部署,那么會存在同時幾個程序?qū)懘疟P數(shù)據(jù)的情況,這時可以通過--device-write-iops選項(xiàng)來限制每秒寫io次數(shù)來限制制定設(shè)備的寫速度。相應(yīng)的還有--device-read-iops選項(xiàng)可以限制讀取IO的速度,但是這種方法只能限制設(shè)備,而不能限制分區(qū),相應(yīng)的Cgroup寫配置文件為/sys/fs/cgroup/blkio/容器ID/?blkio.throttle.write_iops_device。
- bps 是byte per second,每秒讀寫的數(shù)據(jù)量。
- iops 是io per second,每秒IO 的次數(shù)。
可通過以下參數(shù)控制容器的bps 和iops:
- --device-read-bps,限制讀某個設(shè)備的bps
- --device-write-bps,限制寫某個設(shè)備的bps
- --device-read-iops,限制讀某個設(shè)備的iops
- --device-write-iops,限制寫某個設(shè)備的iops
下面的示例是限制容器寫/dev/sda 的速率為5 MB/s。
[root@localhost ~]# docker run -it --device-write-bps /dev/sda:5MB centos:stress /bin/bash[root@f4b37691fa4d /]# dd if=/dev/zero of=test bs=1M count=100 oflag=direct100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 20.006 s, 5.2 MB/s
????????通過dd 測試在容器中寫磁盤的速度。因?yàn)槿萜鞯奈募到y(tǒng)是在host /dev/sda 上的,在容器中寫文件相當(dāng)于對宿主機(jī)/dev/sda 進(jìn)行寫操作。另外,oflag=direct 指定用direct
IO 方式寫文件,這樣--device-write-bps 才能生效。
結(jié)果表明,bps 5.2 MB/s 在限速5MB/s 左右。作為對比測試,如果不限速,結(jié)果如下:
[root@localhost ~]# docker run -it centos:stress /bin/bash[root@d324fcfe3cbb /]# dd if=/dev/zero of=test bs=1M count=100100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.0527029 s, 2.0 GB/s
8、Docker數(shù)據(jù)持久化
8.1、數(shù)據(jù)持久化介紹
在Docker中若要想實(shí)現(xiàn)容器數(shù)據(jù)的持久化(所謂的數(shù)據(jù)持久化即數(shù)據(jù)不隨著Container的結(jié)束而銷毀),需要將數(shù)據(jù)從宿主機(jī)掛載到容器中。目前Docker提供了三種不同的方式將數(shù)據(jù)從宿主機(jī)掛載到容器中。
(1)Volumes:Docker會管理宿主機(jī)文件系統(tǒng)的一部分資源,默認(rèn)位于 /var/lib/docker/volumes 目錄中;(最常用的方式)
[root@localhost ~]# docker run -it -v /opt/ centos /bin/bashUnable to find image 'centos:latest' locally
latest: Pulling from library/centos
a1d0c7532777: Already exists
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest[root@630ab274dcd8 /]# touch /opt/test.txt[root@630ab274dcd8 /]# ls /opt/ test.txt[root@localhost ~]# ls /var/lib/docker/volumes/3e75601aeaa96d13807f38d22fd13666db6ead38af2ce543a249e5515d497bca/_data/test.txt
目前所有Container的數(shù)據(jù)都保存在/var/lib/docker/volumes/目錄下邊,由于沒有在創(chuàng)建時指定卷,所以Docker幫我們默認(rèn)創(chuàng)建許多匿名(就上面這一堆很長ID的名字)卷。
(2)bind mounts:意為著可以指定存儲在宿主機(jī)系統(tǒng)的任意位置;(比較常用的方式)
但是bind mounts在不同的宿主機(jī)系統(tǒng)之間是不可移植的,比如Windows和Linux的存儲結(jié)構(gòu)是不一樣的,bind mount所指向的host目錄也不能一樣。這也是為什么bind mount不能出現(xiàn)在Dockerfile中的原因,因?yàn)闀?dǎo)致Dockerfile無法移植。
(3)tmpfs:掛載存儲在宿主機(jī)系統(tǒng)的內(nèi)存中,而不會寫入宿主機(jī)的文件系統(tǒng);(一般都不會用的方式)
8.2、Volume的基本使用
8.2.1、管理卷
創(chuàng)建一個自定義容器卷
[root@localhost ~]# docker volume create nginx-data
nginx-data
?查看所有容器卷
[root@localhost ~]# docker volume lsDRIVER VOLUME NAME
local nginx-data
查看指定容器卷詳情信息
[root@localhost ~]# docker volume inspect nginx-data
[{"CreatedAt": "0001-01-01T00:00:00Z","Driver": "local","Labels": null,"Mountpoint": "/var/lib/docker/volumes/nginx-data/_data","Name": "nginx-data","Options": null,"Scope": "local"}
]
8.2.2、創(chuàng)建使用指定卷的容器
有了自定義容器卷,我們可以創(chuàng)建一個使用這個數(shù)據(jù)卷的容器
[root@localhost ~]# docker run -d -it --name=nginx -p 800:80 -v nginx-data:/usr/share/nginx/html nginx67a5acf397730011f702127cd7d5001f3c87d12060ed2b0e7bf2015c0d8ba94e[root@localhost ~]# docker exec -it nginx /bin/bashroot@67a5acf39773:/# ls /usr/share/nginx/html/50x.html index.html
????????選項(xiàng)-v代表掛載數(shù)據(jù)卷,這里使用自定數(shù)據(jù)卷nginx-data,并且將數(shù)據(jù)卷掛載到 /usr/share/nginx/html (這個目錄是yum安裝nginx的默認(rèn)網(wǎng)頁目錄)。如果沒有通過-v指定,那么Docker會默認(rèn)幫我們創(chuàng)建匿名數(shù)據(jù)卷進(jìn)行映射和掛載。
注意:
- 數(shù)據(jù)卷下無文件,顯示容器對應(yīng)目錄下的文件
- 數(shù)據(jù)卷下有文件,顯示數(shù)據(jù)卷原有文件,并將容器對應(yīng)目錄的文件隱藏,顯示數(shù)據(jù)卷文件
可以看到網(wǎng)頁目錄下有兩個默認(rèn)頁面,這時我們可以查看宿主機(jī)文件系統(tǒng)的數(shù)據(jù)
[root@localhost ~]# ls /var/lib/docker/volumes/nginx-data/_data/50x.html index.html
? 可以看到容器里面的兩個默認(rèn)頁面,由此可知Volume幫我們做了類似于一個軟鏈接的功能。在容器里邊的改動,我們可以在宿主機(jī)里感知,而在宿主機(jī)里面的改動,在容器里邊可以感知到。
如果我們手動stop并且remove當(dāng)前nginx容器,我們會發(fā)現(xiàn)容器卷里面的文件還在,并沒有隨著容器被刪除掉。
[root@localhost ~]# docker stop nginxnginx[root@localhost ~]# docker rm nginxnginx[root@localhost ~]# ls /var/lib/docker/volumes/nginx-data/_data/50x.html index.html
? 所以在數(shù)據(jù)卷里邊的東西是可以持久化的。如果下次還需要創(chuàng)建一個nginx容器,那么時候復(fù)用當(dāng)前數(shù)據(jù)卷里面文件的。
[root@localhost ~]# docker run -d -it --name=nginx2 -p 801:80 -v nginx-data:/usr/share/nginx/html nginx6741a1c3ea19a45333e0330191c023bc3e04bfad85498edd4c160e0871996ea7[root@localhost ~]# docker exec -it nginx2 /bin/bashroot@6741a1c3ea19:/# ls /usr/share/nginx/html/50x.html index.html
此外,我們還可以啟動多個nginx容器實(shí)例,共享同一個數(shù)據(jù)卷。數(shù)據(jù)卷的復(fù)用性和擴(kuò)展性較強(qiáng)的。
8.2.3、清理卷
如果不再使用自定義數(shù)據(jù)卷了,那么可以手動清理掉:
[root@localhost ~]# docker volume rm -f nginx-datanginx-data[root@localhost ~]# docker volume lsDRIVER VOLUME NAME
8.3、Bind Mounts的基本使用
8.3.1 使用卷創(chuàng)建一個容器
[root@localhost ~]# docker run -d -it --name=nginx -p 800:80 -v /wwwroot:/usr/share/nginx/html nginx0bc4e5c1975bb7fff3d8e5886e78c6711de7204ba7a09cddb24c410421c53f23
????????這里指定了將宿主機(jī)上的 /wwwroot 目錄(如果沒有會自動創(chuàng)建)掛載到 /usr/share/nginx/html (這個目錄是yum安裝nginx的默認(rèn)網(wǎng)頁目錄)。
docker掛載的默認(rèn)權(quán)限是讀寫(rw),用戶也可以通過ro指定為只讀
[root@localhost ~]# docker run -d -it --name=nginx2 -p 801:80 -v /wwwroot:/usr/share/nginx/html:ro nginx0aba96663db35032504213f6aab44424e88d2e0a2e39592e3f1c94ae23e35269
[root@localhost ~]# docker exec -it nginx /bin/bashroot@0bc4e5c1975b:/# ls /usr/share/nginx/html/
? 可以看到,與volumes不同,bind mounts的方式會隱藏掉被掛載目錄里面的內(nèi)容(如果非空的話),這里是/usr/share/nginx/html 目錄下的內(nèi)容被隱藏掉了,因此我們看不到。
但是,我們可以將宿主機(jī)上的文件隨時掛載到容器中:
新建一個index.html,并在容器中查看
[root@localhost ~]# echo "test html" > /wwwroot/index.html[root@localhost ~]# docker exec -it nginx /bin/bashroot@0bc4e5c1975b:/# ls /usr/share/nginx/html/index.html
8.3.2、驗(yàn)證綁定
[root@localhost ~]# docker inspect nginx"HostConfig": {"Binds": ["/wwwroot:/usr/share/nginx/html"],
8.3.3、清理
[root@localhost ~]# docker stop nginxnginx[root@localhost ~]# docker rm nginxnginx[root@localhost ~]# ls /wwwroot/index.html
????????同volumes一樣,當(dāng)我們清理掉容器之后,掛載目錄里面的文件仍然還在,不會隨著容器的結(jié)束而消失,從而實(shí)現(xiàn)數(shù)據(jù)持久化。
8.4、數(shù)據(jù)卷容器
8.4.1、數(shù)據(jù)卷容器概述
用戶需要在容器之間共享一些持續(xù)性更新的數(shù)據(jù)時,可以使用數(shù)據(jù)卷容器。數(shù)據(jù)容器也是一個普通的容器。里邊帶有設(shè)置好的數(shù)據(jù)卷,專門提供給其他容器掛載使用。 通過--volumes-from 數(shù)據(jù)卷容器名來實(shí)現(xiàn)。
8.4.2、創(chuàng)建數(shù)據(jù)卷容器
[root@localhost ~]# docker run -it -v /dbdata:/dbdata --name=dbdata centos /bin/bashUnable to find image 'centos:latest' locally
latest: Pulling from library/centos
a1d0c7532777: Already exists
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest[root@36cd7b069c08 /]# exitexit
//創(chuàng)建一個數(shù)據(jù)卷容器,并在其中創(chuàng)建一個數(shù)據(jù)卷掛載到/dbdata
進(jìn)入test1容器創(chuàng)建文件測試
[root@localhost ~]# docker run -it --volumes-from dbdata --name test1 centos /bin/bash[root@e06b68cbae34 /]# lsbin dev home lib64 media opt root sbin sys usr
dbdata etc lib lost+found mnt proc run srv tmp var[root@e06b68cbae34 /]# touch dbdata/crushlinux[root@e06b68cbae34 /]# lsbin dev home lib64 media opt root sbin sys usr
dbdata etc lib lost+found mnt proc run srv tmp var[root@e06b68cbae34 /]# ls dbdata/crushlinux[root@e06b68cbae34 /]# exitexit
//在test1容器的/dbdata目錄創(chuàng)建測試文件
進(jìn)入test2容器驗(yàn)證結(jié)果
[root@localhost ~]# docker run -it --volumes-from dbdata --name test2 centos /bin/bash[root@bbaf174a1aa9 /]# lsbin dev home lib64 media opt root sbin sys usr
dbdata etc lib lost+found mnt proc run srv tmp var[root@bbaf174a1aa9 /]# ls dbdata/crushlinux
說明:
- 可以多次使用--volume-from參數(shù)從多個容器掛載多個目錄。也可以從其他已經(jīng)掛載了數(shù)據(jù)卷的容器來掛載數(shù)據(jù)卷(類似傳遞)。
- 再次強(qiáng)調(diào):如果刪除了掛載的容器,數(shù)據(jù)卷不會被自動刪除。如果要刪除容器的時候同時刪除數(shù)據(jù)卷,需加上-v參數(shù)。