How to Mirror a Filesystem between Servers with DRBD On Debian or Ubuntu
Something that is often overlooked is just how important your server deployment may be to your business. The harsh truth is that downtime can be expensive as visitors to your site may simply skip over your non-responsive website to buy elsewhere. While a good backup strategy may defend you from data loss and enable you to get everything back up and running after a disaster, you can still end up with an extended period of downtime to cope with. The solution to this is to have a second server setup and ready to take over should the main server fail, and fortunately this can be relatively simple to configure.
Here, we’ll be looking at how you can use Distributed Replicated Block Device (DRBD) to replicate a filesystem from one server to another. DRBD works on the block level rather than the file level, so only transmits the actual data on the disk that changes. You can think of it somewhat like a RAID 1 configuration with the disks mirrored across two servers. It allows you to replicate individual partitions, entire disk drives or even RAID devices between multiple servers meaning it can be somewhat flexible for fitting into your use case.
Here we will be showing you how to set up DRBD to duplicate a partition on a Debian/Ubuntu server to another one. We’ll be making a few assumptions here, first that you have already created equally sized partitions on each server to be replicated, our primary server will be called server1 and the secondary server2.
The first thing we’ll need to do is to install DRBD to both machines. Since it is in the repositories, this is a simple process:
sudo apt-get update
sudo apt-get install drbd8-utils
Configuration is taken care of in the /etc/drbd.d/global_common.conf file. This is broken down into a number of sections with most of the default settings commented out. For the purposes of our example here, this should be fine.
Edit the file on each server and make the following change, in the common section add this to the end:
syncer {
rate 100M;
}
Now save and exit the file. The next step is to create the resource file for the partition we’ll be sharing. You can call this file whatever feels relevant to your usage, as we are duplicating sdb1 here we’ll call it sdb1.res
sudo nano /etc/drbd.d/sdb1.res
In this file, paste the following instructions:
resource r0 {
protocol C;
startup {
wfc-timeout 15;
degr-wfc-timeout 60;
}
net {
cram-hmac-alg sha1;
shared-secret “secret”;
}
on server1 {
device /dev/drbd0;
disk /dev/sdb1;
address 10.0.2.14:7788;
meta-disk internal;
}
on server2 {
device /dev/drbd0;
disk /dev/sdb1;
address 10.0.2.15:7788;
meta-disk internal;
}
}
Note that you’ll need to change the disk setting to match the partition that you are replicating on your servers. The names of the servers given in the “on serverX” section need to match the hostnames configured on the server. You also need to update the IP addresses to match the IPs in use on the servers. Save and exit the file. Again you want this same file on both servers.
With this done, the next step is to create the device metadata for the partition. This needs to be done on both servers, and is done with the following command:
sudo drbdadm create-md r0
Once this has been done on each, you can then start the drbd service on each server:
sudo systemctl start drbd.service
Now on server1, we need to tell the server to be our primary data source and sync over to server2. You only run this command on ONE of the servers.
sudo drbdadm — –overwrite-data-of-peer primary all
This will copy the data over to the other server. This may take some time to complete. Progress can be seen by viewing the following file on either server:
sudo cat /proc/drbd
Once the sync has completed, you can go ahead and create a file system on the partition on the primary server and then mount and use it.
Note that to mount and use the partition on the secondary server, you’ll need to either stop drdb on the servers to prevent your changes being overwritten, or use drdbadm to make the server1 the secondary and server2 the primary. To do so, first unmount your partition on server run, and then enter the following command:
sudo drbdadm secondary r0
Then on server2 run:
sudo drbdadm primary r0
After which the partition can then be mounted and used on server2 instead.