Categories
AI Uncategorized

NFS for Docker

Setup a NFS Server and Client on the Raspberry Pi

Reference and Links

Setup a NFS Server/Client on the RaspberryPi with docker swarm:

A manager node with IP: 10.212.99.144

Four worker node with IPs: 10.212.99.140 to 10.212.99.143

Server Side (Manager Node) – Prepared Disks and Directories to be mount for NFS

With 5 nodes in docker swarm mode (i.e. 1 manager and 4 workers) and prepare the directory for shared volume in a manager node via NFS. First, login manager node with ssh as pi with your password:

$ ssh pi@10.212.99.144
$ sudo mkdir -p /mnt/shared
$ sudo chown pi:pi /mnt/shared
$ sudo chmod 755 /mnt/shared

If your disk was formated in windows file type (NTFS), you are required to install ntfs-3g. For demonstration, I will be using /mnt/shared in the same disk as my cluster, but if you have other usb disks (sdaX…) that you would like to mount, mount them like the following:

$ sudo apt-get update -y && sudo apt-get upgrade -y
$ sudo apt-get ntfs-3g
$ sudo lsblk
$ sudo mount /dev/sda1 /mnt/shared
$ sudo chown -R pi:pi /mnt/shared/existing_dirs
$ sudo find /opt/nfs/existing_dirs/ -type d -exec chmod 755 {} \;
$ sudo find /opt/nfs/existing_dirs/ -type f -exec chmod 644 {} \;

If you mounted the disk, and you would like to mount the disk on boot, we need to add it to our /etc/fstab. We can get the disk information (i.e. mount point, dev name, label, block_size, type, PARTUUID or UUID)by running either:

$ sudo lsblk
# or
$ sudo blkid

Populate the /etc/fstab with your disk info, it will look more or less like:

/dev/sda1 /mnt/shared ext4 defaults,noatime 0 0
OR
PARTUUID=30a9ee06-01 /mnt/shared  ntfs defaults,noatime 0 0

Append rootdelay=10 after rootwait in /boot/cmdline.txt, then reboot for the changes to become active.

Setup the Server Side – Installing NFS Server

Install the NFS Server packages:

$ sudo apt install nfs-kernel-server nfs-common rpcbind -y

Configure the paths in /etc/exports, we need to uid gid for the user that owns permission that we need to pass to the NFS Client. To get that:

$ id pi
uid=1000(pi) gid=1000(pi)

Setup our path that we would like to be accessible in local lan via NFS:

/mnt/shared
10.212.99.0/24(rw,all_squash,no_hide,insecure,async,no_subtree_check,anonuid=1000,anongid=1000)

If you would like to have open access:

/mnt/shared *(rw,all_squash,no_hide,insecure,async,no_subtree_check,anonuid=1000,anongid=1000)

Export the config, enable the services on boot and start NFS:

$ sudo exportfs -ra
$ showmount -e 10.212.99.144
$ sudo systemctl enable rpcbind
$ sudo systemctl enable nfs-kernel-server
$ sudo systemctl enable nfs-common
$ sudo systemctl start rpcbind
$ sudo systemctl start nfs-kernel-server
$ sudo systemctl start nfs-common

Setup the NFS Clients (4 Worker Nodes)

Login clients via ssh and install the NFS or NFS4 Client packages:

$ sudo apt install nfs-common -y

Create the mountpoint of choice and change the ownership:

$ sudo chown pi:pi /mnt/shared

Setup the /etc/idmapd.conf to match the user:

[Mapping]
Nobody-User = pi
Nobody-Group = pi

Mount the NFS Share in server 10.212.99.144 to your local mount point:

$ sudo mount 10.212.99.144:/mnt/shared /mnt/shared

Enable mount on boot via /etc/fstab:

10.212.99.144:/mnt/shared /mnt/shared nfs rw 0 0
or 
10.212.99.144:/mnt/shared      /mnt/shared      nfs4    nofail,x-systemd.automount,x-systemd.after=network-online.target,_netdev  0  0