docker (centOS 7) 使用筆記2 - 使用nfs做爲volume

本次測試的服務器2臺,服務器#1(centos7)最爲docker容器所在的host,服務器#2(centos6)提供NFS服務git

 

1. #2上配置NFS服務github

(1) 安裝nfs軟件包docker

yum -y install nfs-utils portmap

 

(2) 配置 /etc/exportscentos

/home/nfs_share/docker01 10.10.10.127(rw,insecure,sync,no_all_squash,anonuid=65534,anongid=65534)
/home/nfs_share/docker01 10.10.10.102(ro,insecure,sync,no_all_squash,anonuid=65534,anongid=65534)

其中 rw是可讀寫,ro是隻讀bash

 

(3) 重啓服務 服務器

service rpcbind restart
service nfs restart
exportfs -a

 

(4) 查看狀態測試

# nfsstat 
Server rpc stats:
calls      badcalls   badclnt    badauth    xdrcall
0          0          0          0          0  

查看mount的目錄ui

# showmount -e 10.10.10.101
Export list for 10.10.10.101:
/home/nfs_share/docker01 10.10.10.102,10.10.10.127

 

 

2. #1上安裝支持NFS的volume plugin drivercentos7

(1) 本次使用的driver是convoy (https://github.com/rancher/convoy)spa

 下載,解壓,安裝

wget https://github.com/rancher/convoy/releases/download/v0.5.0/convoy.tar.gz
tar xzvf convoy.tar.gz
cp convoy/convoy convoy/convoy-pdata_tools /usr/local/bin/
mkdir -p /etc/docker/plugins/
bash -c 'echo "unix:///var/run/convoy/convoy.sock" > /etc/docker/plugins/convoy.spec'

(2)  掛載nfs共享目錄,啓動convoy daemon

mkdir /home/docker/nfs_share
mount -t nfs 10.10.10.101:/home/nfs_share/docker01 /home/docker/nfs_share
nohup convoy daemon --drivers vfs --driver-opts vfs.path=/home/docker/nfs_share &

nohup.out中有convoy daemon的啓動日誌:

time="2016-11-30T18:46:32+08:00" level=debug msg="Found existing config. Ignoring command line opts, loading config from /var/lib/rancher/convoy" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg= driver=vfs driver_opts=map[vfs.path:/home/docker/nfs_share] event=init pkg=daemon reason=prepare root="/var/lib/rancher/convoy"
time="2016-11-30T18:46:32+08:00" level=debug msg= driver=vfs event=init pkg=daemon reason=complete
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering GET, /volumes/" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering GET, /snapshots/" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering GET, /backups/list" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering GET, /backups/inspect" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering GET, /info" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering GET, /volumes/list" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering POST, /volumes/umount" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering POST, /snapshots/create" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering POST, /backups/create" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering POST, /volumes/create" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering POST, /volumes/mount" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering DELETE, /volumes/" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering DELETE, /snapshots/" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering DELETE, /backups" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering plugin handler POST, /VolumeDriver.Get" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering plugin handler POST, /VolumeDriver.List" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering plugin handler POST, /Plugin.Activate" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering plugin handler POST, /VolumeDriver.Create" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering plugin handler POST, /VolumeDriver.Remove" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering plugin handler POST, /VolumeDriver.Mount" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering plugin handler POST, /VolumeDriver.Unmount" pkg=daemon
time="2016-11-30T18:46:32+08:00" level=debug msg="Registering plugin handler POST, /VolumeDriver.Path" pkg=daemon

 

(3) 啓動容器測試

docker run -t -i -v test_volume1:/test --volume-driver=convoy centos:6 /bin/bash

如今#2服務器上能夠看到docker run創建的test_volume1目錄

# ll /home/nfs_share/docker01/
總用量 8
drwx------. 2 nfsnobody nfsnobody 4096 11月 30 19:11 config
drwx------. 2 nfsnobody nfsnobody 4096 11月 30 19:11 test_volume1

在#2服務器上test_volume1生成1個文件測試:

# echo 'Test01-----' > test.txt

在容器裏查看:

# ll
total 4
-rw-r--r--. 1 root root 12 Nov 30 11:23 test.txt

 #1服務器中查看已經掛載的卷

# convoy list
{
    "test_volume1": {
        "Name": "test_volume1",
        "Driver": "vfs",
        "MountPoint": "/home/docker/nfs_share/test_volume1",
        "CreatedTime": "Wed Nov 30 19:10:51 +0800 2016",
        "DriverInfo": {
            "Driver": "vfs",
            "MountPoint": "/home/docker/nfs_share/test_volume1",
            "Path": "/home/docker/nfs_share/test_volume1",
            "PrepareForVM": "false",
            "Size": "0",
            "VolumeCreatedAt": "Wed Nov 30 19:10:51 +0800 2016",
            "VolumeName": "test_volume1"
        },
        "Snapshots": {}
    }
}

注意convoy delete <volume_name>能夠刪除卷,刪除卷會致使實際的目錄和文件被物理刪除。

相關文章
相關標籤/搜索