Skip to main content

Shared MariaDB 11.8 Galera Cluster Setup

All backend pods and edge proxy nodes in a multi-node hermEX deployment read and write shared directory metadata, server registries, ACME TLS certificates, centralized logs, and the outbound relay spool (SELECT ... FOR UPDATE SKIP LOCKED) in MariaDB 11.8. This guide configures a fault-tolerant 3-node MariaDB 11.8 Galera Cluster behind a virtual IP (10.0.0.5:3306) using either HAProxy + Keepalived or MariaDB MaxScale + Keepalived.

For how hermEX pods connect to this cluster, see Multi-Node Cluster Installation or Single-to-Multi Migration.


1. Topology and Node Layout​

Never run a production Galera cluster with two database nodes alone. A 2-node cluster loses quorum if either node stops. Always deploy three database nodes (or two database nodes plus one garbd arbitrator node) on your private cluster network:

NodeRolePrivate IPPurpose
db-1MariaDB 11.8 + Galera10.0.0.21Database node 1 (wsrep_cluster member)
db-2MariaDB 11.8 + Galera10.0.0.22Database node 2 (wsrep_cluster member)
db-3MariaDB 11.8 + Galera10.0.0.23Database node 3 (wsrep_cluster member)
db-vipSQL VIP (haproxy or maxscale + keepalived)10.0.0.5:3306Single database_dsn endpoint used by all hermEX pods
Keep SQL and Galera ports off the public Internet

Bind MariaDB and VIP listeners only on private cluster IPs (10.0.0.0/24). Allow Galera replication ports (4567, 4568, 4444) strictly between db-1, db-2, and db-3, and allow SQL port 3306 strictly from your hermEX pods and database proxy nodes.


2. Firewall Ports and MariaDB 11.8 Installation​

Run the commands below on all three database nodes (db-1, db-2, db-3).

Open Private Cluster Ports​

Port / ProtocolServiceSource Allowed
3306/tcpMariaDB client connectionshermEX pods + db-vip proxies (10.0.0.0/24)
4567/tcp, 4567/udpGalera group communicationdb-1, db-2, db-3 only
4568/tcpGalera Incremental State Transfer (IST)db-1, db-2, db-3 only
4444/tcpGalera State Snapshot Transfer (SST via mariabackup)db-1, db-2, db-3 only
protocol 112 (VRRP)Keepalived failover heartbeats (if colocated)db-1, db-2, db-3 / LB nodes only

Example with firewalld (RHEL/AlmaLinux/Rocky 9) or ufw (Debian/Ubuntu):

# RHEL / AlmaLinux / Rocky 9 (firewalld)
sudo firewall-cmd --permanent --new-zone=hermex-db
sudo firewall-cmd --permanent --zone=hermex-db --add-source=10.0.0.0/24
for p in 3306/tcp 4567/tcp 4567/udp 4568/tcp 4444/tcp; do
sudo firewall-cmd --permanent --zone=hermex-db --add-port="$p"
done
sudo firewall-cmd --permanent --zone=hermex-db --add-rich-rule='rule protocol value="vrrp" accept'
sudo firewall-cmd --reload

# Debian / Ubuntu (ufw)
sudo ufw allow from 10.0.0.0/24 to any port 3306 proto tcp
for p in 4567/tcp 4567/udp 4568/tcp 4444/tcp; do
sudo ufw allow from 10.0.0.0/24 to any port "${p%/*}" proto "${p#*/}"
done

Install MariaDB 11.8 Server and mariabackup​

On RHEL 9 / AlmaLinux 9 / Rocky 9:

curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash -s -- --mariadb-server-version="mariadb-11.8"
sudo dnf -y install MariaDB-server MariaDB-client MariaDB-backup galera-4 rsync socat

On Debian 12 / Ubuntu 22.04 LTS / Ubuntu 24.04 LTS:

