HAProxy active/backup load balancer setup on CentOS/RHEL 7.x

In these days everyone wants to use High Availability and this is a good thing, so no more single points of failure.
Will demonstrate how this can be achieved using the following:
– lxc containers
– haproxy
– pacemaker
– corosync
– CentOS 7.x

Steps to follow:

If you are using lxc containers, “which” is not installed by default and it is needed
yum -y install which

Install pacemaker and pcs
yum -y install pacemaker pcs

Enable and restart the services
systemctl enable pcsd.service && systemctl start pcsd.service
systemctl enable pacemaker.service && systemctl start pacemaker.service
systemctl enable corosync.service && systemctl start corosync.service

Make sure you remove fqdn record from line containing "127.0.0.1" in /etc/hosts

Add node01 and node02 to /etc/hosts or as DNS records
vi /etc/hosts

And add:

192.168.100.2 node01
192.168.100.3 node02

Setup a password for hacluster user
passwd hacluster

Very important: permit traffic in firewalld
firewall-cmd --add-service=high-availability && \
   firewall-cmd --permanent --add-service=high-availability

Authentication between cluster members and cluster creation
pcs cluster auth node01 node02
pcs cluster setup --name haproxy_cluster node01 node02
pcs cluster start --all

Nodes must be restarted before moving on!

Check everything is right
pcs status
pcs status corosync
pcs status nodes

Disable stonith or your resources will not start
pcs property set stonith-enabled=false

Create the VIP resource for HAProxy
pcs resource create virtual_ip_haproxy ocf:heartbeat:IPaddr2 ip=192.168.100.100 cidr_netmask=24 op monitor interval=10s

Check again if the resource has started
pcs status resources

And:

ip a sh

Setup bash alias to copy haproxy.cfg to secondary node before restarting the service on primary
vi ~/.bashrc

And add:

alias haproxy_restart="haproxy -f /etc/haproxy/haproxy.cfg -c && \
    scp /etc/haproxy/haproxy.cfg root@node02:/etc/haproxy/haproxy.cfg && sleep 5 && systemctl restart haproxy"

Get haproxy ocf resource from web
cd /usr/lib/ocf/resource.d/heartbeat && \
    curl -O https://raw.githubusercontent.com/thisismitch/cluster-agents/master/haproxy && \
    chmod +x haproxy

Add resource for haproxy in cluster
pcs resource create haproxy ocf:heartbeat:haproxy op monitor interval=10s

Create a clone of haproxy resource that must run on both nodes
pcs resource clone haproxy

Now everything should be right, the only thing that remains to configure is /etc/haproxy.cfg and it is not the scope of this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s