Distributed Replicated Block Device (DRBD)

Distributed Replicated Block Device (DRBD) mirrors block devices between multiple hosts. The replication is transparent to other applications on the host systems. Any block device hard disks, partitions, RAID devices, logical volumes, etc can be mirrored.

To get started using drbd, first install the necessary packages. From a terminal enter:

sudo apt install drbd-utils

Note

If you are using the virtual kernel as part of a virtual machine you will need to manually compile the drbd module. It may be easier to install the linux-modules-extra-$(uname -r) package inside the virtual machine.

This section covers setting up a drbd to replicate a separate /srv partition, with an ext3 filesystem between two hosts. The partition size is not particularly relevant, but both partitions need to be the same size.

Configuration

The two hosts in this example will be called drbd01 and drbd02. They will need to have name resolution configured either through DNS or the /etc/hosts file. See DNS for details.

  • To configure drbd, on the first host edit /etc/drbd.conf:

    global { usage-count no; }
    common { syncer { rate 100M; } }
    resource r0 {
            protocol C;
            startup {
                    wfc-timeout  15;
                    degr-wfc-timeout 60;
            }
            net {
                    cram-hmac-alg sha1;
                    shared-secret "secret";
            }
            on drbd01 {
                    device /dev/drbd0;
                    disk /dev/sdb1;
                    address 192.168.0.1:7788;
                    meta-disk internal;
            }
            on drbd02 {
                    device /dev/drbd0;
                    disk /dev/sdb1;
                    address 192.168.0.2:7788;
                    meta-disk internal;
            }
    } 
    

    Note

    There are many other options in /etc/drbd.conf, but for this example their default values are fine.

  • Now copy /etc/drbd.conf to the second host:

    scp /etc/drbd.conf drbd02:~
    
  • And, on drbd02 move the file to /etc:

    sudo mv drbd.conf /etc/
    
  • Now using the drbdadm utility initialize the meta data storage. On each server execute:

    sudo drbdadm create-md r0
    
  • Next, on both hosts, start the drbd daemon:

    sudo systemctl start drbd.service
    
  • On the drbd01, or whichever host you wish to be the primary, enter the following:

    sudo drbdadm -- --overwrite-data-of-peer primary all
    
  • After executing the above command, the data will start syncing with the secondary host. To watch the progress, on drbd02 enter the following:

    watch -n1 cat /proc/drbd
    

    To stop watching the output press Ctrl+c.

  • Finally, add a filesystem to /dev/drbd0 and mount it:

    sudo mkfs.ext3 /dev/drbd0
    sudo mount /dev/drbd0 /srv
    

Testing

To test that the data is actually syncing between the hosts copy some files on the drbd01, the primary, to /srv:

sudo cp -r /etc/default /srv

Next, unmount /srv:

sudo umount /srv

Demote the primary server to the secondary role:

sudo drbdadm secondary r0

Now on the secondary server promote it to the primary role:

sudo drbdadm primary r0

Lastly, mount the partition:

sudo mount /dev/drbd0 /srv

Using ls you should see /srv/default copied from the former primary host drbd01.

References

1 Like

The drbd8-utils package was replaced by drbd-utils in Xenial and later. Releases starting with Disco no longer have the dummy transitional package drbd8-utils.

As such, I believe it would be best to adopt the new name :wink:

$ rmadison drbd8-utils drbd-utils
drbd8-utils | 2:8.3.11-0ubuntu1 | precise | amd64, armel, armhf, i386, powerpc
drbd8-utils | 2:8.4.3-0ubuntu0.12.04.2 | precise-updates | amd64, armel, armhf, i386, powerpc
drbd8-utils | 2:8.4.4-1ubuntu1 | trusty | amd64, arm64, armhf, i386, powerpc, ppc64el
drbd8-utils | 2:8.9.6-1 | xenial/universe | amd64, arm64, armhf, i386, powerpc, ppc64el, s390x
drbd8-utils | 2:8.9.10-2 | bionic/universe | amd64, arm64, armhf, i386, ppc64el, s390x
drbd8-utils | 2:8.9.10-2ubuntu0.1 | bionic-updates/universe | amd64, arm64, armhf, i386, ppc64el, s390x
drbd-utils | 8.9.6-1 | xenial | source, amd64, arm64, armhf, i386, powerpc, ppc64el, s390x
drbd-utils | 8.9.10-2 | bionic | source, amd64, arm64, armhf, i386, ppc64el, s390x
drbd-utils | 8.9.10-2ubuntu0.1 | bionic-updates | source, amd64, arm64, armhf, i386, ppc64el, s390x
drbd-utils | 9.5.0-1 | disco | source, amd64, arm64, armhf, i386, ppc64el, s390x
drbd-utils | 9.5.0-1 | eoan | source, amd64, arm64, armhf, i386, ppc64el, s390x
drbd-utils | 9.11.0-1build1 | focal | source, amd64, arm64, armhf, ppc64el, riscv64, s390x
drbd-utils | 9.11.0-1build1 | groovy | source, amd64, arm64, armhf, ppc64el, riscv64, s390x

1 Like

Thanks for the commend @sdeziel. We are indeed going through reviews for all Ubuntu Server Guide and I’ll update this particular documentation anytime soon. Thanks for your contribution.

I updated the package name from drbd8-utils to drbd-utils.

There seems to be a missing hyperlink, or wrong description of the link at least.
" See ??? for details."

Couldn’t find “linux-server” package as mentioned under the “notes”, however found the drbd.ko in the linux-modules-extra package e.g. linux-modules-extra-`uname -r`

Thanks, @camille.rodriguez. I fixed the broken link now.

Thanks, @janakawicks. Fixed.