Rancher is great for spinning up a number of services in docker containers, from Tomcat web applications to WordPress and Ghost installations. This is all good and well, but if you're familiar with Docker you'll know that Docker containers without volumes are a recipe for disaster. Data persistence is of utmost importance, and to enable persistance for containers in Rancher this tutorial will explain how. We will setup an NFS server and link this up with the "Rancher NFS" storage driver.
The first step is to install the NFS server locally, or within a VM. I'd strongly suggest not installing an NFS server inside a docker container without a volume for obvious reasons 🙂
sudo apt-get update sudo apt-get install nfs-kernel-server
We will now create a shared directory into which all data will be saved from Rancher:
sudo mkdir /nfs sudo chown nobody:nogroup /nfs
The next step is to let the NFS server know about the nfs folder created above:
sudo nano /etc/exports
Paste the following in the file:
Restart the NFS server with the following command:
sudo systemctl restart nfs-kernel-server
Now for linking Rancher NFS up with the installed NFS server. In Rancher click on Stacks -> Infrastructure. Next click on "Add from Catalog" as seen below:
Find the Rancher NFS catalog item and click on View Details:
See the environment details I inserted to link up with the NFS server:
MOUNT_DIR = /nfs MOUNT_OPTS = nfsvers=4 NFS_SERVER = 192.168.100.13 ON_REMOVE = purge RANCHER_DEBUG = false
Note the following:
MOUNT_DIR – this corresponds to the /nfs directory inserted in the exports file above.
MOUNT_OPTS – we explicitly specify NFS version 4 as this works nicely with Rancher.
NFS_SERVER – This is set to the IP address of the VM or machine on which you installed NFS
Rancher NFS is now successfully configured and the final step to creating a container with persistent storage is to specify details in the volume tab as shown below:
As an example we used MySQL and mapped "somename" to /var/lib/mysql. The other important part is the storage driver which is rancher-nfs
You can now click on Infrastructure -> Storage, and if everything has been successfully configured, you will now see "somename" with an active stated listed under volumes as seen below: