Demo showing how to setup redundant HAProxy servers to load-balance traffic between web servers.
Most of the setup is based on this blog post: HAProxy and Keepalived: Example Configuration
- Vagrant - 1.6.5+
- vagrant-cachier plugin (optional install)
vagrant plugin install vagrant-cachier
There will be two load-balancer servers running HAProxy and keepalived.
- LB1 will have VRRP priority 101 and is the master load-balancer
- LB2 will have VRRP priority 100 and is the backup load-balancer
Both LBs use round-robin algorithm to route traffic to Web1 and Web2 servers which run Nginx on port 80.
Create the 4 VMs:
vagrant up
Test it out:
# Hit web1-box directly
curl http://192.168.33.68
# Hit web2-box directly
curl http://192.168.33.68
# Hit vip
curl http://192.168.33.84
See the HAProxy stats:
LB 1: http://192.168.33.80/stats
LB 2: http://192.168.33.81/stats
In one terminal:
while true; do curl -sI http://192.168.33.84 | head -n 1; sleep 1; done
# ==> HTTP/1.1 200 OK
In a second terminal:
vagrant ssh lb-box1
sudo tcpdump -i eth1
# Look for VRRP advertisements.
# Press CTRL+C
sudo su
watch -n 2 "echo show stat | socat /var/lib/haproxy/stats.sock stdio | grep http,BACKEND"
# Observe the http backend bytes out (column 10)
# It should be going up
In a third terminal:
vagrant ssh lb-box2
sudo tcpdump -i eth1
# Look for VRRP advertisements.
# Press CTRL+C
sudo su
watch -n 2 "echo show stat | socat /var/lib/haproxy/stats.sock stdio | grep http,BACKEND"
# Observe the http backend bytes out (column 10)
# It should be going not be going up
In a fourth terminal:
vagrant halt lb-box1
The curl
requests should continue via the vip but be routed to LB2.
Observe the http backend bytes out (column 10) in third terminal (lb-box2) - it should now be going up.
vagrant up lb-box1
The curl
requests should continue via the vip but be routed back to LB1.
- HAProxy and Keepalived: Example Configuration
- Keepalived and HAProxy in AWS: An Exploratory Guide
- Making HAProxy High Available For MySQL Galera Cluster
- Redundant Load Balancers – HAProxy and Keepalived
- How To Use HAProxy to Set Up HTTP Load Balancing on an Ubuntu VPS
- Virtual Router Redundancy Protocol (VRRP)
- HAProxy Architecture Guide
- keepalived User Guide [PDF]