Apache Load Balancing

Apache Load Balancing is a technique aiming at distributing workload in a computer network, in order to optimally utilize resources, avoid overload and maximize throughput.Computer clusters rely on load balancing to distribute workload across network links, CPUs, web servers, etc. A server farm is a common application of load balancing, where multiple servers seamlessly provide a single Internet service. In this case the load balancer accepts requests from external clients and forwards them to one of the available back-end servers according to a scheduling algorithm (e.g. round robin, random choice, on a reported load basis, etc..)Load balancers can be implemented using dedicated hardware or ad-hoc software

Mod_Proxy_Balancer:-

mod_proxy_balancer is an Apache module available since Apache 2.1. It allows turning an Apache installation into a load balancer retrieving requested pages from two or more back-end web servers and delivering them to the user’s computer.One important feature of mod_proxy_balancer is that it can keep track of sessions which means that a single user always deals with the same back-end web server (sticky sessions).

Requirements for configuring apache load balancer:

The module requires:
1) an Apache HTTP Server installation version 2.1 or later
2) mod_proxy extension.

Apache Installation steps for load balancing:-
First install Apache Portable Runtime, APR Utils, and the Httpd server

a)APR installation

wget http://mirrors.axint.net/apache//apr/apr-1.4.6.tar.gz
tar -xvzf apr-1.4.6.tar.gz
cd apr-1.4.6/
./configure
make
make install

b)APR utils

wget http://mirrors.axint.net/apache//apr/apr-util-1.4.1.tar.gz
tar -xvzf apr-util-1.4.1.tar.gz
cd apr-util-1.4.1
./configure –with-apr=/usr/local/apr
make
make install

c)Apache installation (version-2.2.24)

Download httpd 2.2.24.tar.gz and execute the following commands

tar -xvzf httpd-2.2.24.tar.gz
cd httpd-2.2.24
./configure –prefix=/usr/local/apache2 –enable-mods-shared=all –enable-http –enable-deflate –enable-expires –enable-slotmem-shm –enable-headers –enable-rewrite –enable-proxy –enable-proxy-balancer –enable-proxy-http –enable-proxy-fcgi –enable-mime-magic –enable-log-debug
make && make install

Apache installation which supports load balancing is now complete.

Configuration changes to be made in apache:-

When you configure mod_proxy_balancer, you can choose among three load-balancing algorithms: Request Counting, Weighted Traffic Counting, and Pending Request Counting. The best algorithm to use depends on the individual use case. Here I have used request counting method.

Uncomment the following lines in your httpd.conf file

LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule negotiation_module modules/mod_negotiation.so

Add the lines for load balancer configuration shown below to the end of httpd.conf

#Distribution servers and method
<Proxy balancer://mybalancer>
BalancerMember http://192.168.1.21:80 loadfactor=1
BalancerMember http://192.168.1.32:80 loadfactor=3
ProxySet lbmethod=byrequests
</Proxy>
ProxyPass /balance balancer://mybalancer/
</IfModule>

Now restart apache and enter the URL http://localhost/balance. The page loaded will be from 192.168.1.21. Refresh page 3 times and content from 192.168.1.32 will be displayed on all 3 occasions(since load factor is 3 by request). We can use any name for Proxy balancer. It just signifies application name. Balancer Member directive specifies worker IP and port. As many balancer members as needed can be specified depending on the need. Load factor specifies load each worker takes. 2IP’s in this case. lbmethod specifies the algorithm to be used for load balancing. ProxyPass directive specifies that content from workers will be available at http://localhost/balance

Next add these lines to httpd.conf file

#Load Balancer Configuration- Balancer manager for web interface
<IfModule mod_proxy_balancer.c>
<Location “/balancer-manager”>
SetHandler balancer-manager
Order deny,allow
Deny from all
# Allow from local subnet only
Allow from all
</Location>

Occasionally you may need to change your load balancing configuration, but that may not be easy to do without affecting the running server. The above part of the configuration provides a web interface to handle the back end workers. mod_status extension is required for this feature. The balancer manager can be used to change load factor associated with a worker or to put it in offline mode. Statistics and configuration details will be displayed and settings could be edited through this interface. The Balancer Manager can be accessed by pointing a browser at http://localhost/balancer-manager.

Load Balancing Algorithms

There are mainly 3 algorithms for load balancing.
1)Request counting :- With this algorithm, incoming requests are distributed among back-end workers in such a way that each back end gets a proportional number of requests defined in the configuration by the load-factor variable. The example shown in this case uses request counting algorithm. 1 request out of every 4 will be sent to 192.168.1.21 and 3 requests will be sent to 192.168.1.32

2)Weighted Traffic Counting Algorithm :- Weighted Traffic Counting considers the number of bytes instead of number of requests.

3)Pending Request Counting Algorithm :- In this algorithm, the scheduler keeps track of the number of requests that are assigned to each back-end worker at any given time. Each new incoming request will be sent to the back end that has least number of pending requests – in other words, to the back-end worker that is relatively least loaded. This helps keep the request queues even among the back-end workers, and each request generally goes to the worker that can process it the fastest.

Leave a Reply

Your email address will not be published. Required fields are marked *