Skip to content

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 pause and watching it recover
  • A rolling deploy with Ansible (serial: 1) behind a load balancer

Topology ​

NodeImageCPUs / memoryAddressesRole
lbdebian-131 / 1GiBmgmt 10.126.0.11HAProxy on :80, stats on :8404
web1debian-131 / 1GiBmgmt 10.126.0.12Backend
web2debian-131 / 1GiBmgmt 10.126.0.13Backend

Before you start ​

bash
vz image pull disk debian-13

Run it ​

bash
cd examples/26-load-balancer
vz validate
vz up
vz node ls
ssh lab@10.126.0.11        # password: lab

First 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 web2

Ansible: a rolling deploy ​

On the Mac:

  • ansible: brew install ansible In one terminal, keep requesting the site:
bash
while true; do curl -s http://localhost:8080; sleep 0.5; done

In 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 time

Notes ​

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