26 Load balancer
HAProxy spreads requests over two nginx servers and stops sending to one that goes down.
What you practice
- Round-robin balancing and health checks in HAProxy
- Failing a backend with
vz node pauseand watching it recover - A rolling deploy with Ansible (
serial: 1) behind a load balancer
Topology
| Node | Image | CPUs / memory | Addresses | Role |
|---|---|---|---|---|
lb | debian-13 | 1 / 1GiB | mgmt 10.126.0.11 | HAProxy on :80, stats on :8404 |
web1 | debian-13 | 1 / 1GiB | mgmt 10.126.0.12 | Backend |
web2 | debian-13 | 1 / 1GiB | mgmt 10.126.0.13 | Backend |
Before you start
bash
vz image pull disk debian-13Run it
bash
cd examples/26-load-balancer
vz validate
vz up
vz node ls
ssh lab@10.126.0.11 # password: labFirst boot installs packages; give it a minute or two after vz up returns.
Try it
bash
for i in 1 2 3 4; do curl -s http://localhost:8080; done # web1, web2, web1, web2
open http://localhost:8404 # HAProxy stats page
vz node pause web2 # turns red after ~4s
for i in 1 2 3; do curl -s http://localhost:8080; done # web1 only
vz node resume web2Ansible: a rolling deploy
On the Mac:
- ansible:
brew install ansibleIn one terminal, keep requesting the site:
bash
while true; do curl -s http://localhost:8080; sleep 0.5; doneIn another:
bash
cd ansible
chmod 600 lab_key
ansible all -m ping
ansible-playbook deploy.yml -e version=v2 # the loop never fails; v1 turns into v2 one server at a timeNotes
The key pair in ansible/ was generated for this example and is public now: use it only in this lab. Git does not keep file modes, so run chmod 600 ansible/lab_key once, or ssh refuses the key.
Clean up
bash
vz down
vz destroy --yes # deletes the overlays and state of this lab