Stack Guides & Tips

Adding shared disk storage to a WordPress cluster

Published · Last updated

If you’ve been following my WordPress cluster guides, you’ll know that I use GlusterFS or Unison to handle file replication between nodes in the cluster. If you’re just getting started, my guide to setting up a WordPress cluster for huge sites covers the full build.

This works well when there are not too many files per directory. But if you have thousands upon thousands of images in your uploads folder then GlusterFS can get a bit slow. This is because of the technique it’s using to check if the file has been changed on any of the other nodes. Switching to Unison instead of GlusterFS helps, but a very large uploads folder is still the hardest thing to replicate.

Performance Tip

File replication tools have to compare every file on every node. Once your uploads folder holds tens of thousands of images, that comparison dominates. Moving just wp-content/uploads onto a single shared NFS volume takes those files out of the replication loop entirely.

Anyway, the ideal situation would be for it to be possible to mount a single block-storage volume from all 3 of your nodes at once, but unfortunately that’s not possible with a standard Digital Ocean volume. What you can do is to create yourself a file storage droplet and attach a huge volume to it, then use NFS to access that single volume from the cluster.

There are options for how to go about using this knowledge with a WordPress cluster. For example, we could stick the entire WordPress folder in this single location, but then if the volume is down, all your nodes will be down.

The approach I’m taking in this article is to put the wp-content/uploads folder onto the shared volume over NFS. Then, if for some reason the single-point-of-failure of this shared storage volume is down, it’s only images that are affected, and hopefully for the duration they will be largely unaffected as we’ll have a CDN in front of our cluster serving up this static content. If you haven’t set one up yet, see my guide to configuring Cloudflare for WordPress and WooCommerce.

Here’s a link to Digital Ocean’s guide to setting up NFS: https://www.digitalocean.com/community/tutorials/how-to-set-up-an-nfs-mount-on-ubuntu-22-04

Create a new droplet – I chose a 2GB 2CPU droplet – add private networking, and add a volume of your choice.

Once the droplet has been created, log onto it over SSH and then run the commands provided by Digital Ocean.

They make it easy to click to copy the commands, then you can right-click in your terminal window to paste. This will configure and format your new massive storage volume.

Once that is done, you’re ready to configure NFS.

Configure your file server

On the new storage volume droplet – the host – run the following:

apt-get update
apt-get install nfs-kernel-server
mkdir /var/largestorage -p
chown www-data:www-data /var/largestorage/
systemctl restart nfs-kernel-server

Edit the /etc/exports file to configure remote access to your share.

vi /etc/exports

In my example, I’ve added the following line:

/var/largestorage 10.138.104.226(rw,no_root_squash,no_subtree_check) 10.138.232.3(rw,no_root_squash,no_subtree_check) 10.138.232.6(rw,no_root_squash,no_subtree_check)

Configure your nodes to access the file server

On your cluster nodes – the clients – run the following:

apt-get install nfs-common

Now in my example, I’m replacing the uploads folder, so I’ve temporarily moved the contents of that elsewhere. You still need a folder on the client for the mount point however, so ensure that your uploads folder exists and is empty. Then run the following:

mount 10.138.148.38:/var/largestorage /var/largestorage

Once you’ve run the 2 commands above on all your nodes, they will all have access to the same storage folder for uploads on your large volume droplet.

Configure everything to mount when you reboot

vi /etc/fstab

Add this line for the NFS share.

10.138.148.38:/var/largestorage /var/www/wpicluster/wp-content/uploads nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0

Create a symbolic link to the newly mounted folder. Providing you have the storage mount on each device, when Unison or GlusterFS replicates the symbolic link it will still work on that new node.

ln -s /var/www/wpicluster/wp-content/uploads /var/localstorage
Note

The NFS volume is a single point of failure for your images. Keep a CDN in front of the cluster so that if the volume ever goes down, previously served images stay cached and visitors are largely unaffected until it comes back.

Let me know any questions.

Further reading

The full cluster build this storage guide extends
Faster file replication when uploads folders grow huge
Keep your running cluster healthy day to day

3 comments

  1. Gane

    Hi Dave,

    I'm thankful and I appreciate the blog post, but i'm very much confused on these items.. request your inputs..

    1) Should i choose automatic mount (or) should i choose manual mount in the DO control panel?
    2) I'm guess i should not follow the tutorial at DO? (you've put it only as reference?)
    3) I have 3 Nodes (+1 Load balancer). Can i setup volume on the Node 1 and then mount it to node 2 and 3? (or) should i explicitly create another droplet only for this volume?

    I have tried to create volume on the Node 1 and then changing the steps above accordingly, but the part where "Node 1 should be an NFS server as well as NFS common" confuses me.. Please throw some light on this..

    Thanks & Regards,
    Gane

    1. Dave H. Customer

      1) Automatic
      2) No, follow my guide
      3) If you mean a storage volume, Digital Ocean do not yet allow you to connect them to more than one node. This would be great - like a storage area network (SAN).

      If you need individual help, get in touch through our on-site chat system.

      1. Gane

        Thanks Dave, appreciate your timely reply. I read about your website being hacked, hope it doesn't happen again - you and your work is a boon for the internet. God bless!

        Please note, I had to execute below line on my NFS server droplet before doing "mount" on client nodes.

        %exportfs -a

        See article: https://unix.stackexchange.com/questions/106122/mount-nfs-access-denied-by-server-while-mounting-on-ubuntu-machines

        Thanks
        Gane (Your fan from India)

▲
▼
1/1