docker使用nfs 做跨主機儲存
構建容錯應用程式時,可能需要配置同一服務的多個副本才能訪問相同的檔案。
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
[[email protected] ~]# yum install nfs-utils rpcbind -y [[email protected] ~]# mkdir -p /data/nfs/docker [[email protected] ~]# echo "/data/nfs *(rw,no_root_squash,sync)">>/etc/exports [[email protected] ~]# exportfs -r [[email protected] ~]# systemctl start rpcbind nfs-server [[email protected] ~]# 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. [[email protected] ~]# showmount -e localhost Export list for localhost: /data/nfs *
docker節點需要安裝nfs 客戶端
[email protected]:~# sudo apt install nfs-common [email protected]-master:~# showmount -e 192.168.138.130 Export list for 192.168.138.130: /data/nfs * [email protected]-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 [email protected]-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" } ]
[email protected]:~# sudo apt install nfs-common [email protected]-master:~# showmount -e 192.168.138.130 Export list for 192.168.138.130: /data/nfs * [email protected]-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 [email protected]-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 在一臺宕機或刪除,重啟一個容器或在另一臺重啟一個檢視資料情況;
[email protected]:~# docker run -dit --name data1 -v volume-nfs:/mnt ubuntu:16.04 05d6e8cb75dc99a71f11c6c2a0833369b40be7dc9f650c814b403d519b11d50d [email protected]-master:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 05d6e8cb75dc ubuntu:16.04 "/bin/bash" 3 seconds ago Up 1 second data1 [email protected]-master:~# docker exec data1 ls /mnt docker houpj.txt 刪除容器在另一臺節點 [email protected]:~# docker run -dit --name data2 -v volume-nfs:/mnt ubuntu:16.04 b8fff79ccaeaeac97161bb14d69d4ae6ea21ce00ae9c95ecd476dcfe2640b08f [email protected]:~# 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 [email protected]:~# docker exec -it data2 /bin/bash [email protected]:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [email protected]:/# ls /mnt/ docker houpj.txt
參考:
https://www.cnblogs.com/elvi/p/8463673.html
https://forums.docker.com/t/docker-volume-create/41160