sudo apt-get update
sudo apt-get install -y curl apt-transport-https ca-certificates
curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | sudo bash -s -- --mariadb-server-version="mariadb-11.8"
sudo apt-get install -y mariadb-server mariadb-client mariadb-backup galera-4 rsync socat

Ensure time synchronization is active on every node:

sudo systemctl enable --now chronyd || sudo systemctl enable --now systemd-timesyncd

3. Configure Galera Cluster on Each Node​

Stop MariaDB before writing the cluster configuration:

sudo systemctl stop mariadb

On RHEL-family systems, place the configuration in /etc/my.cnf.d/60-galera.cnf. On Debian/Ubuntu, place it in /etc/mysql/mariadb.conf.d/60-galera.cnf.

Replace THIS_NODE_IP and THIS_NODE_NAME on each host (10.0.0.21 / db-1, 10.0.0.22 / db-2, 10.0.0.23 / db-3), and replace REPLACE_SST_PASSWORD:

[mysqld]
# Network and character set
bind-address = 0.0.0.0
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
skip-name-resolve = ON
max_connections = 1000
max_allowed_packet = 128M

# InnoDB tuning for hermEX directory, relay spool, and logs
default_storage_engine = InnoDB
innodb_buffer_pool_size = 2G
innodb_log_file_size = 512M
innodb_flush_log_at_trx_commit = 1
innodb_doublewrite = 1
innodb_autoinc_lock_mode = 2
binlog_format = ROW

# Galera cluster settings
wsrep_on = ON
wsrep_provider = /usr/lib64/galera-4/libgalera_smm.so
wsrep_cluster_name = "hermex_galera"
wsrep_cluster_address = "gcomm://10.0.0.21,10.0.0.22,10.0.0.23"
wsrep_node_address = "THIS_NODE_IP"
wsrep_node_name = "THIS_NODE_NAME"

# State Snapshot Transfer using non-blocking mariabackup
wsrep_sst_method = mariabackup
wsrep_sst_auth = "sstuser:REPLACE_SST_PASSWORD"

# Increase flow-control tolerance for relay bursts and system_logs
wsrep_provider_options = "gcs.fc_limit=64;gcache.size=1G"
Verify wsrep_provider library path by OS

On RHEL/AlmaLinux/Rocky 9, Galera installs to /usr/lib64/galera-4/libgalera_smm.so or /usr/lib64/galera/libgalera_smm.so. On Debian 12 and Ubuntu, it installs to /usr/lib/galera/libgalera_smm.so. Check with ls /usr/lib*/galera*/libgalera_smm.so and set wsrep_provider to the exact path present on your servers.


4. Bootstrap the Cluster and Create Databases​

  1. Initialize the primary component only on db-1 (10.0.0.21):
    sudo galera_new_cluster
    Wait until db-1 starts, then connect locally and create the SST user, hermEX cluster account, and email database (plus optional email_relay if you isolate outbound spool I/O):
    sudo mariadb <<'EOF'
    CREATE DATABASE IF NOT EXISTS email
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_unicode_ci;

    CREATE DATABASE IF NOT EXISTS email_relay
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_unicode_ci;

    CREATE USER IF NOT EXISTS 'sstuser'@'localhost' IDENTIFIED BY 'REPLACE_SST_PASSWORD';
    CREATE USER IF NOT EXISTS 'sstuser'@'10.0.0.%' IDENTIFIED BY 'REPLACE_SST_PASSWORD';
    GRANT RELOAD, PROCESS, LOCK TABLES, BINLOG MONITOR, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
    GRANT RELOAD, PROCESS, LOCK TABLES, BINLOG MONITOR, REPLICATION CLIENT ON *.* TO 'sstuser'@'10.0.0.%';

    CREATE USER IF NOT EXISTS 'hermex'@'10.0.0.%' IDENTIFIED BY 'REPLACE_STRONG_DB_PASSWORD';
    GRANT ALL PRIVILEGES ON email.* TO 'hermex'@'10.0.0.%';
    GRANT ALL PRIVILEGES ON email_relay.* TO 'hermex'@'10.0.0.%';

    FLUSH PRIVILEGES;
    EOF
  2. Start MariaDB on db-2 and then on db-3 so each node joins via SST and replicates the email schema and users:
    # On db-2, wait until ready, then run on db-3
    sudo systemctl enable --now mariadb
  3. Enable the mariadb service on db-1 as well (sudo systemctl enable mariadb) and verify cluster membership from any node:
    sudo mariadb -e "SHOW GLOBAL STATUS WHERE Variable_name IN ('wsrep_cluster_size','wsrep_cluster_status','wsrep_local_state_comment','wsrep_ready');"
    Confirm wsrep_cluster_size = 3, wsrep_cluster_status = Primary, wsrep_local_state_comment = Synced, and wsrep_ready = ON.

