How to Configure a High Availability (HA) Cluster using Pacemaker and Corosync on Linux

Introduction

High Availability (HA) clusters ensure that critical services remain accessible even if a server hardware failure occurs. In the Linux ecosystem, the standard approach involves combining Corosync (for cluster messaging and membership) and Pacemaker (the cluster resource manager). This guide provides a fundamental overview of setting up an active/passive HA cluster using these tools.

Prerequisites

To follow this guide, you require:

  • Two Linux servers (e.g., Ubuntu 22.04 or RHEL 9) on the same subnet. Let’s call them node01 and node02.
  • Root or sudo privileges on both servers.
  • DNS or /etc/hosts resolution properly configured so both nodes can ping each other by hostname.

Step 1: Install Pacemaker and Corosync

Begin by installing the necessary cluster packages on both nodes. On Debian/Ubuntu-based systems, run:

sudo apt update && sudo apt install pacemaker corosync pcs

On RHEL/CentOS/Rocky Linux, use:

sudo dnf install pacemaker corosync pcs

Start and enable the pcsd service (the Pacemaker Configuration System daemon) on both nodes:

sudo systemctl enable --now pcsd

Step 2: Set the Cluster Authentication Password

The installation creates a dedicated hacluster user. You must set a password for this user on both nodes to allow them to communicate securely.

sudo passwd hacluster

Ensure you use the same password on both node01 and node02.

Step 3: Authenticate and Create the Cluster

From node01, authenticate the cluster nodes using the pcs command line tool:

sudo pcs host auth node01 node02 -u hacluster -p YOUR_PASSWORD

Once authenticated, create the cluster and define its name (e.g., my_ha_cluster):

sudo pcs cluster setup my_ha_cluster node01 node02

Start the cluster services on both nodes and enable them to start on boot:

sudo pcs cluster start --all
sudo pcs cluster enable --all

Step 4: Disable STONITH (For Testing Only)

STONITH (Shoot The Other Node In The Head) is a fencing mechanism crucial for production clusters to prevent split-brain scenarios. However, configuring fencing hardware (like IPMI or PDU) is complex. For testing or initial setup in a lab environment, you must disable STONITH, or the cluster will refuse to start resources.

sudo pcs property set stonith-enabled=false

Step 5: Create a Cluster Resource (Virtual IP)

A common HA use case is a floating Virtual IP (VIP) that moves between nodes. We will create a resource named ClusterIP using the ocf:heartbeat:IPaddr2 resource agent. Run this on node01:

sudo pcs resource create ClusterIP ocf:heartbeat:IPaddr2 ip=192.168.1.150 cidr_netmask=24 op monitor interval=30s

Replace 192.168.1.150 with an available IP on your subnet. You can verify the cluster status and see which node currently hosts the Virtual IP by running:

sudo pcs status

If you gracefully reboot the active node or put it into standby mode using sudo pcs node standby node01, Pacemaker will automatically migrate the Virtual IP to node02, minimizing downtime.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.