How to setup DRBD on Ubuntu 22.04

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"

In this article I will help you how to set DRBD on Ubuntu Server 22.04. DRBD is a Distributed and Replicated* storage system wich can be used for high availability computer clusters or can be used to create larger storage pools defined by software.

  • *I’m not sure what the B and D stands for, I think is Block Device

First step: install the required software

Which in ubuntu means install drbd-utils with apt:

sudo apt-get install drbd-utils
drbd on ubuntu. Install the necessary packages
if you are not paying attention to the error in this screenshot: don’t forget to become root with su or sudo

Apt-get (or apt install) will take care of install the dependencies. For example, this was a brand new Virtual Machine that I’ve created only to learn how to setup DRBD on Ubuntu and dependencies included some mta, Postfix in this case.

preconfiguring dependencies
Anyway, follow the instructions on screen

Second step: install the required software

Wait… what?

I mean, install the required software on the second server. As this is a distributed replicated storage system, we need at least two nodes, so become root in the second one and install drbd-utils.

Alternatively, you can install just one of them and after drbd-utils installation clone the system disk (or clone the VM if you are with virtual machines)

drbd on ubuntu. Install the necessary packages
installing drbd-utils on ubuntu

Third step: create a configuration file

Before this step: you should save a partition or a whole disk (much better) to be the shared resource. For this example I’m working with /dev/vda4 (the last partition of the only disk of my virtual machines).

Create /etc/drbd.conf file with the following content:

global { usage-count no; }
common { syncer { rate 100M; } }
resource r0 { #resource name
	protocol C;
	startup {
		wfc-timeout 15;
		degr-wfc-timeout 60;
	net {
		cram-hmac-alg sha1;
		shared-secret "NotSoSecret";
	on gonzbuntu-a { #one server
		device /dev/drbd0;
		disk /dev/vda4; #shared partition
		meta-disk internal;
	on gonzbuntu-b { the other server
                device /dev/drbd0;
                disk /dev/vda4; #shared partition
                meta-disk internal;

Our shared resource is called (not imaginatively) r0.

drbd.conf config file

This is just a demo. You may want to have several shared resources and cleaner configuration files. You can create a file per resource and store in separate files in /etc/drbd.d/<resourcename>.res

The resource configuration is the same on both nodes, so we can just copy the config file to the other server with scp:

scp /etc/drbd.conf root@server2:/etc/drbd.conf
drbd on ubuntu. Copying the configuration
you can also make sure that both servers see each other by adding them to /etc/hosts

Fourth step: create device metadata

This step must be completed only on the initial device creation, on both hosts:

drbdadm create-md <resource>

Then we can start, also on both hosts, the drbd service:

systemctl start drbd
systemctl status drbd
drbd on ubuntu. Starting the service
starting the drbd service

Fifth step: start synchronization

On the master host run the following command:

drbdadm -- --overwrite-data-of-peer primary all

At this point (we are just starting with drbd on ubuntu) any of them can be the primary, just choose the one you like the more. You can check how the synchro goes on the secondary by running:

watch -n1 cat /proc/drbd
cheking drbd synchronization status
and you’ll see a nice screen like this one

Sixth step: add a filesystem

Now we can create a filesystem and mount into some directory. But first make the drbd service starts on every reboot:

systemctl enable drbd.service
mkfs.ext4 /dev/drbd0
mount /dev/drbd0 /mnt/drb/

Seventh step: test

Let’s create a test file on the master and then transform this host on the secondary one:

dd if=/dev/zero of=/mnt/drb/filename.ext bs=1M count=1024
umount /mnt/drb
drbdadm secondary r0
drbdadm status r0

Before we work on the master-to-be host, this commands should look like this:

Starting tests
try not to include my typos 😀

On the second host you can check first if both hosts are with the secondary role and then promote to primary:

drbdadm status r0
drbdadm primary r0

And now you can mount the drb device and check if the file created on the first host it’s here:

mount /dev/drb0 /mnt/drb
ls -lh /mnt/drb/filename.ext
drbd it's alive!
the file is also on the other host.

And this is it, t least for learning purposes, how to setup DRBD on Ubuntu server ;). If you are working with Centos 8, we already had a drbd tutorial right here.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"
Gonzalo Rivero
Gonzalo Rivero
I am Gonzalo, I live in Salta, a city located in the NW of Argentina. I play the guitar and a little harmonica. I also like to bike.


Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook