项目作者: mjstealey

项目描述 :
NFS version 3 server and client in docker
高级语言: Shell
项目地址: git://github.com/mjstealey/nfs-in-docker.git
创建时间: 2018-05-18T19:11:50Z
项目社区:https://github.com/mjstealey/nfs-in-docker

开源协议:

下载


NFS in Docker

WORK IN PROGRESS

NFS version 3 server and client in docker.

About

Definitions for both an NFS server and client have been defined using CentOS 7 as the base. Using docker-compose to coordinate two node demonstration.

Volumes served by the NFS server can be defined as host volume mounts, or reside strictly inside the docker container. Volumes are mounted at runtime based on environment variables passed into the container.

Environment variables

Server

RPCNFSDCOUNT

nfsd threads - number of nfsd threads to use. Default =8.

NFS_SERVER_DIRS

NSF mounts - full path for server side NFS volumes, as seen by the container, that will be serviced. Default ='/nfs/share'. All volumes should begin with /nfs and a semicolon (:) should be used between each path definition.

Client

NFS_SERVER

FQDN or IP - of the NFS server. Default =server.

NFS_SERVER_DIRS

Volumes as provided from the NFS server. Default ='/nfs/share'.

NFS_CLIENT_DIRS

Volumes to mount on the client. Default ='/mnt/share'. Must be an in order correlation to the volumes as defined in NFS_SERVER_DIRS as that is the order they will be mounted in. Example: mount ${NFS_SERVER}:${NFS_SERVER_DIRS[0]} ${NFS_CLIENT_DIRS[0]}

Preliminary setup

docker volume

Due to differences in permissions in how macOS and Linux treat host mounted volumes, a docker volume will be defined for use by the primary NFS export directory, and bound to the server container.

Linux

Create directory named nfs and create a docker volume with it:

  1. mkdir nfs
  2. docker volume create \
  3. --name nfs \
  4. --opt type=tmpfs \
  5. --opt device=$(pwd)/nfs \
  6. --opt o=bind

Verify creation of volume:

  1. $ docker volume inspect nfs
  2. [
  3. {
  4. "CreatedAt": "2018-05-18T13:37:27-04:00",
  5. "Driver": "local",
  6. "Labels": {},
  7. "Mountpoint": "/var/lib/docker/volumes/nfs/_data",
  8. "Name": "nfs",
  9. "Options": {
  10. "device": "/home/$USER/nfs-in-docker/nfs",
  11. "o": "bind",
  12. "type": "tmpfs"
  13. },
  14. "Scope": "local"
  15. }
  16. ]

Viewing the contents of the volume: Since the Linux volume is bound to the host, we can simply observe the contents using ls.

  1. ls -lR nfs

macOS

Create docker volume named nfs:

  1. docker volume create \
  2. --name nfs \
  3. --driver local \
  4. --opt type=tmpfs \
  5. --opt device=tmpfs

Verify creation of volume:

  1. $ docker volume inspect nfs
  2. [
  3. {
  4. "CreatedAt": "2018-05-18T16:09:05Z",
  5. "Driver": "local",
  6. "Labels": {},
  7. "Mountpoint": "/var/lib/docker/volumes/nfs/_data",
  8. "Name": "nfs",
  9. "Options": {
  10. "device": "tmpfs",
  11. "type": "tmpfs"
  12. },
  13. "Scope": "local"
  14. }
  15. ]

Viewing the contents of the volume: Run this from your Mac terminal and it’ll drop you in a container with full permissions on the Moby VM. This also works for Docker for Windows for getting in Moby Linux VM (doesn’t work for Windows Containers).

  1. docker run -it --rm --privileged --pid=host justincormack/nsenter1

List docker’s volumes

  1. ls /var/lib/docker/volumes

more info: https://github.com/justincormack/nsenter1

Start the docker-compose.yml file

A docker-compose.yml file has been provided to create the two node server and client network for demonstration.

  1. $ docker-compose up -d
  2. Creating client ... done
  3. Creating server ... done

Once run the user should observe two new containers

  1. $ docker-compose ps
  2. Name Command State Ports
  3. -------------------------------------------------------------------
  4. client /usr/local/bin/tini -- /do ... Up
  5. server /usr/local/bin/tini -- /do ... Up 111/udp, 2049/tcp

At this point the NFS server container should be serving four directories to the NFS client container.

From the server:

  1. $ docker exec server cat /etc/exports
  2. /nfs/secret *(rw,sync,no_subtree_check,no_root_squash,fsid=272)
  3. /nfs/home *(rw,sync,no_subtree_check,no_root_squash,fsid=281)
  4. /nfs/modules *(rw,sync,no_subtree_check,no_root_squash,fsid=238)
  5. /nfs/modulefiles *(rw,sync,no_subtree_check,no_root_squash,fsid=250)

From the client:

  1. $ docker exec client showmount -e server
  2. Export list for server:
  3. /nfs/modulefiles *
  4. /nfs/modules *
  5. /nfs/home *
  6. /nfs/secret *
  7. $ docker exec client cat /etc/fstab
  8. ### <server>:</remote/export> </local/directory> <nfs-type> <options> 0 0
  9. server:/nfs/secret /secret nfs rw,hard,intr 0 0
  10. server:/nfs/home /home nfs rw,hard,intr 0 0
  11. server:/nfs/modules /opt/apps/Linux nfs rw,hard,intr 0 0
  12. server:/nfs/modulefiles /opt/apps/modulefiles/Linux nfs rw,hard,intr 0 0