5. Expose the Single SQL VIP (10.0.0.5:3306)​

Point every hermEX pod to a floating VIP (10.0.0.5:3306) rather than hardcoding a single database node IP. Choose Option A (HAProxy + Keepalived) or Option B (MariaDB MaxScale + Keepalived). Both options route active SQL writes to one primary node at a time to eliminate Galera certification deadlocks on concurrent SELECT ... FOR UPDATE SKIP LOCKED relay workers and schema locks.

Run the SQL proxy + Keepalived pair on two dedicated lightweight load balancers (lb-1 at 10.0.0.6 and lb-2 at 10.0.0.7), or colocate them on db-1 and db-2.

Allow binding to the floating VIP on both proxy hosts:

echo 'net.ipv4.ip_nonlocal_bind = 1' | sudo tee /etc/sysctl.d/90-vip-bind.conf
sudo sysctl -p /etc/sysctl.d/90-vip-bind.conf

Option A: HAProxy + Keepalived​

  1. Install HAProxy and Keepalived on both proxy hosts:
    # RHEL-family
    sudo dnf -y install haproxy keepalived

    # Debian / Ubuntu
    sudo apt-get install -y haproxy keepalived
  2. Create a dedicated unauthenticated health-check user inside MariaDB (accessible only from your proxy hosts, or use port checks as shown below without extra users):
    CREATE USER IF NOT EXISTS 'haproxy_check'@'10.0.0.%';
    FLUSH PRIVILEGES;
  3. Write /etc/haproxy/haproxy.cfg on both proxy hosts. Notice db-2 and db-3 carry backup so all active SQL writes hit one healthy primary (db-1) and automatically fail over cleanly if db-1 goes down:
    global
    log /dev/log local0
    user haproxy
    group haproxy
    daemon
    maxconn 4096

    defaults
    log global
    mode tcp
    option tcplog
    option dontlognull
    retries 3
    timeout connect 5s
    timeout client 10m
    timeout server 10m
    timeout check 3s

    frontend mariadb_in
    bind 10.0.0.5:3306
    default_backend mariadb_galera

    backend mariadb_galera
    mode tcp
    balance first
    option mysql-check user haproxy_check
    default-server inter 2s fall 3 rise 2 on-marked-down shutdown-sessions
    server db-1 10.0.0.21:3306 check
    server db-2 10.0.0.22:3306 check backup
    server db-3 10.0.0.23:3306 check backup
  4. Enable and start HAProxy:
    sudo haproxy -c -f /etc/haproxy/haproxy.cfg
    sudo systemctl enable --now haproxy
    (On SELinux systems: sudo setsebool -P haproxy_connect_any 1.)

