Have you ever wanted to scale your web operations to multiple web servers and still needed a simple common place to share data between servers?
What to do with User Generated Content?
When we want to store user generated content such images, documents or video that are uploaded by them from time to time we have three options:
- Push them as BLOBs into the database (or your NoSQL system such as MongoDB).
- Store them on disk that is accessible by several servers.
- Store them on a "File Server as a Service" like Amazon S3.
This time we'll focus on the second option:
DIY with Network Services
The basic solution for DIY is installing a file sharing daemon on one of your servers and share a folder of it to the other web servers.
There are three common file sharing protocols that may be used:
- SAMBA (that is common and supported by Windows as well).
- NFS (supported only on *NIX machines).
- CIFS that mostly used in Windows, but can be mounted to LINUX machines as well.
In this case I decided to focus on the NFS implementation, so stay tuned:
First configure the NFS server
- Install the NFS RPM: yum -y install nfs-utils
- Start service: /etc/init.d/nfs start
- Open the relevant ports in the iptables FW (2049 and 111):
- iptables -I INPUT -p tcp -s 192.168.85.0/24 -m state --state NEW,RELATED,ESTABLISHED --dport 2049 -j ACCEPT
- iptables -I INPUT -p udp -s 192.168.85.0/24 -m state --state NEW,RELATED,ESTABLISHED --dport 2049 -j ACCEPT
- iptables -I INPUT -p tcp -s 192.168.85.0/24 -m state --state NEW,RELATED,ESTABLISHED --dport 111 -j ACCEPT
- iptables -I INPUT -p udp -s 192.168.85.0/24 -m state --state NEW,RELATED,ESTABLISHED --dport 111 -j ACCEPT
- Configure exported locations by editing /etc/exports according to the following examples:
- Provide every server behind the firewall R/W to this folder: /path/to/directory *(rw)
- Please notice that in this case you should provide the relevent permissions on disk (since the guest machine will use in default the nobody user to access this disk). For example: chmod -R 777 /path/to/directory
- Provide a single server read only permission to this folder: /path/to/directory 192.168.2.21(ro)
- Finally load these exported locations: /usr/sbin/exportfs -a
- Create local directory: mkdir /local/directory
- Add line to /etc/fstab
directory /local/directory nfs
- Mount the folder: mount /local/directory
Scaling you system is possible with few simple steps, this was one of them
P.S In the DevOps world, scripts is everything. So you may use the following:
yum -y install nfs-utils
chmod -R 777 /path/to/directoryecho '/path/to/directory *(rw)' >> /etc/exports
echo "$1:/path/to/directory /path/to/directory nfs" >> /etc/fstab