You inherited a small fleet that uses a single Ubuntu VM as the source of truth for a vendor app’s /var/lib/appdata directory. The VM is on a SAN with hourly snapshots, which the team has been comfortable calling “the backup story.” Then the hypervisor underneath that VM has a brief PSU event during a Tuesday afternoon, the disk takes ninety seconds to come back, and the application spends the next hour replaying a half-corrupted SQLite file. The post-mortem assumes that “we should put it on a cluster” — but the application’s vendor doesn’t support a clustered filesystem and won’t certify replicated databases. What you actually need is a primary/secondary block-level mirror at the kernel layer, with a cluster manager that promotes the secondary, moves a virtual IP, and remounts the filesystem when the primary disappears. That stack is DRBD 9 + Pacemaker + Corosync, and it has been running this exact pattern on Linux for two decades.
What’s happening
DRBD (Distributed Replicated Block Device) is a kernel module that sits below the filesystem and replicates writes from a primary node to a secondary node synchronously over the network. To the application, /dev/drbd0 looks like a regular block device — you put ext4 or xfs on it, mount it, and write. On the secondary, the block device is identical byte-for-byte, but it is not mounted; the kernel refuses to expose two writers to the same blocks because that path leads to silent corruption. To actually fail over, something has to detect the primary is gone, demote it (if reachable), promote the secondary, and mount the filesystem on the new primary along with any service IPs the application clients expect. That “something” is the cluster manager: Corosync handles the membership/heartbeat layer; Pacemaker handles the resources (DRBD master/slave, filesystem, virtual IP) and the constraint that they must all live on the same node. STONITH (Shoot The Other Node In The Head) is the missing third leg — without a fencing mechanism, a network partition turns into split-brain and you spend a weekend reconciling two divergent copies. On a two-node lab cluster you can get away with no-quorum-policy=ignore and a witness disk; on a real production deployment you set up IPMI or hypervisor-API fencing and you test it before you trust it.
How to verify
Block-level mirror status from either node:
drbdadm status appdata
# appdata role:Primary
# disk:UpToDate
# peer role:Secondary
# replication:Established peer-disk:UpToDate
Both sides show UpToDate and Established once the initial sync completes. Anything else — Inconsistent, Outdated, SyncTarget — is a state you have to understand before you trust failover.
Cluster state via Pacemaker:
crm_mon -1 -A
# Cluster Summary:
# Stack: corosync
# Current DC: drbd-01 (version 2.x) - partition with quorum
# Node List:
# Online: [ drbd-01 drbd-02 ]
# Full List of Resources:
# Clone Set: ms_drbd_appdata [drbd_appdata] (promotable)
# Masters: [ drbd-01 ]
# Slaves: [ drbd-02 ]
# fs_appdata (ocf::heartbeat:Filesystem): Started drbd-01
# vip_appdata (ocf::heartbeat:IPaddr2): Started drbd-01
A controlled failover drill, the most important test you will run on this cluster:
# From the primary node
crm node standby drbd-01
# Watch crm_mon on the other terminal; vip + filesystem should move to drbd-02 in <10s
crm node online drbd-01
If the standby dance does not move the resources within ten seconds, do not put this in front of production traffic.
The fix
Save the script as /root/setup-drbd-node.sh and run it on each of the two nodes after editing the variables at the top. It performs the full install on Ubuntu 22.04: base hardening, kernel/sysctl tuning for DRBD throughput, DRBD 9 from the LINBIT-compatible Ubuntu packages, Corosync 3.x + Pacemaker 2.x, the DRBD resource definition for a 100GB volume on /dev/sdb, an ext4 filesystem on /dev/drbd0 mounted at /srv/appdata, a virtual IP managed as a Pacemaker resource, ordering and colocation constraints so the trio always moves together, and a controlled-failover drill at the bottom.
#!/usr/bin/env bash
set -euo pipefail
### Ubuntu 22.04 DRBD 9 + Pacemaker + Corosync two-node HA installer
### Run on BOTH nodes after editing the variables below
### Run on drbd-01 FIRST (force initial sync), then drbd-02
### Assumes /dev/sdb is the dedicated replication device on both nodes (same size!)
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
# === EDIT PER DEPLOYMENT ===
NODE_NAME="drbd-01" # drbd-01 or drbd-02
NODE_IP="10.20.0.11" # this node's replication-network IP
PEER_NAME="drbd-02"
PEER_IP="10.20.0.12"
VIP="10.20.0.10" # virtual IP that follows the primary
VIP_CIDR="24"
REPLICATION_NIC="ens4" # dedicated NIC for DRBD traffic
RESOURCE_NAME="appdata"
BACKING_DEVICE="/dev/sdb"
MOUNT_POINT="/srv/appdata"
CLUSTER_NAME="appdata-ha"
HACLUSTER_PASSWORD="CHANGE_ME_HACLUSTER"
# ===========================
echo
echo "========================================"
echo "DRBD node ${NODE_NAME} (${NODE_IP})"
echo "========================================"
echo
echo "==> Updating base OS"
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get -y dist-upgrade
apt-get -y install curl wget jq lsof tcpdump iproute2 chrony ufw fail2ban \
unattended-upgrades parted xfsprogs e2fsprogs lvm2 \
python3-pip net-tools dnsutils
systemctl enable --now chrony
echo "==> Kernel/sysctl tuning for DRBD throughput"
cat > /etc/sysctl.d/99-drbd.conf <<'SYSCTL'
# Network buffers — DRBD replication can saturate 1Gbps quickly
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.rmem_default = 524288
net.core.wmem_default = 524288
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.core.netdev_max_backlog = 30000
# Don't aggressively swap; we want page cache for the FS on top of DRBD
vm.swappiness = 5
# Don't drop dirty data on the floor; force ordered flushes
vm.dirty_background_ratio = 5
vm.dirty_ratio = 15
SYSCTL
sysctl --system
echo "==> /etc/hosts (avoid DNS dependency for cluster membership)"
if ! grep -q "${PEER_IP}\s*${PEER_NAME}" /etc/hosts; then
echo "${PEER_IP} ${PEER_NAME}" >> /etc/hosts
fi
if ! grep -q "${NODE_IP}\s*${NODE_NAME}" /etc/hosts; then
echo "${NODE_IP} ${NODE_NAME}" >> /etc/hosts
fi
hostnamectl set-hostname "${NODE_NAME}"
echo "==> Firewall — allow replication traffic and cluster comms between the two nodes only"
ufw --force reset
ufw default deny incoming
ufw default allow outgoing
ufw allow OpenSSH
ufw allow from "${PEER_IP}" to any port 7788:7799 proto tcp comment 'DRBD replication'
ufw allow from "${PEER_IP}" to any port 5404,5405 proto udp comment 'Corosync'
ufw allow from "${PEER_IP}" to any port 2224 proto tcp comment 'pcsd'
ufw allow from "${PEER_IP}" to any port 3121 proto tcp comment 'Pacemaker remote'
ufw allow from "${PEER_IP}" to any port 21064 proto tcp comment 'DLM'
ufw --force enable
echo "==> Hardening SSH"
sed -i 's|^[#[:space:]]*PermitRootLogin.*|PermitRootLogin prohibit-password|' /etc/ssh/sshd_config
sed -i 's|^[#[:space:]]*PasswordAuthentication.*|PasswordAuthentication no|' /etc/ssh/sshd_config
sshd -t && systemctl restart ssh
echo "==> Installing DRBD 9 utilities and kernel module"
apt-get -y install drbd-utils drbd-dkms
# Confirm kernel module loads
modprobe drbd
lsmod | grep -q drbd || { echo "DRBD module did not load"; exit 1; }
echo "==> Wiping any old DRBD metadata on the backing device"
if [[ ! -b "${BACKING_DEVICE}" ]]; then
echo "Backing device ${BACKING_DEVICE} not present — abort"
exit 1
fi
wipefs -a "${BACKING_DEVICE}" || true
echo "==> Writing DRBD global config"
cat > /etc/drbd.d/global_common.conf <<'GLOBAL'
global {
usage-count no;
udev-always-use-vnr;
}
common {
handlers {
# Don't auto-promote on a failed primary; let Pacemaker decide
fence-peer "/usr/lib/drbd/crm-fence-peer.9.sh";
after-resync-target "/usr/lib/drbd/crm-unfence-peer.9.sh";
split-brain "/usr/lib/drbd/notify-split-brain.sh root";
}
startup {
wfc-timeout 30;
degr-wfc-timeout 30;
}
options {
on-no-data-accessible io-error;
auto-promote no;
}
disk {
on-io-error detach;
disk-flushes yes;
md-flushes yes;
resync-rate 100M;
c-plan-ahead 20;
c-max-rate 200M;
c-min-rate 4M;
}
net {
protocol C;
max-buffers 36864;
sndbuf-size 1024k;
rcvbuf-size 2048k;
verify-alg sha256;
ping-int 5;
ping-timeout 30;
connect-int 10;
timeout 60;
# Asymmetric split-brain recovery — discard the secondary's changes by default
after-sb-0pri discard-zero-changes;
after-sb-1pri discard-secondary;
after-sb-2pri disconnect;
rr-conflict disconnect;
cram-hmac-alg sha256;
}
}
GLOBAL
echo "==> Writing DRBD resource: ${RESOURCE_NAME}"
cat > "/etc/drbd.d/${RESOURCE_NAME}.res" <<EOF
resource ${RESOURCE_NAME} {
device /dev/drbd0;
disk ${BACKING_DEVICE};
meta-disk internal;
net {
shared-secret "${HACLUSTER_PASSWORD}";
}
on ${NODE_NAME} {
node-id 0;
address ${NODE_IP}:7788;
}
on ${PEER_NAME} {
node-id 1;
address ${PEER_IP}:7788;
}
connection-mesh {
hosts ${NODE_NAME} ${PEER_NAME};
}
}
EOF
chmod 600 /etc/drbd.d/global_common.conf "/etc/drbd.d/${RESOURCE_NAME}.res"
echo "==> Creating DRBD metadata"
yes | drbdadm create-md "${RESOURCE_NAME}"
echo "==> Bringing the resource up (will sit in Inconsistent until peer joins)"
systemctl enable drbd
systemctl restart drbd
drbdadm up "${RESOURCE_NAME}"
echo "==> Installing Pacemaker + Corosync + pcs"
apt-get -y install pacemaker corosync pcs resource-agents
# Set hacluster password (pcs auth needs it)
echo "hacluster:${HACLUSTER_PASSWORD}" | chpasswd
systemctl enable --now pcsd
echo "==> Corosync skeleton config"
mkdir -p /etc/corosync
cat > /etc/corosync/corosync.conf <<EOF
totem {
version: 2
cluster_name: ${CLUSTER_NAME}
transport: knet
crypto_cipher: aes256
crypto_hash: sha256
}
nodelist {
node {
ring0_addr: ${NODE_IP}
name: ${NODE_NAME}
nodeid: 1
}
node {
ring0_addr: ${PEER_IP}
name: ${PEER_NAME}
nodeid: 2
}
}
quorum {
provider: corosync_votequorum
two_node: 1
wait_for_all: 0
}
logging {
to_logfile: yes
logfile: /var/log/corosync/corosync.log
to_syslog: no
timestamp: on
}
EOF
# Generate shared corosync auth key on drbd-01 only; copy to drbd-02 out of band
if [[ "${NODE_NAME}" == "drbd-01" ]]; then
if [[ ! -f /etc/corosync/authkey ]]; then
corosync-keygen -l
chmod 400 /etc/corosync/authkey
echo
echo "*** COPY /etc/corosync/authkey TO ${PEER_NAME}:/etc/corosync/authkey BEFORE ENABLING COROSYNC THERE ***"
echo
fi
fi
echo "==> Enabling Corosync + Pacemaker"
systemctl enable corosync pacemaker
# Don't start Pacemaker yet on drbd-02 if the authkey isn't there
if [[ -f /etc/corosync/authkey ]]; then
systemctl restart corosync
systemctl restart pacemaker
else
echo "==> /etc/corosync/authkey missing — copy from drbd-01 before starting Pacemaker"
fi
echo "==> Wait for cluster to settle (only meaningful once both nodes are up)"
for i in {1..30}; do
if crm_mon -1 >/dev/null 2>&1; then break; fi
sleep 2
done
echo
echo "========================================"
echo "BASE INSTALL DONE on ${NODE_NAME}"
echo "========================================"
echo
echo "Next steps (run ONCE, on drbd-01 only, after BOTH nodes finish base install):"
echo
echo " # 1. Authenticate pcs across both nodes"
echo " pcs host auth drbd-01 drbd-02 -u hacluster -p '${HACLUSTER_PASSWORD}'"
echo
echo " # 2. Force initial DRBD sync from drbd-01"
echo " drbdadm primary --force ${RESOURCE_NAME}"
echo " drbdadm status ${RESOURCE_NAME} # wait for UpToDate/UpToDate"
echo
echo " # 3. Create filesystem on /dev/drbd0 (only on the primary, once)"
echo " mkfs.ext4 -L ${RESOURCE_NAME} /dev/drbd0"
echo " mkdir -p ${MOUNT_POINT}"
echo
echo " # 4. Demote so Pacemaker can take over promotion"
echo " drbdadm secondary ${RESOURCE_NAME}"
echo
echo " # 5. Disable boot-time DRBD start; Pacemaker manages it now"
echo " systemctl disable drbd"
echo " systemctl stop drbd"
echo
echo " # 6. Define Pacemaker resources (run on drbd-01 only)"
echo
echo " pcs property set stonith-enabled=false # set to true and configure fencing for production"
echo " pcs property set no-quorum-policy=ignore"
echo " pcs resource defaults update resource-stickiness=200"
echo
echo " pcs resource create drbd_${RESOURCE_NAME} ocf:linbit:drbd \\"
echo " drbd_resource=${RESOURCE_NAME} \\"
echo " op monitor interval=29s role=Master \\"
echo " op monitor interval=31s role=Slave"
echo
echo " pcs resource promotable drbd_${RESOURCE_NAME} \\"
echo " master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true"
echo
echo " pcs resource create fs_${RESOURCE_NAME} ocf:heartbeat:Filesystem \\"
echo " device=/dev/drbd0 directory=${MOUNT_POINT} fstype=ext4 \\"
echo " options='noatime,nodiratime' \\"
echo " op monitor interval=20s timeout=40s"
echo
echo " pcs resource create vip_${RESOURCE_NAME} ocf:heartbeat:IPaddr2 \\"
echo " ip=${VIP} cidr_netmask=${VIP_CIDR} nic=${REPLICATION_NIC} \\"
echo " op monitor interval=10s"
echo
echo " # 7. Constraints — keep them on the same node, in the right order"
echo " pcs constraint colocation add fs_${RESOURCE_NAME} with master drbd_${RESOURCE_NAME}-clone INFINITY"
echo " pcs constraint colocation add vip_${RESOURCE_NAME} with fs_${RESOURCE_NAME} INFINITY"
echo " pcs constraint order promote drbd_${RESOURCE_NAME}-clone then start fs_${RESOURCE_NAME}"
echo " pcs constraint order start fs_${RESOURCE_NAME} then start vip_${RESOURCE_NAME}"
echo
echo " # 8. Smoke test"
echo " crm_mon -1"
echo " pcs node standby drbd-01 # vip + fs move to drbd-02"
echo " pcs node unstandby drbd-01"
After both nodes finish the base install, run the post-install block printed at the bottom of the script — exactly once, on drbd-01. The sequence matters: bring DRBD to UpToDate/UpToDate first, lay down the filesystem on /dev/drbd0, demote, hand control to Pacemaker. If you skip the demote step, Pacemaker fights DRBD for primary and the resource enters a failed state.
A few rules that save you a weekend:
- Pacemaker manages DRBD, not systemd. Once Pacemaker is in charge,
systemctl start drbdat boot will create the exact race condition you are trying to avoid. Thesystemctl disable drbdstep is not optional. - STONITH is not optional in production. The script ships with
stonith-enabled=falseso you can finish the install. Before you put real data on this cluster, configure an IPMI agent (fence_ipmilan) or a hypervisor-API agent (fence_vmware_rest,fence_xvm,fence_aws, etc.) and flip the property back totrue. A two-node cluster without fencing is a split-brain incident waiting to happen. - Test failover before the application moves in. Standby drill, hard-power drill, network-partition drill (pull the replication NIC, watch DRBD
Diskless, watch Pacemaker decide). If any of the three doesn’t produce a clean recovery, the cluster is not production-ready. - Dedicate a NIC to DRBD replication. Sharing the management VLAN with replication traffic works until the day an unrelated rsync saturates the link, replication backs up, write latency on the primary climbs, and the application thinks the disk is sick. A second NIC is cheap.
For an HA filesystem tier you don’t have to operate yourself, Stack Harbor runs DRBD + Pacemaker clusters under Managed Cloud Ops on managed cloud compute with fencing configured, drill cadence enforced, and Sev-1 escalation wired into our monitoring plane — start a scoping call.