docker数据卷:数据卷管理和convoy卷插件
程序员文章站
2022-07-01 11:27:43
...
一、 docker数据卷管理
1. 为什么要用数据卷
2. docker提供两种卷
[aaa@qq.com ~]# docker network prune 将不用的网络清除掉
[aaa@qq.com ~]# docker run -d --name vm1 -v /opt/website:/usr/share/nginx/html nginx
56cb4e323a20c34dbd36b23d73647e7f83324f5bad83752796e6c91e724f0cf9
[aaa@qq.com website]# curl 172.17.0.2
/opt/website
[aaa@qq.com docker]# docker run -d --name registry registry
fc4dd2676bc25ad2db56daf04b867f332383295ec3cc82cd84dd65d5f2cd1d2f
[aaa@qq.com volumes]# ls
b3d8a5831c678f8bd94d72f59ed76adb41c3f7c701e8767cb1a890ae66ef7986 metadata.db
通过docker volume可以将容器内的内容复制到挂载点
[aaa@qq.com _data]# docker run -d --name vm2 -v /usr/share/nginx/html nginx
e6941d7e4ec13979bb10a62a31c707bed2f981d51f99cabf370fc772259c6fa3
[aaa@qq.com 3117caa56a6c78ac79f7b94ed441ce19d08b5ffd69cd0bd22ef1189b017c3eae]# cd _data/
[aaa@qq.com _data]# ls
50x.html index.html
3. bind mount 和docker managed volume对比
相同点:两者都是host文件系统中的某个路径
不同点:
二、卷插件简介
三、convoy卷插件
1. 支持三种运行方式:devicemapper,NFS,EBS。
以下实验使用nfs的方式。
下载软件:
https://github.com/rancher/convoy/releases/download/v0.5.0/convoy.tar.gz
在所有节点提前挂载NFS存储
2. convoy卷插件安装
[aaa@qq.com ~]# yum install -y nfs-utils
[aaa@qq.com ~]# yum install -y nfs-utils
[aaa@qq.com ~]# systemctl start rpcbind
[aaa@qq.com ~]# systemctl start rpcbind
同步nfs
[aaa@qq.com ~]# cat /etc/exports
/nfs *(rw)
[aaa@qq.com ~]# systemctl start nfs
[aaa@qq.com ~]# showmount -e
Export list for server1:
/nfs *
[aaa@qq.com /]# mkdir /nfs
[aaa@qq.com ~]# mkdir /nfs
[aaa@qq.com ~]# showmount -e server1
Export list for server1:
/nfs *
[aaa@qq.com ~]# mount 172.25.60.1:/nfs/ /nfs/
[aaa@qq.com ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
172.25.60.1:/nfs 17811456 2572160 15239296 15% /nfs
[aaa@qq.com /]# cat /etc/exports 因为server2使用的时nfsnobody的身份,无法操作nfs挂载目录中的数据,所以将其改为no_root_squash
/nfs *(rw,no_root_squash)
[aaa@qq.com /]# exportfs -rv
exporting *:/nfs
server2可以读写nfs挂载点
[aaa@qq.com /]# cd /nfs/
[aaa@qq.com nfs]# echo hello > test
[aaa@qq.com ~]# cd /nfs/
[aaa@qq.com nfs]# ls
test
[aaa@qq.com nfs]# rm -fr test
docker通过卷插件(plugins)访问nfs
[aaa@qq.com ~]# tar zxf convoy.tar.gz
[aaa@qq.com ~]# cd convoy/
[aaa@qq.com convoy]# ls
convoy convoy-pdata_tools SHA1SUMS
[aaa@qq.com convoy]# mv * /usr/local/bin/
[aaa@qq.com ~]# which convoy
/usr/local/bin/convoy
[aaa@qq.com ~]# tar -vzxf convoy.tar.gz
convoy/
convoy/convoy-pdata_tools
convoy/convoy
convoy/SHA1SUMS
[aaa@qq.com ~]# cd convoy/
[aaa@qq.com convoy]# mv * /usr/local/bin/
[aaa@qq.com convoy]# which convoy
/usr/local/bin/convoy
[aaa@qq.com ~]# mkdir -p /etc/docker/plugins/
/etc/docker时docker缺省的一个扫描路径
[aaa@qq.com convoy]# mkdir -p /etc/docker/plugins/
将convoy打入后台
[aaa@qq.com ~]# convoy daemon --drivers vfs --driver-opts vfs.path=/nfs &
[aaa@qq.com nfs]# ll /run/convoy/convoy.sock
srwxr-xr-x 1 root root 0 Apr 9 22:21 /run/convoy/convoy.sock
[aaa@qq.com nfs]# echo "unix:///var/run/convoy/convoy.sock" > /etc/docker/plugins/convoy.spec
[aaa@qq.com convoy]# convoy daemon --drivers vfs --driver-opts vfs.path=/nfs &
[aaa@qq.com convoy]# ll /run/convoy/convoy.sock
srwxr-xr-x 1 root root 0 Apr 9 22:26 /run/convoy/convoy.sock
[aaa@qq.com convoy]# echo "unix:///var/run/convoy/convoy.sock" > /etc/docker/plugins/convoy.spec
创建卷
[aaa@qq.com nfs]# convoy create vol1
[aaa@qq.com nfs]# ls
config vol1
[aaa@qq.com nfs]# convoy list
{
"vol1": {
"Name": "vol1",
"Driver": "vfs",
"MountPoint": "",
"CreatedTime": "Thu Apr 09 22:29:04 +0800 2020",
"DriverInfo": {
"Driver": "vfs",
"MountPoint": "",
"Path": "/nfs/vol1",
"PrepareForVM": "false",
"Size": "0",
"VolumeCreatedAt": "Thu Apr 09 22:29:04 +0800 2020",
"VolumeName": "vol1"
},
"Snapshots": {}
}
}
[aaa@qq.com nfs]# docker run -d --name vm1 -v vol1:/usr/share/nginx/html nginx
[aaa@qq.com nfs]# ls vol1/
50x.html index.html
[aaa@qq.com ~]# ls /nfs/vol1/
50x.html index.html
容器可以销毁但是原来的数据保存在nfx中
[aaa@qq.com nfs]# docker rm -f vm1
[aaa@qq.com nfs]# ls vol1/
50x.html index.html
数据同步过程:
docker engine --- /etc/docker/plugins/convoy.spec --- /run/convoy/convoy.sock ---api ---vol1 --- server2(同步到其它的节点)
删除卷
[aaa@qq.com ~]# convoy delete vol1
[aaa@qq.com ~]# mv /etc/docker/plugins/convoy.spec /mnt/ 或者直接删掉plugins
kill (convoy服务)
[aaa@qq.com docker]# systemctl daemon-reload
[aaa@qq.com ~]# systemctl restart docker
docker默认创建的volume在/var/lib/docker/volumes/中
[aaa@qq.com ~]# docker volume create vol1
vol1
[aaa@qq.com ~]# ls /var/lib/docker/volumes/
metadata.db vol1
上一篇: 追女孩秘诀
下一篇: Docker介绍和基本管理