Option B: MariaDB MaxScale + Keepalived​

  1. Install MaxScale on both proxy hosts from the MariaDB repository:
    # RHEL-family
    sudo dnf -y install maxscale keepalived

    # Debian / Ubuntu
    sudo apt-get install -y maxscale keepalived
  2. Create the MaxScale monitor account in MariaDB (run once on db-1):
    CREATE USER IF NOT EXISTS 'maxscale'@'10.0.0.%' IDENTIFIED BY 'REPLACE_MAXSCALE_PASSWORD';
    GRANT REPLICATION CLIENT, SUPER, RELOAD, PROCESS, SHOW DATABASES, EVENT ON *.* TO 'maxscale'@'10.0.0.%';
    GRANT SELECT ON mysql.* TO 'maxscale'@'10.0.0.%';
    FLUSH PRIVILEGES;
  3. Write /etc/maxscale.cnf on both proxy hosts (readconnroute with router_options=master guarantees strict single-writer routing with zero split-transaction surprises across directory and relay):
    [maxscale]
    threads=auto

    [db-1]
    type=server
    address=10.0.0.21
    port=3306
    protocol=MariaDBBackend

    [db-2]
    type=server
    address=10.0.0.22
    port=3306
    protocol=MariaDBBackend

    [db-3]
    type=server
    address=10.0.0.23
    port=3306
    protocol=MariaDBBackend

    [Galera-Monitor]
    type=monitor
    module=galeramon
    servers=db-1,db-2,db-3
    user=maxscale
    password=REPLACE_MAXSCALE_PASSWORD
    monitor_interval=2000ms

    [hermEX-Write-Router]
    type=service
    router=readconnroute
    router_options=master
    servers=db-1,db-2,db-3
    user=maxscale
    password=REPLACE_MAXSCALE_PASSWORD

    [hermEX-Listener]
    type=listener
    service=hermEX-Write-Router
    protocol=MariaDBClient
    address=10.0.0.5
    port=3306
  4. Start MaxScale and confirm all three servers are Synced:
    sudo systemctl enable --now maxscale
    sudo maxctrl list servers

Keepalived VRRP Configuration for 10.0.0.5​

Create /usr/local/bin/check_sql_proxy.sh on both proxy hosts (adjust the process name to maxscale if using Option B):

#!/bin/bash
set -euo pipefail
pgrep -x haproxy >/dev/null

Make it executable (sudo chmod +x /usr/local/bin/check_sql_proxy.sh), and write /etc/keepalived/keepalived.conf:

On Primary Proxy (lb-1):

vrrp_script chk_sql_proxy {
script "/usr/local/bin/check_sql_proxy.sh"
interval 2
fall 2
rise 2
}

vrrp_instance VI_HERMEX_DB {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
unicast_src_ip 10.0.0.6
unicast_peer {
10.0.0.7
}
authentication {
auth_type PASS
auth_pass REPLACE_VRRP_SECRET
}
virtual_ipaddress {
10.0.0.5/24 dev eth0
}
track_script {
chk_sql_proxy
}
}

On Backup Proxy (lb-2), set state BACKUP, priority 100, unicast_src_ip 10.0.0.7, and unicast_peer { 10.0.0.6 }, replace eth0 with your private interface name (ip -br link), and start Keepalived on both hosts:

sudo systemctl enable --now keepalived

6. Connect hermEX Pods and Verify​

Set database_dsn in /etc/hermex/config.json on all hermEX pods and edge nodes (leave relay_database_dsn empty "" to share email, or point it to /email_relay):

{
"database_dsn": "hermex:REPLACE_STRONG_DB_PASSWORD@tcp(10.0.0.5:3306)/email?parseTime=true&multiStatements=true",
"relay_database_dsn": ""
}

Verify connectivity, schema initialization, and backups from Pod 0:

mariadb -h 10.0.0.5 -u hermex -p -e "SELECT VERSION(), @@wsrep_node_name;" email
sudo hermex-admin -config /etc/hermex/config.json ensure-schema
sudo hermex-admin -config /etc/hermex/config.json backup-db --output /tmp/cluster-db-test.sql.gz
Cluster recovery after a total power loss

If all three Galera nodes stop simultaneously, inspect /var/lib/mysql/grastate.dat on each node. Start galera_new_cluster on the node showing safe_to_bootstrap: 1 (or the highest seqno reported by sudo mariadbd --wsrep-recover if all three show 0, after setting safe_to_bootstrap: 1 on that one node only), then run systemctl start mariadb on the remaining two nodes.