构建容错应用程序时,可能需要配置同一服务的多个副本才能访问相同的文件。
There are several ways to achieve this when developing your applications. One is to add logic to your application to store files on a cloud object storage system like Amazon S3. Another is to create volumes with a driver that supports writing files to an external storage system like NFS or Amazon S3.
Volume drivers allow you to abstract the underlying storage system from the application logic. For example, if your services use a volume with an NFS driver, you can update the services to use a different driver, as an example to store data in the cloud, without changing the application logic.
安装nfs
[root@bogon ~]# yum install nfs-utils rpcbind -y [root@bogon ~]# mkdir -p /data/nfs/docker [root@bogon ~]# echo "/data/nfs *(rw,no_root_squash,sync)">>/etc/exports [root@bogon ~]# exportfs -r [root@bogon ~]# systemctl start rpcbind nfs-server [root@bogon ~]# systemctl enable rpcbind nfs-server Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service. [root@bogon ~]# showmount -e localhost Export list for localhost: /data/nfs *
docker节点需要安装nfs 客户端
root@playbook-master:~# sudo apt install nfs-common root@playbook-master:~# showmount -e 192.168.138.130 Export list for 192.168.138.130: /data/nfs * root@playbook-master:~# docker volume create --driver local --opt type=nfs --opt o=addr=192.168.138.130,rw --opt device=:/data/nfs volume-nfs volume-nfs root@playbook-master:~# docker volume inspect volume-nfs [ { "CreatedAt": "2018-12-20T12:09:03+08:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/volume-nfs/_data", "Name": "volume-nfs", "Options": { "device": ":/data/nfs", "o": "addr=192.168.138.130,rw", "type": "nfs" }, "Scope": "local" } ]
root@playbook-master:~# sudo apt install nfs-common root@playbook-master:~# showmount -e 192.168.138.130 Export list for 192.168.138.130: /data/nfs * root@playbook-master:~# docker volume create --driver local --opt type=nfs --opt o=addr=192.168.138.130,rw --opt device=:/data/nfs volume-nfs volume-nfs root@playbook-master:~# docker volume inspect volume-nfs [ { "CreatedAt": "2018-12-20T12:09:03+08:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/volume-nfs/_data", "Name": "volume-nfs", "Options": { "device": ":/data/nfs", "o": "addr=192.168.138.130,rw", "type": "nfs" }, "Scope": "local" } ]
测试docker 在一台宕机或删除,重启一个容器或在另一台重启一个查看数据情况;
root@playbook-master:~# docker run -dit --name data1 -v volume-nfs:/mnt ubuntu:16.04 05d6e8cb75dc99a71f11c6c2a0833369b40be7dc9f650c814b403d519b11d50d root@playbook-master:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 05d6e8cb75dc ubuntu:16.04 "/bin/bash" 3 seconds ago Up 1 second data1 root@playbook-master:~# docker exec data1 ls /mnt docker houpj.txt 删除容器在另一台节点 root@ubuntu:~# docker run -dit --name data2 -v volume-nfs:/mnt ubuntu:16.04 b8fff79ccaeaeac97161bb14d69d4ae6ea21ce00ae9c95ecd476dcfe2640b08f root@ubuntu:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b8fff79ccaea ubuntu:16.04 "/bin/bash" 4 seconds ago Up 3 seconds data2 c3e3c995acac busybox "sh" 12 minutes ago Up 12 minutes busybox7 root@ubuntu:~# docker exec -it data2 /bin/bash root@b8fff79ccaea:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@b8fff79ccaea:/# ls /mnt/ docker houpj.txt
参考:
https://www.cnblogs.com/elvi/p/8463673.html
https://forums.docker.com/t/docker-volume-create/41160