Docker&Kubernetes ❀ Docker Datavolume 数据卷的使用与原理说明
Posted 无糖可乐没有灵魂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker&Kubernetes ❀ Docker Datavolume 数据卷的使用与原理说明相关的知识,希望对你有一定的参考价值。
文章目录
1、数据卷概念
Docker产生的数据如果不通过Docker commit生成新的镜像,数据作为镜像的一部分保存下来,那么当容器删除后,数据自然也就被删除了,为了保护数据此处使用卷;
容器之间可以数据共享;
卷 就是目录和文件,存在与一个或多个容器中,由Docker挂载到容器,但是不属于联合文件系统,因此能够绕过Union File System提供一下用于持续存储或共享数据的特征:
- 数据卷可在容器之间共享或重用数据;
- 数据卷中的更改可以直接生效;
- 数据卷中的更改不会包含在镜像的更新中 ;
- 数据卷的生命周期一直持续到没有容器使用它为止;
卷的设计目的就是数据的持久化,完全独立于容器的生存周期,因此Docker不会在容器删除时删除其挂载的数据卷;
从容器内复制文件到某主机上docker cp 容器ID:容器内路径 目标主机路径
2、数据卷的使用
容器内添加数据卷
2.1 直接命令添加
命令格式:docker run -it -v /宿主机绝对路径:/容器内目录 镜像名
实验如下:
[root@localhost ~]# ls / | grep -w myDataVolume
[root@localhost ~]# docker run -it centos
[root@7896967b6746 /]# ls | grep -w dataVolumeContainer
[root@localhost ~]# docker run -it -v /myDataVolume:/dataVolumeContainer centos #数据共享;
[root@localhost ~]# docker run -it -v /myDataVolume:/dataVolumeContainer:ro centos #写保护;
#写保护使得宿主机单向进行写操作,容器只能查看;
[root@475fabb64590 /]# ls / | grep -w dataVolumeContainer #注意容器ID;
dataVolumeContainer
[root@localhost ~]# ls / | grep -w myDataVolume
myDataVolume
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
475fabb64590 centos "/bin/bash" 4 minutes ago Up 4 minutes amazing_williamson
7896967b6746 centos "/bin/bash" 8 minutes ago Up 8 minutes affectionate_keldysh
查看是否挂载成功
[root@localhost ~]# docker inspect 475fabb64590
[
"Id": "475fabb64590421b2ecd129614c4b66d6fc655e3ce1894b9965b3c2d6d32dafe",
"Created": "2021-03-08T09:03:59.643698063Z",
"Path": "/bin/bash",
"Args": [],
"State":
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 6245,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-03-08T09:04:00.089636568Z",
"FinishedAt": "0001-01-01T00:00:00Z"
,
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/475fabb64590421b2ecd129614c4b66d6fc655e3ce1894b9965b3c2d6d32dafe/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/475fabb64590421b2ecd129614c4b66d6fc655e3ce1894b9965b3c2d6d32dafe/hostname",
"HostsPath": "/var/lib/docker/containers/475fabb64590421b2ecd129614c4b66d6fc655e3ce1894b9965b3c2d6d32dafe/hosts",
"LogPath": "/var/lib/docker/containers/475fabb64590421b2ecd129614c4b66d6fc655e3ce1894b9965b3c2d6d32dafe/475fabb64590421b2ecd129614c4b66d6fc655e3ce1894b9965b3c2d6d32dafe-json.log",
"Name": "/amazing_williamson",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig":
"Binds": [
"/myDataVolume:/dataVolumeContainer"
],
"ContainerIDFile": "",
"LogConfig":
"Type": "json-file",
"Config":
,
"NetworkMode": "default",
"PortBindings": ,
"RestartPolicy":
"Name": "no",
"MaximumRetryCount": 0
,
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
,
"GraphDriver":
"Data":
"LowerDir": "/var/lib/docker/overlay2/f3f73c128842aa3d106287d5efe63bfdc7f67450283b1fe48ad06ad5f8f60faa-init/diff:/var/lib/docker/overlay2/4a9db94f13d4540530cd143cb0be0720bcf47eef0e35edb78c5c30a223617005/diff",
"MergedDir": "/var/lib/docker/overlay2/f3f73c128842aa3d106287d5efe63bfdc7f67450283b1fe48ad06ad5f8f60faa/merged",
"UpperDir": "/var/lib/docker/overlay2/f3f73c128842aa3d106287d5efe63bfdc7f67450283b1fe48ad06ad5f8f60faa/diff",
"WorkDir": "/var/lib/docker/overlay2/f3f73c128842aa3d106287d5efe63bfdc7f67450283b1fe48ad06ad5f8f60faa/work"
,
"Name": "overlay2"
,
"Mounts": [ #数据盘挂载;
"Type": "bind",
"Source": "/myDataVolume",
"Destination": "/dataVolumeContainer",
"Mode": "",
"RW": true, #是否存在写保护
"Propagation": "rprivate"
],
"Config": #配置文件;
"Hostname": "475fabb64590",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [ #执行终端;
"/bin/bash"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels":
"org.label-schema.build-date": "20201204",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
,
"NetworkSettings": #网络配置;
"Bridge": "",
"SandboxID": "57558675c2087cfaa484926919023cd9010d5dada5c951fc3bb22201e313ce98",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": ,
"SandboxKey": "/var/run/docker/netns/57558675c208",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "c0f4cf5ce14b3c5fa8a740769a5a87b4e8a7fe17254fb5ef53e30298e6ab07f7",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks":
"bridge":
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "2581e70a11b247a522326b69da1c598f088db7d5ba10fe51ee1d7dd88d6d94a9",
"EndpointID": "c0f4cf5ce14b3c5fa8a740769a5a87b4e8a7fe17254fb5ef53e30298e6ab07f7",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
]
容器配置文件路径
[root@localhost ~]# find / -name hostconfig.json
find: ‘/proc/4165309’: No such file or directory
/var/lib/docker/containers/4282cfbf7c4dbf04b76ebf37ceccfec78887a4f64bfa00c31812c66246b15e60/hostconfig.json
[root@Redhat8 ~]# cat /var/lib/docker/containers/4282cfbf7c4dbf04b76ebf37ceccfec78887a4f64bfa00c31812c66246b15e60/hostconfig.json
"Binds":null,"ContainerIDFile":"","LogConfig":"Type":"json-file","Config":,"NetworkMode":"default","PortBindings":,"RestartPolicy":"Name":"no","MaximumRetryCount":0,"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"CgroupnsMode":"host","Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"private","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","ConsoleSize":[0,0],"Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","BlkioWeight":0,"BlkioWeightDevice":[],"BlkioDeviceReadBps":null,"BlkioDeviceWriteBps":null,"BlkioDeviceReadIOps":null,"BlkioDeviceWriteIOps":null,"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Devices":[],"DeviceCgroupRules":null,"DeviceRequests":null,"KernelMemory":0,"KernelMemoryTCP":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":null,"OomKillDisable":false,"PidsLimit":null,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0,"MaskedPaths":["/proc/asound","/proc/acpi","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware"],"ReadonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]
验证数据是否共享
[root@localhost ~]# touch /myDataVolume/test.txt
[root@475fabb64590 /]# ls /dataVolumeContainer/
test.txt
[root@475fabb64590 /]# echo this is test! > /dataVolumeContainer/test.txt
[root@localhost ~]# cat /myDataVolume/test.txt
this is test!
配置容器停止数据同步
[root@475fabb64590 /]# exit #容器退出;
[root@localhost ~]# touch test2.txt
[root@localhost ~]# echo this is test2! > /myDataVolume/test2.txt
[root@localhost ~]# docker start 475fabb64590
475fabb64590
[root@localhost ~]# docker attach 475fabb64590 #重新登录容器;
[root@475fabb64590 /]# cat /myDataVolume/test2.txt
this is test2!
配置写保护数据共享
[root@localhost ~]# docker run -it -v /myDataVolume:/dataVolumeContainer:ro centos
[root@493b11d40260 /]#
[root@localhost ~]# docker inspect 493b11d40260
~
"Mounts": [
"Type": "bind",
"Source": "/myDataVolume",
"Destination": "/dataVolumeContainer",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
~
2.2 DockerFile添加
DockerFile是Docker images的编程,类似于Linux的Shell;
命令格式:VOLUME ["容器内路径","容器内路径","容器内路径"...]
出于可以移植和分享的考虑,-v 主机目录:容器目录这种方法不能够直接在DockerFile中实现,由于宿主机目录是依赖于特定宿主机的,并不能够保证在所有的宿主机上都存在这样的特定目录;
[root@Docker ~]# mkdir /myDocker
[root@Docker ~]# vim /myDocker/DockerFile
#volume test
FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "finished,--------successl"
CMD /bin/bash
[root@Docker ~]# docker build -f /myDocker/DockerFile -t zxc/centos .
Sending build context to Docker daemon 12.16MB
Step 1/4 : FROM centos
---> 300e315adb2f
Step 2/4 : VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
---> Running in 26c85da57a92
Removing intermediate container 26c85da57a92
---> d719126ef7c3
Step 3/4 : CMD echo "finished,--------successl"
---> Running in be54681b353f
Removing intermediate container be54681b353f
---> 38faacfc353d
Step 4/4 : CMD /bin/bash
---> Running in 12770df16d5e
Removing intermediate container 12770df16d5e
---> cc068d02bff6
Successfully built cc068d02bff6
Successfully tagged zxc/centos:latest
[root@Docker ~]# docker images zxc/centos
REPOSITORY TAG IMAGE ID CREATED SIZE
zxc/centos latest cc068d02bff6 11 seconds ago 209MB
[root@Docker ~]# docker run -it zxc/centos
[root@dac174d83dc5 /]# ls /
bin dataVolumeContainer2 etc lib lost+found mnt proc run srv tmp var
dataVolumeContainer1 dev home lib64 media opt root sbin sys usr
[root@Docker ~]# docker inspect dac174d83dc5
~
"Mounts": [
"Type": "volume",
"Name": "6f803f6075d4cb8138f7c9d9080f75dcd2e388ef6968756763e78e4b403c3326",
"Source": "/var/lib/docker/volumes/6f803f6075d4cb8138f7c9d9080f75dcd2e388ef6968756763e78e4b403c3326/_data",
"Destination": "/dataVolumeContainer1",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
,
"Type": "volume",
"Name": "4f2ac33d13c287f97990a73a741f649b9ccc3a4d6e99aa1f298875bf964346e2",
"Source": "/var/lib/docker/volumes/4f2ac33d13c287f97990a73a741f649b9ccc3a4d6e99aa1f298875bf964346e2/_data",
"Destination": "/dataVolumeContainer2",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
],
~
[root@Docker ~]# ll /var/lib/docker/volumes/
total 24
drwx-----x. 3 root root 19 Mar 9 23:41 4f2ac33d13c287f97990a73a741f649b9ccc3a4d6e99aa1f298875bf964346e2
drwx-----x. 3 root root 19 Mar 9 23:41 6f803f6075d4cb8138f7c9d9080f75dcd2e388ef6968756763e78e4b403c3326
brw-------. 1 root root 8, 3 Mar 9 23:38 backingFsBlockDev
-rw-------. 1 root root 32768 Mar 9 23:41 metadata.db
验证数据是否共享
[root@dac174d83dc5 /]# echo this is aaa! > /dataVolumeContainer1/test_file1
[root@dac174d83dc5 /]# cat /dataVolumeContainer1/test_file1
this is aaa!
[root@Docker ~]# cat /var/lib/docker/volumes/6f803f6075d4cb8138f7c9d9080f75dcd2e388ef6968756763e78e4b403c3326/_data/test_file1
this is aaa!
3、数据卷容器
3.1 数据卷容器是什么
命名的容器挂载数据卷,其他容器通过挂载这个实现数据共享,挂载数据卷的容器,称之为数据卷容器;
3.2 vloumes-from的使用
[root@Docker ~]# docker images zxc/centos
REPOSITORY TAG IMAGE ID CREATED SIZE
zxc/centos latest cc068d02bff6 47 minutes ago 209MB
以新的镜像zxc/centos创建三个Docker容器分别为a b c,容器b数据共享与容器a,容器c数据共享与容器b;
[root@Docker ~]# docker run -it --name docker_a zxc/centos
[root@bd8cf8bdc1ae /]# ls /
bin dataVolumeContainer2 etc lib lost+found mnt proc run srv tmp var dataVolumeContainer1 dev home lib64 media opt root sbin sys usr
[root@bd8cf8bdc1ae /]# touch /dataVolumeContainer1/test_a.txt
[root@bd8cf8bdc1ae /]# [root@Docker ~]#
[root@Docker ~]# docker run -it --name docker_b --volumes-from docker_a zxc/centos
[root@d01c3386d06e /]# ls /dataVolumeContainer1/
test_a.txt
[root@d01c3386d06e /]# touch /dataVolumeContainer1/test_b.txt
[root@d01c3386d06e /]# [root@Docker ~]#
[root@Docker ~]# docker run -it --name docker_c --volumes-from docker_b zxc/centos
[root@d23497032be0 /]# ls /dataVolumeContainer1/
test_a.txt test_b.txt
[root@d23497032be0 /]# touch /dataVolumeContainer1/test_c.txt
[root@d23497032be0 /]# [root@Docker ~]#
[root@Docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d23497032be0 zxc/centos "/bin/sh -c /bin/bash" 30 seconds ago Up 29 seconds docker_c
d01c3386d06e zxc/centos "/bin/sh -c /bin/bash" About a minute ago Up About a minute docker_b
bd8cf8bdc1ae zxc/centos "/bin/sh -c /bin/bash" 3 minutes ago Up 3 minutes docker_a
[root@Docker ~]# docker attach bd8cf8bdc1ae
[root@bd8cf8bdc1ae /]# ls /dataVolumeContainer1/ /容器a也存在容器b与容器c的数据;
test_a.txt test_b.txt test_c.txt
逐一删除容器a b,并观察数据状态;
[root@Docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d23497032be0 zxc/centos "/bin/sh -c /bin/bash" 4 minutes ago Up 4 minutes docker_c
d01c3386d06e zxc/centos "/bin/sh -c /bin/bash" 5 minutes ago Up 5 minutes docker_b
bd8cf8bdc1ae zxc/centos "/bin/sh -c /bin/bash" 7 minutes ago Up 7 minutes docker_a
[root@Docker ~]# docker rm -f bd8cf8bdc1ae
bd8cf8bdc1ae
[root@Docker ~]# docker attach d01c3386d06e
[root@d01c3386d06e /]# ls /dataVolumeContainer1/ /删除掉a后,b仍存在全部数据;
test_a.txt test_b.txt test_c.txt
[root@d01c3386d06e /]# read escape sequence
[root@Docker ~]# docker rm -f d01c3386d06e
d01c3386d06e
[root@Docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d23497032be0 zxc/centos "/bin/sh -c /bin/bash" 5 minutes ago Up 5 minutes docker_c
[root@Docker ~]# docker attach d23497032be0
[root@d23497032be0 /]# ls /dataVolumeContainer1/ /删除掉a b后,c仍存在全部数据;
test_a.txt test_b.txt test_c.txt
[root@d23497032be0 /]# read escape sequence
3.3 结论验证
容器之间的配置信息的传递,数据卷的生命周期一直持续到没有容器;
以上是关于Docker&Kubernetes ❀ Docker Datavolume 数据卷的使用与原理说明的主要内容,如果未能解决你的问题,请参考以下文章
Docker&Kubernetes ❀ Docker 容器技术笔记链接梳理
Docker&Kubernetes ❀ Kubernetes集群实践与部署笔记知识点梳理
Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法
Docker&Kubernetes ❀ Kubernetes集群安装部署过程与常见的错误解决方法