Share


HAProxy: HAProxy is free, open source software that provides high availability, load balancing and proxying for TCP and HTTP-based applications. HAProxy is written in C. It stands for High Availability Proxy.

HAProxy works by spreading incoming requests from clients across multiple servers which is the base of any load balancing algorithm. A client connects to an HAProxy instance which processes all
of the requests by using a reverse proxy to forward the request to one of the available endpoints, based on some load-balancing algorithm.

HAProxy can be run on Linux, Solaris and FreeBSD. In this guide we will see how to install HAProxy load-balancer on Linux Ubuntu OS.

Prerequisites

1. “sudo” access to the system upon which you are installing HAProxy.
2. Add private IP addresses to your virtual machines.
3. Nginx or some other webserver installed on virtual machines other than load-balancer. The incoming requests will be distributed between these two web-servers.

Installing and Configuring load balancing with HAProxy

For simplicity we will install and configure HAProxy load-balancer with following machines:
1. web server1: IP Address: 10.0.12.15
2. web server2: IP Address: 10.0.12.16
3. HAproxy load-balancer 10.0.12.10

Step: 1 First update your system packages:

$ sudo apt-get update && sudo apt-get upgrade


Step: 2 To install HAProxy on Ubuntu, run the following command:

$ sudo apt-get install haproxy


To manage HAProxy via an init script, set “enabled” parameter to 1 as shown below:

$ nano /etc/default/haproxy


ENABLED=1


Now the following option can be used with an init script:

$ service haproxy “option”
start reload restart status stop

Step: 3 Now edit the haproxy default configuration file i.e. /etc/haproxy/haproxy.cfg and configure it as described below. Run the following command

$ vim /etc/haproxy/haproxy.cfg


Now go to the end of the file and edit the following information:

frontend Local_Server
bind 10.0.12.10:80
mode http
default_backend webserver
backend webserver
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 web1 10.0.12.15:80
server web2 10.0.12.16:80


Step: 4 Verify the configuration syntax with the following command:

$ sudo haproxy -c -f /etc/haproxy/haproxy.cfg


If everything goes right, it will show an output like: “Configuration file is valid.”

Step: 5 Now restart Haproxy and enter 10.0.12.10(load-balancer system ip) on web browser and refresh continuously 2-4 times to see if HAProxy load-balancer is working.

$ sudo service haproxy restart


Now, any incoming requests to the HAProxy load-balancer at IP address 10.0.12.10 will be forwarded to webserver1 and webserver2 with an IP address 10.0.12.15 and 10.0.12.16 respectively.

These web-server will serve the HTTP requests. Also you can try checking if HAProxy is working properly by stopping one web-server at a time.


Share