Demo of HAProxy high availability using Vagrant.
This project sets up the following VMs on a private network in VirtualBox:
- haproxy1 (192.168.2.9)
- haproxy2 (192.168.2.10)
- web1 (192.168.2.11)
- web2 (192.168.2.12)
The HAProxy servers are themselves made redundant via keepalived, with virtual IP 192.168.2.2.
- Install Vagrant
- Install Virtualbox
- Either clone this repo, or download the zip file and extract it to an empty directory.
- From the root directory, run
vagrant up
- View the various pages:
- source web sites: e.g., http://192.168.2.11
- web site, load-balanced via HA Proxy: e.g., http://192.168.2.9
- web site, through the virtual IP: http://192.168.2.2
- HA proxy status: e.g., http://192.168.2.2/haproxy?stats
keepalived is an implementation of the VRRP (Virtual Router Redundancy Protocol) protocol to make IPs highly available - a so called VIP (Virtual IP).
You can check which HAProxy server is currently handling requests to the virtual IP 192.168.2.2 by checking the server's ip addresses:
$ vagrant ssh haproxy1 -c "echo haproxy1; ip addr | grep 192.168.2.2/32"
haproxy1
inet 192.168.2.2/32 scope global eth1
Connection to 127.0.0.1 closed.
$ vagrant ssh haproxy2 -c "echo haproxy2; ip addr | grep 192.168.2.2/32"
haproxy2
Connection to 127.0.0.1 closed.
Try turning various servers on and off, and visit any of the URLs to see what the results are. As HAProxy services are turned on and off, also check the active HAProxy.
Examples:
vagrant ssh web1 -c "sudo service apache2 stop"
vagrant ssh web1 -c "sudo service apache2 start"
vagrant ssh haproxy1 -c "sudo service haproxy stop"
vagrant ssh haproxy1 -c "sudo service haproxy start"
vagrant halt
turns off vmsvagrant destroy
destroys all traces of the VMs, but leaves the cached imagesvagrant box remove ...
removes the downloaded image file.