Computing Magazine

How to Setup Nfs Mount in Linux

Posted on the 23 November 2015 by Abhishek Somani @somaniabhi
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:
Lets Configure

NFS-Master :
NFS - Client :
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

then execute :

exportfs -a
service nfs restart
CLIENT SETUP : SSH to server 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 /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% / 20G 785M 18G 5% /home/rails/upload_dir

nano /etc/fstab
Add the following line: /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 [email protected] or message him on his linkedIn profile . HOW TO SETUP NFS MOUNT IN LINUX

Back to Featured Articles on Logo Paperblog