How to Set Up DRBD and Keepalived for Real‑Time File Sync and Failover
This guide walks you through installing and configuring DRBD and keepalived on two Linux nodes to achieve real‑time block‑level file synchronization, automatic primary/secondary role switching, and high‑availability failover for services such as PostgreSQL, including troubleshooting common issues like split‑brain and busy mounts.
Install DRBD and keepalived for Real‑Time File Synchronization and Dual‑Node Failover
System Initialization
Ensure a data disk or additional partition is available. yum update -y Disable the firewall:
systemctl stop firewalld
systemctl disable firewalldEdit /etc/hosts to add the two nodes:
192.168.1.240 Primary kylin-01
192.168.1.241 Secondary kylin-02Disable SELinux:
vim /etc/sysconfig/selinux
# set SELINUX=disabledInstall required packages:
yum install gcc libxslt-devel libxslt perl keyutils-libs-devel net-tools -yDownload and Compile DRBD
Download and build the DRBD kernel module:
wget https://pkg.linbit.com//downloads/drbd/9/drbd-9.2.8.tar.gz
tar -zxvf drbd-9.2.8.tar.gz
cd drbd-9.2.8
make && make installDownload and install drbd‑utils:
wget https://pkg.linbit.com//downloads/drbd/utils/drbd-utils-9.27.0.tar.gz
tar -zxvf drbd-utils-9.27.0.tar.gz
cd drbd-utils-9.27.0
./configure --prefix=/usr/local/drbd --without-83support --with-udev --with-initscripttype=systemd --without-manual
make && make installInstalled binaries are located in /usr/sbin/drbdsetup, /usr/sbin/drbdmeta, and /usr/sbin/drbdadm. Configuration files reside in /usr/local/drbd/etc/drbd.d.
Configure DRBD
Create a global configuration file global_common.conf (example content omitted for brevity).
Create a resource definition /usr/local/drbd/etc/drbd.d/drbd.res on both nodes:
resource r1 {
protocol C;
on kylin-01 {
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.1.240:7789;
meta-disk internal;
}
on kylin-02 {
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.1.241:7789;
meta-disk internal;
}
}Initialize and Start DRBD
# Create metadata
drbdadm create-md r1
# Start the service on both nodes
systemctl start drbd
systemctl enable drbd
# Verify the service is listening
netstat -anp | grep 7789Check the role of each node with drbdadm role r1. The first node will be Secondary by default.
Promote a node to primary:
drbdadm primary --force r1
drbdadm status r1After synchronization, the status will show role:Primary and disk:UpToDate.
Test DRBD Synchronization
Format the DRBD device and mount it on the primary node:
mkfs.ext4 /dev/drbd0
mkdir /data
mount /dev/drbd0 /dataCreate a test file, unmount, switch roles, and verify the file appears on the secondary node after it becomes primary.
Handling Common Issues
If umount reports “device is busy”, find the holding process with fuser -m /data and kill it.
In a split‑brain scenario, follow the official DRBD manual to discard one side’s data and reconnect:
# On the secondary node
drbdadm secondary r1
drbdadm disconnect all
drbdadm --discard-my-data connect r1
# On the primary node
drbdadm disconnect all
drbdadm connect r1
drbdsetup /dev/drbd0 primaryInstall and Configure keepalived
Install keepalived via yum: yum install -y keepalived Configure a virtual IP (e.g., 192.168.1.239) and set authentication. The master node’s notify_master script promotes DRBD to primary, mounts the filesystem, and starts the PostgreSQL container. The backup node’s notify_backup and notify_stop scripts stop the container, unmount the filesystem, and demote DRBD.
Enable and start keepalived on both nodes:
systemctl enable keepalived
systemctl start keepalivedVerify failover by creating or deleting data in the PostgreSQL container, then switching the virtual IP between nodes; the data should remain synchronized via DRBD.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
