docker (centOS 7) 使用笔记2 - 使用nfs作为volume

本次测试的服务器2台,服务器#1(centos7)最为docker容器所在的host,服务器#2(centos6)提供NFS服务

 

1. #2上配置NFS服务

(1) 安装nfs软件包

yum -y install nfs-utils portmap

 

(2) 配置 /etc/exports

/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是只读

 

(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的目录

# 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 driver

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

 下载,解压,安装

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>可以删除卷,删除卷会导致实际的目录和文件被物理删除。

 

posted @ 2017-08-01 14:22  Beef Liu  阅读(683)  评论(0编辑  收藏  举报