HAProxy Setup
We’re going to show you how to set up a very simple HAProxy configuration. We’re testing everything on Ubuntu 20.04 but it should work very similarly on other distros.
Our load balancer:
- 192.168.3.22:80
Our web servers that we will be balancing:
- test1.lab.net 192.168.3.101:80
- test2.lab.net 192.168.3.102:80
- test3.lab.net 192.168.3.103:80
Update your repo info and install the HAProxy package like this:
sudo apt update
sudo apt install haproxy
You can start, stop, restart, and check status like this:
sudo systemctl start haproxy
sudo systemctl stop haproxy
sudo systemctl restart haproxy
sudo systemctl status haproxy
Add the following sections to the end of your config file. The first section will set up the frontend. The second section will set up the backend nodes. The third section is optional and will set up stats.
sudo vi /etc/haproxy/haproxy.cfgfrontend Local_Server bind 192.168.3.22:80 mode http default_backend my_servers backend my_servers mode http balance roundrobin option forwardfor http-request set-header X-Forwarded-Port %[dst_port] http-request add-header X-Forwarded-Proto https if { ssl_fc } option httpchk HEAD / HTTP/1.1rnHost:localhost server test1.lab.net 192.168.3.101:80 server test2.lab.net 192.168.3.102:80 server test3.lab.net 192.168.3.103:80 frontend stats bind 192.168.3.22:1936 stats enable stats hide-version stats refresh 30s stats show-node stats auth username:password stats uri /stats
You can validate the config file like this:
haproxy -c -f /etc/haproxy/haproxy.cfg
Restart the service:
sudo systemctl restart haproxy
Test it by navigating here. This is the VIP and it should balance your connection between the backend servers.
192.168.3.22:80 |
You should also be able to view the stats page like this:
http://192.168.3.22:1936/stats |