这一节介绍docker的数据卷,我们首先聊一下虚拟化,然后操作一下docker数据卷。
1、虚拟化
虚拟化是云计算的基础,我们接触最多的虚拟化产品openstack、vmware、docker、yarn、mesos等这些都是虚拟化相关的产品。其中如果按照功能分类,又可以分为cpu虚拟化、内存虚拟化、IO虚拟化、网络虚拟化等。但是大家是否注意到没有硬盘虚拟化。因为什么呢?这是因为openstack或者vmware都是认为采用共享存储的网络云盘(NAS)作为存储工具,所以不单独将磁盘进行虚拟化。
一般情况下按照磁盘使用程度可以将虚拟机分为两种类型:web服务器、数据库。web服务器一般只需要挂载一个50G的磁盘就足够用了,一般用于存放应用备份以及日志,数据库类型的虚拟机需要挂载一个200G~500g的磁盘。当然现在类似ALAP数据库增加该虚拟机有可能负担较大的计算任务,所以会挂载多个盘。
2、数据卷
docker挂载数据卷和linux挂载磁盘没什么区别,所以我们只需要把docker数据卷挂载这一块操作一下就好了。
1、容器常用的方式是将宿主机上的文件夹挂载到容器中的一个文件夹
2、挂载文件常见很少见,由于有些文件操作是先创建一份新文件再替换掉老文件,这种情况下文件inode就会变化,docker就会出现问题
3、容器可以继承一个容器挂载的数据卷
3、操作
3.1 在一个容器上挂载两个数据卷
root@ubuntu16server:/# docker run -it -v /opt/data1:/data1 -v /opt/data2:/data2 ubuntu /bin/bash
root@a571b7338fa1:/#
root@a571b7338fa1:/#
root@a571b7338fa1:/#
root@a571b7338fa1:/#
root@a571b7338fa1:/# ls /
bin boot data1 data2 dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@a571b7338fa1:/#
查看数据卷:
root@ubuntu16server:/# docker inspect a571b7338fa1
[
{
"Id": "a571b7338fa1c9e2890fe9f72610bc94b5c94f4790f5fe6322ee3ccc1364730c",
"Created": "2019-05-10T14:43:39.576855888Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-05-10T14:43:40.285177573Z",
"FinishedAt": "2019-05-10T14:44:10.937686762Z"
},
"Image": "sha256:d131e0fa2585a7efbfb187f70d648aa50e251d9d3b7031edf4730ca6154e221e",
"ResolvConfPath": "/var/lib/docker/containers/a571b7338fa1c9e2890fe9f72610bc94b5c94f4790f5fe6322ee3ccc1364730c/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/a571b7338fa1c9e2890fe9f72610bc94b5c94f4790f5fe6322ee3ccc1364730c/hostname",
"HostsPath": "/var/lib/docker/containers/a571b7338fa1c9e2890fe9f72610bc94b5c94f4790f5fe6322ee3ccc1364730c/hosts",
"LogPath": "/var/lib/docker/containers/a571b7338fa1c9e2890fe9f72610bc94b5c94f4790f5fe6322ee3ccc1364730c/a571b7338fa1c9e2890fe9f72610bc94b5c94f4790f5fe6322ee3ccc1364730c-json.log",
"Name": "/peaceful_wright",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/opt/data1:/data1",
"/opt/data2:/data2"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"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,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"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/a0ff7054e46f83c99e37afe081064fb3b4ef0124ab844d7203cb6037822fb4d0-init/diff:/var/lib/docker/overlay2/c6fbad833505793da0e29809ed3ec3475a1c7489c57cadc009f43da0b3a7db08/diff:/var/lib/docker/overlay2/2201aeef542fe4e6eb1ead3d807b3d6503af739bbbb3502d9e7c4f59d245b918/diff:/var/lib/docker/overlay2/7f234d3a44d498d3aa31e49878d6a6b1e0bcce547d7d2bf0e273b5abc5f48f66/diff:/var/lib/docker/overlay2/c2ac04abc8a10f442ac993ee71820da4594445ff77a9da93f391c6cf41db23d2/diff",
"MergedDir": "/var/lib/docker/overlay2/a0ff7054e46f83c99e37afe081064fb3b4ef0124ab844d7203cb6037822fb4d0/merged",
"UpperDir": "/var/lib/docker/overlay2/a0ff7054e46f83c99e37afe081064fb3b4ef0124ab844d7203cb6037822fb4d0/diff",
"WorkDir": "/var/lib/docker/overlay2/a0ff7054e46f83c99e37afe081064fb3b4ef0124ab844d7203cb6037822fb4d0/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/opt/data1",
"Destination": "/data1",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/opt/data2",
"Destination": "/data2",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "a571b7338fa1",
"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": "ubuntu",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "d643b24199a246ce42016b3161c5725d11df07e44ed3f4f99a1f997a628ba561",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/d643b24199a2",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "8ca155bb114473abfbcec83f36e2006af0175a03ac525307c14ab107fcd1cf28",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
root@ubuntu16server:/#
发现这个容器挂载了两个数据卷。
3.2 创建一个新容器,数据卷继承a571b7338fa1这个容器
root@ubuntu16server:/# docker run -it --volumes-from a571b7338fa1 ubuntu /bin/bash
root@487aa6a1d926:/# ls /
bin boot data1 data2 dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@487aa6a1d926:/#
数据卷可以挂载日志盘以及数据盘,在实际使用中一般都是挂载云盘,这样不管docker容器在调度是在哪里启动都可以挂载到相应的盘上去。