NFS mount is used to share a directory between several servers, It saves the disk Space and is Useful for a Infrastructure where a application shares a common directory and is hosted in multiple instances. Lets think we have a Rails application where we upload a csv file and save it a directory in server with file name to DB . On the other hand a Rake Task/ Cron Job runs every two minutes that polls DB for file name and proccess the CSV file to get the desired response. It works fine when we have a single instance hosted in server, think if we have two instances of Rails in different Server and is managed by a Load balancer to server request. When a File Upload Request comes to ELB , it directs to instance1 with file1 and the second request to instance 2 with file2, The cron job in instance 2 picks the file name file1 from DB and doesnot found any matching file and Marks FILE NOT FOUND IN DB , where as file is present in instance1. To avoid this type of issues we can use S3 which we can get py paying few amount, the Opensource way is Using NFS Mount, Lets Check how to Configure NFS. Lets Say we have two instances with IP:
10.23.225.16
10.23.225.17
Lets Configure
NFS-Master : 10.23.225.16
NFS - Client : 10.23.225.17
MASTER SETUP :Execute the following Commands:
sudo su
yum install nfs-utils nfs-utils-lib
Startup Scripts :
chkconfig nfs on
service rpcbind start
service nfs start
EXPORT the Shared Directory :Suppose we want the directory
/home/rails/upload_dir , to be shared between the instances.Open
vi /etc/exportsAdd the following lines at EOF
/home/rails/upload_dir 10.23.225.17(rw,sync)
then execute :
exportfs -a
service nfs restart
CLIENT SETUP : SSH to server 10.23.225.17 Execute following commands:
yum install nfs-utils nfs-utils-lib
#Assuming the upload_dir path is same as instance1
mkdir -p /home/rails/upload_dir
mount 10.23.225.26:/home/rails/upload_dir /home/rails/upload_dir
You Can verify it by typing:
df –h
Filesystem Size Used Avail Use% Mounted on /dev/sda 20G 783M 18G 5% / 10.23.225.16:/home/rails/upload_dir 20G 785M 18G 5% /home/rails/upload_dir
TO ENSURE IT IS ALWAYS MOUNTED MAKE CHANGES IN FS TAB:
nano /etc/fstab
Add the following line:
10.23.225.16:/home/rails/upload_dir /home/rails/upload_dir nfs defaults,_nfs_shared 0 0
Try creating a file in instace1 of shared dir and check it should be populated in instance2 of shared dir(/home/rails/upload_dir) Keep in Mind Once the NFS Mount is unmounted all the data will be lost from the mounted instance , but data will be safe in Master instance.This post is created by
Santosh Mohanty . You can contact him for any questions at
santa.jyp@gmail.com or message him on his
linkedIn profile .