Demo of HAProxy using Vagrant
This is the toolset I used to present on load balancers at University of Nebraska at Kearney on 2/19/14.
- It sets up a 3 VM mini-network inside Virtualbox. The three hosts are haproxy (172.28.33.1), web1 (172.28.33.11), and web2 (172.28.33.12)
- It sets up the following port forwards between your host's external interface and the internal VM's:
Host port | Guest machine | Guest port | Notes |
---|---|---|---|
8080 | haproxy | 8080 | HAProxy Admin Interface |
8081 | haproxy | 80 | Load Balanced Apache |
- It installs Apache on the two web servers, and configures it with a index page that identifies which host you're viewing the page on.
- It installs HAProxy on the haproxy host, and drops a configuration file in place with the two webservers pre-configured. It doesn't require HAProxy to be the default gateway because it NAT's the source IP as well as the destination IP.
- Install Vagrant
- Install Virtualbox
- Either clone this repo with
git clone https://github.com/kserge2001/vagrant-haproxy.git
or just download the current zip file and extract it in an empty directory.
- Open 3 terminal windows -- one for each host. Change to the directory containing the Vagrantfile from step 3 above.
- In terminal #1, run
vagrant up haproxy && vagrant ssh haproxy
- In terminal #2, run
vagrant up web1 && vagrant ssh web1
- In terminal #3, run
vagrant up web2 && vagrant ssh web2
- Open up https://localhost:8080/haproxy?stats in your host's browser. This is the HAProxy admin interface.
- Open up https://localhost:8081/ in your host's browser. This is the load balanced interface to the two web servers. Note this is port forwarded via your actual host, and will be accessible via your externally accessible IP address - you can access test the load balancer from another workstation if you wish.
- Open up https://172.28.33.11/ in a browser to see if web1's Apache is working.
- Open up https://172.28.33.12/ in a browser to see if web2's Apache is working.
- To see the Apache access logs on web1 and web2, run
sudo tail -f /var/log/apache2/access.log
If you'd like to filter out the "pings" from the load balancer, runsudo tail -f /var/log/apache2/access.log | grep -v OPTIONS
- To stop Apache on one of the webservers to simulate an outage, run
sudo service apache2 stop
To start it again, runsudo service apache2 start
- To make changes to haproxy, edit the config file with
sudo nano /etc/haproxy/haproxy.cfg
When you want to apply the changes, runsudo service haproxy reload
If you break things and want to reset back, just runsudo cp /etc/haproxy/haproxy.cfg.orig /etc/haproxy/haproxy.cfg && sudo service haproxy reload
- When you're all done, type
exit
at the shell to get back to your local terminal. - To shut down the VM's, run
vagrant halt web1 web2 haproxy
- To remove the VM's from your hard drive, run
vagrant destroy web1 web2 haproxy
- If you wish to remove the cached image file from which these machines were created, run
vagrant box remove precise32