1 構(gòu)建實(shí)驗(yàn)環(huán)境
1.1 實(shí)驗(yàn)機(jī)的環(huán)境準(zhǔn)備
1.1.1 實(shí)驗(yàn)機(jī)系統(tǒng)規(guī)劃:
- 本次實(shí)驗(yàn)使用的系統(tǒng)版本為:
CentOS Linux release 7.6.1810 (Core)
- 實(shí)驗(yàn)用虛擬機(jī)的安裝和初始化請(qǐng)參見(jiàn):《Linux---CentOS7.x-操作系統(tǒng)初始化的簡(jiǎn)單介紹》
1.1.2 實(shí)驗(yàn)網(wǎng)絡(luò)規(guī)劃:
IP 地址規(guī)劃
主機(jī)名 | IP | 掩碼 | 網(wǎng)關(guān) | 用途 |
---|---|---|---|---|
端口規(guī)劃
端口 | 協(xié)議 | 用途 |
---|---|---|
1.1.3 安裝路徑規(guī)劃
因使用 yum 安裝裳瘪,保持軟件默認(rèn)設(shè)置不變
1.1.4 用戶規(guī)劃
用戶名 | 密碼 | 用途 |
---|---|---|
1.2 軟件環(huán)境的準(zhǔn)備
1.2.1 刪除沖突軟件
無(wú)
1.2.2 安裝依賴(lài)服務(wù)
無(wú)
1.2.3 安裝依賴(lài)軟件
]# yum -y install yum-util device-mapper-persistent-data lvm2
1.2.4 系統(tǒng)環(huán)境初始化
為 /var/lib/docker
創(chuàng)建單獨(dú)的分區(qū)
docker 安裝后,會(huì)在該分區(qū)內(nèi)存放 docker 使用的鏡像罪针,容器彭羹,網(wǎng)絡(luò)等相關(guān)組件。因此為隨著使用逐漸增大泪酱,為以后維護(hù)方便派殷,建議設(shè)置單獨(dú)的分區(qū)
]# mkdir /var/lib/docker
]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 57G 0 part
├─centos-root 253:0 0 45G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm
└─centos-home 253:2 0 10G 0 lvm /home
sdb 8:16 0 100G 0 disk
sr0 11:0 1 1024M 0 rom
]# pvcreate /dev/sdb
]# vgcreate vg_docker /dev/sdb
]# lvcreate -l 100%FREE -n lv_docker vg_docker
]# mkfs -t xfs /dev/mapper/vg_docker-lv_docker
]# echo "/dev/vg_docker/lv_docker /var/lib/docker xfs defaults 0 0" >> /etc/fstab
]# mount -a
]# df -h /dev/vg_docker/lv_docker
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_docker-lv_docker 100G 33M 100G 1% /var/lib/docker
1.3 獲取 docker 的安裝包
使用 yum 方式進(jìn)行安裝,新增相應(yīng)的 yum 源
]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
注:
為了獲得更好的下載速率墓阀,建議此處更換為國(guó)內(nèi)的鏡像站毡惜,如:
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2 安裝 docker
]# yum -y install docker-ce-18.09.7 docker-ce-cli-18.09.7 containerd.io
3 基礎(chǔ)配置
加速鏡像下載
因?yàn)榫W(wǎng)絡(luò)原因,從官網(wǎng)拉取鏡像較慢斯撮,可以通過(guò)以下方式虱黄,來(lái)加速?gòu)?Docker Hub 中拉取鏡像
為了永久性保留更改,您可以修改 /etc/docker/daemon.json
文件并添加上 registry-mirrors
鍵值吮成。
]# mkdir -p /etc/docker
]# cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://registry.docker-cn.com", "https://dockerhub.azk8s.cn", "https://reg-mirror.qiniu.com"]
}
EOF
注:
為保證加速服務(wù)高可用,可以多配置幾個(gè)鏡像站辜梳,如下:
驗(yàn)證生效(需要先啟動(dòng) docker 進(jìn)程):
]# docker info
... 省略 ...
Registry Mirrors:
https://registry.docker-cn.com/
... 省略 ...
4 啟動(dòng)與驗(yàn)證
4.1 啟動(dòng)
]# systemctl start docker.service
4.2 驗(yàn)證測(cè)試
]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9bb5a5d4561a: Pull complete
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
4.3 設(shè)置開(kāi)機(jī)啟動(dòng)
驗(yàn)證完成后粱甫,將 docker 設(shè)置為開(kāi)機(jī)啟動(dòng)
]# systemctl enable docker.service
附A 通過(guò) REHL 默認(rèn)源 安裝 docker
]# yum -y install docker
更改 docker 的存儲(chǔ)模式(可選項(xiàng))
docker 在基于 rhel 的分支默認(rèn)使用的 devicemapper,并且默認(rèn)配置成 loop-lvm 模式運(yùn)行作瞄。這種模式使用文件來(lái)作為虛擬池(thin pool)構(gòu)建鏡像和容器的層茶宵。但這種模式并不適用于生產(chǎn),因此要更改為 direct-lvm 模式宗挥。direct-lvm 通過(guò) dm-thin 內(nèi)核模塊乌庶,直接使用raw分區(qū)种蝶,在高負(fù)載和高密度下具有性能優(yōu)勢(shì)。
注:
該部分內(nèi)容瞒大,可查看 附B 的相關(guān)內(nèi)容螃征,經(jīng)過(guò)實(shí)際使用,該方案虛耗空間透敌,不建議使用盯滚。
附B 使用 Device Mapper storage 驅(qū)動(dòng)
手工配置 DIRECT-LVM 模式
在本次實(shí)驗(yàn)中,使用 /dev/sdb 大小為 50 G
]# lsblk -d
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 100G 0 disk
sdb 8:16 0 50G 0 disk
sr0 11:0 1 4.2G 0 rom
關(guān)閉 docker 服務(wù)
]# systemctl stop docker
注:
需提前安裝 device-mapper-persistent-data lvm2
創(chuàng)建 pv
]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.
創(chuàng)建 vg 名為 docker
]# vgcreate docker /dev/sdb
Volume group "docker" successfully created
創(chuàng)建兩個(gè) lv酗电,分別叫做 thinpool魄藕、thinpoolmeta
]# lvcreate --wipesignatures y -n thinpool docker -l 95%VG
Logical volume "thinpool" created.
]# lvcreate --wipesignatures y -n thinpoolmeta docker -l 1%VG
Logical volume "thinpoolmeta" created.
將 volume 轉(zhuǎn)換成一個(gè) thin pool,并且改變?cè)獢?shù)據(jù)的位置
]# lvconvert -y \
> --zero n \
> -c 512K \
> --thinpool docker/thinpool \
> --poolmetadata docker/thinpoolmeta
Thin pool volume with chunk size 512.00 KiB can address at most 126.50 TiB of data.
WARNING: Converting docker/thinpool and docker/thinpoolmeta to thin pool's data and metadata volumes with metadata wiping.
THIS WILL DESTROY CONTENT OF LOGICAL VOLUME (filesystem etc.)
Converted docker/thinpool and docker/thinpoolmeta to thin pool.
通過(guò) lvm 的配置文件撵术,配置 thin pool 的自動(dòng)擴(kuò)展
設(shè)置使用率達(dá)到 80% 時(shí)背率,自動(dòng)增加 20% 的容量
]# vim /etc/lvm/profile/docker-thinpool.profile
activation {
thin_pool_autoextend_threshold=80
thin_pool_autoextend_percent=20
}
使配置生效
]# lvchange --metadataprofile docker-thinpool docker/thinpool
Logical volume docker/thinpool changed.
啟動(dòng)動(dòng) lv 卷的監(jiān)視
]# lvs -o+seg_monitor
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert Monitor
home centos -wi-ao---- 10.00g
root centos -wi-ao---- 85.00g
swap centos -wi-ao---- <3.88g
thinpool docker twi-a-t--- <47.50g 0.00 0.02 monitored
因?yàn)橹鞍惭b時(shí)啟動(dòng)過(guò) docker 所以需要先將 /var/lib/docker 下的內(nèi)容移除,以便于 docker 使用新的 lvm pool 來(lái)存放容器的鏡像和容器內(nèi)容
]# mkdir /var/lib/docker.bk
]# mv /var/lib/docker/* /var/lib/docker.bk
]# ll /var/lib/docker
total 0
編輯 /etc/docker/daemon.json 和配置 devicemapper 存儲(chǔ)驅(qū)動(dòng)程序所需的選項(xiàng) 嫩与。如果該配置文件不存在寝姿,則新建該文件
]# vim /etc/docker/daemon.json
{
"storage-driver": "devicemapper",
"storage-opts": [
"dm.thinpooldev=/dev/mapper/docker-thinpool",
"dm.use_deferred_removal=true",
"dm.use_deferred_deletion=true"
]
}
啟動(dòng) docker
]# systemctl start docker
驗(yàn)證配置生效
]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 18.06.0-ce
Storage Driver: devicemapper
Pool Name: docker-thinpool
Pool Blocksize: 524.3kB
Base Device Size: 10.74GB
Backing Filesystem: xfs
... 省略 ...
Kernel Version: 3.10.0-693.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 16
Total Memory: 3.7GiB
Name: centos74-dev-60-44
ID: Y77C:O2PO:2H7K:JOJ7:D3KH:FHSK:JLRF:UFII:25JM:BYDO:HGYS:55PX
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
主要查看 Storage Driver、Pool Name 兩個(gè)值