The directories should all initially be empty (example using Linux volume mount).

  1. $ ls -lR nfs
  2. nfs:
  3. total 0
  4. drwxrwxrwx 2 root root 6 May 18 13:37 home
  5. drwxrwxrwx 2 root root 6 May 18 13:37 modulefiles
  6. drwxrwxrwx 2 root root 6 May 18 13:37 modules
  7. drwxrwxrwx 2 root root 6 May 18 13:37 secret
  8. nfs/home:
  9. total 0
  10. nfs/modulefiles:
  11. total 0
  12. nfs/modules:
  13. total 0
  14. nfs/secret:
  15. total 0

Test with nfs-test.sh

A script named nfs-test.sh has been provided to test the NFS mounts.

Run $ ./nfs-test.sh, the following output should be observed:

  1. ### NFS test ###
  2. ### write on server ###
  3. $ touch /nfs/home/server-touch-home
  4. $ touch /nfs/secret/server-touch-secret
  5. $ touch /nfs/modules/server-touch-modules
  6. $ touch /nfs/modulefiles/server-touch-modulefiles
  7. ### read from client ###
  8. $ ls -l /home
  9. total 0
  10. -rw-r--r-- 1 root root 0 May 18 17:45 server-touch-home
  11. $ ls -l /secret
  12. total 0
  13. -rw-r--r-- 1 root root 0 May 18 17:45 server-touch-secret
  14. $ ls -l /opt/apps/Linux
  15. total 0
  16. -rw-r--r-- 1 root root 0 May 18 17:45 server-touch-modules
  17. $ ls -l /opt/apps/modulefiles/Linux
  18. total 0
  19. -rw-r--r-- 1 root root 0 May 18 17:45 server-touch-modulefiles
  20. ### write on client ###
  21. $ touch /home/client-touch-home
  22. $ touch /secret/client-touch-secret
  23. $ touch /opt/apps/Linux/client-touch-modules
  24. $ touch /opt/apps/modulefiles/Linux/client-touch-modulefiles
  25. ### read from server ###
  26. $ ls -l /nfs/home
  27. total 0
  28. -rw-r--r-- 1 root root 0 May 18 17:45 client-touch-home
  29. -rw-r--r-- 1 root root 0 May 18 17:45 server-touch-home
  30. $ ls -l /nfs/secret
  31. total 0
  32. -rw-r--r-- 1 root root 0 May 18 17:45 client-touch-secret
  33. -rw-r--r-- 1 root root 0 May 18 17:45 server-touch-secret
  34. $ ls -l /nfs/modules
  35. total 0
  36. -rw-r--r-- 1 root root 0 May 18 17:45 client-touch-modules
  37. -rw-r--r-- 1 root root 0 May 18 17:45 server-touch-modules
  38. $ ls -l /nfs/modulefiles
  39. total 0
  40. -rw-r--r-- 1 root root 0 May 18 17:45 client-touch-modulefiles
  41. -rw-r--r-- 1 root root 0 May 18 17:45 server-touch-modulefiles
  42. ### create user=worker, gid=1000, uid=1000 from client ###
  43. $ groupadd --gid 1000 worker && useradd -m -c "Workflow user" -d /home/worker --uid 1000 -g worker -s /bin/bash worker
  44. ### read from client ###
  45. $ ls -l /home
  46. total 0
  47. -rw-r--r-- 1 root root 0 May 18 17:45 client-touch-home
  48. -rw-r--r-- 1 root root 0 May 18 17:45 server-touch-home
  49. drwx------ 2 worker worker 59 May 18 17:45 worker
  50. ### read from server ###
  51. $ ls -l /nfs/home
  52. total 0
  53. -rw-r--r-- 1 root root 0 May 18 17:45 client-touch-home
  54. -rw-r--r-- 1 root root 0 May 18 17:45 server-touch-home
  55. drwx------ 2 1000 1000 59 May 18 17:45 worker

The directories of the nfs volume should now be populated (example using Linux volume mount).

  1. $ ls -lR nfs
  2. nfs:
  3. total 0
  4. drwxrwxrwx 3 root root 67 May 18 13:45 home
  5. drwxrwxrwx 2 root root 68 May 18 13:45 modulefiles
  6. drwxrwxrwx 2 root root 60 May 18 13:45 modules
  7. drwxrwxrwx 2 root root 58 May 18 13:45 secret
  8. nfs/home:
  9. total 0
  10. -rw-r--r-- 1 root root 0 May 18 13:45 client-touch-home
  11. -rw-r--r-- 1 root root 0 May 18 13:45 server-touch-home
  12. drwx------ 2 1000 1000 59 May 18 13:45 worker
  13. ls: cannot open directory nfs/home/worker: Permission denied
  14. nfs/modulefiles:
  15. total 0
  16. -rw-r--r-- 1 root root 0 May 18 13:45 client-touch-modulefiles
  17. -rw-r--r-- 1 root root 0 May 18 13:45 server-touch-modulefiles
  18. nfs/modules:
  19. total 0
  20. -rw-r--r-- 1 root root 0 May 18 13:45 client-touch-modules
  21. -rw-r--r-- 1 root root 0 May 18 13:45 server-touch-modules
  22. nfs/secret:
  23. total 0
  24. -rw-r--r-- 1 root root 0 May 18 13:45 client-touch-secret
  25. -rw-r--r-- 1 root root 0 May 18 13:45 server-touch-secret