Advertisment

Web Server Load-Balancing on Ubuntu with HAProxy

HAProxy is an open-source load-balancer which can load balance any TCP service. HAProxy is a free, very fast and reliable solution

author-image
Rajkumar Maurya
New Update
haproxy-ubuntu

HAProxy is an open-source load-balancer which can load balance any TCP service. HAProxy is a free, very fast and reliable solution that offers load-balancing, high-availability, and proxying for TCP and HTTP-based applications

Advertisment

STEP 1: Here, we are installing HAProxy (High Availability Proxy) in Ubuntu by using the below commands:
apt-get install haproxy
haproxy -v
STEP 2: Start HAProxy by using the init script /etc/default/haproxy. Set ENABLED option to 1 as:
ENABLED=1
STEP 3: HAProxy is now installed. Now install 2(two) Apache Web Server in other machines. Here, we are using three systems virtually through Virtual Box.
Virtual machine 1 - Load Balancer
Hostname: haproxy
OS: Ubuntu
Private IP: 192.168.5.215
Virtual machine 2 - Web Server 1
Hostname: webser01
OS: Ubuntu with LAMP
Private IP: 192.168.5.216
Virtual machine 3- Web Server 2
Hostname: webserver02
OS: Ubuntu with LAMP
Private IP: 192.168.5.217
STEP 4: Before configuring HAProxy, take a backup of the original file by renaming it:
mv /etc/haproxy/haproxy.cfg{,.original}
Here, we have created haproxy.cfg file. Create nano /etc/haproxy/haproxy.cfg file as:
global
log /dev/log local0
log 127.0.0.1 local1 notice
maxconn 4096
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen webfarm 0.0.0.0:80
mode http
stats enable
stats uri /haproxy?stats
balance roundrobin
option httpclose
option forwardfor
server webserver01 192.168.5.216:80 check
server webserver02 192.168.5.217:80 check
STEP 5: Start the HAProxy service using below command:
sudo service haproxy start
STEP 6: Testing Load-Balancing and Fail-over:
Edit the server name in both the default index.html files located by default at /var/www/index.html on both web servers.

On the Web Server 1 (webserver01 with IP- 192.168.5.216), append below line as:
sudo sh -c “echo

Hostname: webserver01 (192.168.5.216)

>> /var/www/index.html”
On the Web Server 2 (webserver02 with IP- 192.168.5.217), append below line as:
sudo sh -c “echo

Hostname: webserver02 (192.168.5.217)

Advertisment

>> /var/www/index.html”
STEP 7: Now, open your browser on your local machine and type haproxy installed machine IP i.e. http://192.168.5.215. Every time when you refresh the tab, the load is being distributed to each web server.

proxy2

When we refresh the page for the second time, we get the following interface.

Advertisment
proxy1
ubuntu server load web-server-load-balancing haproxy
Advertisment

Stay connected with us through our social media channels for the latest updates and news!

Follow us: