When an application becomes popular, it sends an increased number of requests to the application server. A single application server may not be able to handle the entire load alone. We can always scale up the underlying hardware, that is, add more memory and more powerful CUPs to increase the server capacity; but these improvements do not always scale linearly. To solve this problem, multiple replicas of the application server are created and the load is distributed among these replicas. Load balancing can be implemented at OSI Layer 4, that is, at TCP or UDP protocol levels, or at Layer 7, that is, application level with HTTP, SMTP, and DNS protocols.
In this article, we will install a popular load balancing or load distributing service, HAProxy. HAProxy receives all the requests from clients and directs them to the actual application server for processing. Application server directly returns the final results to the client. We will be setting HAProxy to load balance TCP connections.
You will need two or more application servers and one server for HAProxy:
You will need the root access on the server where you want to install HAProxy
It is assumed that your application servers are properly installed and working
Install and Configure HAProxy:
Follow these steps to load balancing with HAProxy:
- Install HAProxy:
$ sudo apt-get update $ sudo apt-get install haproxy
- Enable the HAProxy
initscript to automatically start HAProxy on system boot. Open
$ cat /etc/default/haproxy # Set ENABLED t 1 if you want the init script to start haproxy. ENABLED=1 # Ad extra flags here. #EXTRAOPTS="-de -m 16" $
$ cd /etc/haproxy $ sudo cp haproxy.cfg haproxy.cfg.copy $ sudo nano haproxy.cfg
- Find the
defaultssection and change the
optionparameters to match the following:
mode tcp option tcplog
defaults log global mode tcp option tcplog option dontlognull contimeout 5000
- Next, define
frontend, which will receive all requests:
frontend www bind 184.108.40.206:80 # haproxy public IP default_backend as-backend # backend used
backend as-backend balance leastconn mode tcp server as1 10.0.2.71:80 check # application srv 1 server as2 10.0.2.72:80 check # application srv 2
Save and quit the HAProxy configuration file.
We need to set
rsyslogto accept HAProxy logs. Open the
/etc/rsyslog.conf, and uncomment following parameters:
$ModLoad imudp $UDPServerRun 514
# provides UDP syslog reception $ModLoad imudp $UDPServerRun 54
$ sudo nano /etc/rsyslog.d/haproxy.conf
Save the changes and exit the new file.
$ sudo service rsyslog restart
- Restart HAProxy:
$ sudo service haproxy restart
- Now, you should be able to access your backend with the HAProxy IP address.
How HAProxy works:
Here, we have configured HAProxy as a frontend for a cluster of application servers. Under the
frontend section, we have configured HAProxy to listen on the public IP of the HAProxy server. We also specified a backend for this frontend. Under the
backend section, we have set a private IP address of the application servers. HAProxy will communicate with the application servers through a private network interface. This will help to keep the internal network latency to a minimum.
HAProxy supports various load balancing algorithms. Some of them are as follows:
Round-robin distributes the load in a round robin fashion. This is the default algorithm used.
leastconn selects the backend server with fewest connections.
source uses the hash of the client’s IP address and maps it to the backend. This ensures that requests from a single user are served by the same backend server.
We have selected the leastconn algorithm, which is mentioned under the
backend section with the
balance leastconn line. The selection of a load balancing algorithm will depend on the type of application and length of connections.
Lastly, we configured
rsyslog to accept logs over UDP. HAProxy does not provide separate logging system and passes logs to the system log daemon,
rsyslog, over the UDP stream.
Depending on your Ubuntu version, you may not get the latest version of HAProxy from the default
apt repository. Use the following repository to install the latest release:
$ sudo apt-get install software-properties-common $ sudo add-apt-repository ppa:vbernat/haproxy-1.6 # replace 1.6 with required version $ sudo apt-get update && apt-get install haproxy