In this short tutorial I will show how to install and configure HAproxy (basic configuration) to achieve load balancing between two web servers.
Before we start a brief overview of my test environment:
- HAproxy server - Has 2 NIC's - eth0 is configured with external IP address 18.104.22.168, eth1 is configured with internal IP address 192.168.0.5
- 2 Web servers - each with 1 NIC configured with internal ip addresses (192.168.0.6, 192.168.0.7) both listening on port 80
I will not go into the web server configurations themselves, but be sure to check both web server nodes are reachable from HAproxy server node and both are listening on port 80.
A good practice is to create a test html page with the node name on every back-end node, so you will actually see the LB in action when making request to the front-end node.
It's time to get our hands dirty.
You will need the 'epel' repository in order to install HAproxy via yum:
Install 'epel' repository:
# rpm -Uvh http://ftp-stud.hs-esslingen.de/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm
Next, install HAproxy and enable it's service on boot:
#yum install haproxy -y
#chkconfig haproxy on
Backup your initial HAproxy configuration file:
Get a sample configuration file:
#wget http://c818095.r95.cf2.rackcdn.com/haproxy.cfg -O /etc/haproxy/haproxy.cfg
It's time to edit our configuration file:
Under ,the default configuration banner "#HTTP default webfarm" locate the following line:
listen webfarm ...:80
And change it to your public external IP address, for example:
listen webfarm 22.214.171.124:80
Next, make sure to add the IP addresses of your web servers :
#replace with web node private ip
server web01 192.168.0.6:80 check
server web02 192.168.0.7:80 check
You may also want to limit the 'maxconn' parameter to limit the maximum allowed parallel connections to the server.
There are of course _much more_ parameters that I will not get into in this tutorial such as: ACL's, LB type's and many others.
Save the file and restart HAproxy:
Check that HAproxy is indeed listening on it's public address on port 80 via:
#lsof -i :80
haproxy 32607 haproxy 4u IPv4 134165 0t0 TCP server01:http (LISTEN)
... your load balancer is ready to receive requests.
Every http request to 126.96.36.199 will be redirected between the back-end nodes.