Thursday, July 9, 2015

Apache – mod_proxy_balancer

We have seen many Apache modules which provides various benefits like load balancing support , security, URL rewriting and Proxy passing etc. In this article we will see one more Apache module called mod_proxy_balancer which works as a load balancer. We use this module when we have a stateless applications which does not require any sticky sessions or clustering but need load balancing support.

1) Load the module mod_proxy_balancer by adding the below line to the httpd.conf

LoadModule proxy_balancer_module "/opt/ers40jk/apache2.2/modules/standard/mod_proxy_balancer.so"

2) Once the module is loaded we need to add the set handler element for mod_proxy_balancer element as

<IfModule mod_proxy_balancer.c>
<Location "/balancer-manager">
  SetHandler balancer-manager
  Order deny,allow
  Allow from all
</Location>

3) Now we need to add the URL from Back end so that Apache can send the requests. The configuration looks as

<Proxy balancer://localhost>
         BalancerMember http://172.16.202.95:18011 loadfactor=1
         BalancerMember http://172.16.202.96:18010 loadfactor=2
</Proxy>

4) Once we have added the necessary back end URL to send requests , we need to provide the proxy pass elements so that we can use this in the browser URL

ProxyPass /myApp balancer://localhost/myApp/
</IfModule>

This example is setup for round robin load balancing where the second BalanceMember processes 2 of every 3 requests. You can adjust this and also the load balancing method based on your needs. Some different load balancing methods mentioned in the doc are byrequests, bytraffic and bybusyness.

Here is the total configuration in my httpd.conf file

<VirtualHost *:8580>
  
    DocumentRoot /var/www/virtual/www.sam1.com/html/
    ServerName myproject.local
    ErrorLog logs/dummy-www.sam1.com-8380-error_log
    CustomLog logs/dummy-www.sam1.com-8380-access_log common
 
    <Directory "/var/www/virtual/www.sam1.com/html/">
        Options None
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
        Allow from all
    </Directory>
 
     <IfModule mod_proxy_balancer.c>
          <Location "/balancer-manager">
            SetHandler balancer-manager
            Order deny,allow
            Deny from all
            Allow from all
          </Location>

        <Proxy balancer://localhost>
             BalancerMember http://172.16.202.95:18011 loadfactor=1
             BalancerMember http://172.16.202.96:18010 loadfactor=2
        </Proxy>

             ProxyPass /myApp balancer://localhost/myApp/
       </IfModule>

</VirtualHost>

We access the same application deployed in both tomcat instances back end running on IP address 172.16.202.95 and 96. The application is accessed using myproject.local:8580/myApp


Hope this helps, More to Come J

No comments :

Post a Comment