Setup NFS Server on Ubuntu
NFS - Network File System is a distributed file system protocol developed by Sun Systems. It facilitates a client computer to have access to file/folder over network like a local folder. This is a easy/simple way to provide persistant storage for your docker-swarm or kubernetes cluster. In both these cases, the container (or pod) run on one of the available nodes (servers), and when we have replicas it will be running on more than one node. So local storage on the server is out of the question.
With NFS we can directly mount the folder in the container. We can control everything from the configuration file. And thanks to the NFS storage we can increase and reduce node without worries as well.
Once you have your ubuntu server ready follow the following steps:
Step 1: Setup Software
|
|
Step 2: Make directory with right permissions
Once you have the server install, create folders that needs to be shared.
|
|
Step 3: Grant access to client machines
|
|
Add the following line to the file following the below structure:
|
|
eg:
|
|
The above line exposes the folder to all the clients in the network.
To explain the permissions part:
- rw: read/write (allowing the client to read and write)
- sync: requires changes to be written to the disk before applied
- no_subtree_check: eliminates subtree checking
- no_root_squash: Disable code preventing root user from writing to this folder
In most use cases the permissions (rw,sync,no_subtree_check) are used.
But since in this particular case we are mounting the NFS folder to docker container we need the no_root_squash. As containers boot into root/single user mode. So we will need to remove the check.
Once you added the file you need to export the nfs share directory.
|
|
You might have to restart the nfs service sudo systemctl restart nfs-kernel-server
Step 4: Allow NFS access throught Firewall
Incase you have a firewall isntalled as well you will need to open NFS port.
If you are using ufw this is the foolowing command.
|
|
Note: Default port for NFS is 2049
Step 5: Add the volume to your docker-compose.yml
Eg docker-compose.yml
file with NFS volume mounted.
|
|