Enhancing Load Balancing by Using Keepalived With HAProxy
Keepalived can provide failover services for backup routers, while at the same time also use HAProxy for load balancing to achieve high availability across distributed servers. The advantage of this approach is that the packet and application layers are separated, which means that the health checks that are performed by Keepalived for the load-balancing servers aren't impacted by the inbound HTTP or TCP traffic that HAProxy is managing. Also, failover routing, which is achieved by using VRRP, can be activated automatically without waiting for a client response to timeout. To learn more about the usefulness of VRRP, see Using Keepalived With VRRP.
The practicality of using this method is that if the public-facing HAProxy load balancer goes offline, Keepalived automatically detects this event and dynamically switches to another HAProxy server. If the Keepalived primary router goes offline, the VRRP settings that you configured ensure that traffic is automatically handled by the Keepalived backup router.
The role of HAProxy in the setup is to provide inbound load balancing and session persistence to the backend servers: Keepalived is solely responsible for monitoring the status of HAProxy and providing an alternative routing mechanism. Using both tools in combination provides a highly available and resilient load-balancing solution.
The following procedure is similar to Setting Up Load Balancing in NAT Mode, however in this case HAProxy is installed on both the Keepalived primary server and the Keepalived backup server.
In this procedure, the external virtual IP address is 192.168.1.1
on the
192.168.1.0/24
external network. This IP address is dynamically assigned
through NAT between the Keepalived primary server whose IP address is
192.168.1.10
, and the Keepalived backup server, whose external IP address
is 192.168.1.11
.
The internal network is hosted on the 10.0.0.0/24
subnet. For IP addresses,
websvr1
has 10.0.0.71
while websvr2
has
10.0.0.72
.
Figure 3-2 Keepalived and HAProxy Configuration for High Availability Load Balancing
