Creating a load-balanced MySQL or MariaDB cluster with Pacemaker and HAProxy is a critical task for maintaining high availability and fault tolerance in your database environment. Pacemaker is an open-source high-availability resource manager that ensures your applications and services remain accessible during unexpected failures. HAProxy is a high-performance load balancer that distributes incoming requests to multiple servers to optimize performance and maintain uptime. By combining these two technologies, you can create a resilient and scalable database cluster capable of handling high levels of traffic.
This guide will walk you through the process of setting up a MySQL or MariaDB cluster, configuring Pacemaker for high availability, and using HAProxy to distribute incoming requests evenly across your cluster. The steps provided assume you have a basic understanding of Linux system administration and networking concepts.
sudo apt-get update && sudo apt-get install -y mariadb-server mariadb-client pacemaker pcs haproxy
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
bind-address = 0.0.0.0 wsrep_provider = /usr/lib/galera/libgalera_smm.so wsrep_cluster_address = "gcomm://NODE1_IP,NODE2_IP,NODE3_IP"
sudo systemctl start mariadb --wsrep-new-cluster
sudo systemctl start mariadb
mysql -u root -e "SHOW STATUS LIKE 'wsrep_cluster_size';"
sudo pcs cluster setup --name my_cluster NODE1_HOSTNAME NODE2_HOSTNAME NODE3_HOSTNAME
sudo systemctl enable pacemaker corosync && sudo systemctl start pacemaker corosync
sudo pcs resource create Cluster_VIP ocf:heartbeat:IPaddr2 ip=VIRTUAL_IP_ADDRESS cidr_netmask=NETMASK op monitor interval=30s
sudo nano /etc/haproxy/haproxy.cfg
frontend mysql_frontend bind *:3306 mode tcp default_backend mysql_backend backend mysql_backend mode tcp balance leastconn option tcp-check tcp-check connect port 3306 server node1 NODE1_IP:3306 check server node2 NODE2_IP:3306 check server node3 NODE3_IP:3306 check
sudo systemctl enable haproxy && sudo systemctl start haproxy
Comment anonymously. Login not required.