← All Articles

Shared filesystem between servers using NFS

A while ago I needed to share the same copy of some large files between several server instances. It needed to be the same actual file, and synced instantly - ruling out the use of hacky solutions like rsync'ing files via a cron job etc. 

It turns out this isn't too difficult to do with NFS, which lets you nominate a folder on a master server which can be accessed by one (or more) client servers. 

Here's how to do this, using CentOS servers:


# Set NFS file sharing between servers (Run on master server)
yum install nfs-utils nfs-utils-lib
chkconfig nfs on

# Edit /etc/exports to enable the /var/www/mydomain/shared folder to be shared with client servers at these IP addresses
/var/www/mydomain/shared 103.4.123.198(rw,sync,no_root_squash,no_subtree_check)
/var/www/mydomain/shared 103.4.123.199(rw,sync,no_root_squash,no_subtree_check)

# edit /etc/sysconfig/nfs and uncomment the following lines
RQUOTAD_PORT=875
LOCKD_TCPPORT=32803
LOCKD_UDPPORT=32769
MOUNTD_PORT=892
STATD_PORT=662
STATD_OUTGOING_PORT=2020

# Setup NFS on client server
yum install nfs-utils nfs-utils-lib

/etc/init.d/nfs start
chkconfig nfs on

mkdir -p /path/to/where/to/mount
mount -t nfs 103.4.123.456:/var/www/mydomain/shared/ /path/to/where/to/mount/

# add the following lines to /etc/fstab (client server only) to automount every reboot
103.4.123.456:/var/www/mydomain/shared nfsrw,sync,hard,intr 0 0

 

Made with JoyBird