Apache Load Balancer setup


A step by step guide

When I was setting up the apache load balancer first time in my life, I struggled to get correct information at one place. The documentation was also not standardized. So to help those who want to set up the load balancer on their own, here are the simple step which will help you to get this set up done in short time.

What is load balancer?

  • Load balancing is a computer networking methodology to distribute workload across multiple computers to achieve
    • High Availability
    • Maximize throughput
    • Minimize response time
    • Optimal resource utilization
    • Avoid overload
    • Increase reliability through redundancy
  • So a single Tomcat process death will not “kill” the entire site.
  • The load balancing service is usually provided by dedicated software or hardware
  • A load balancer is a worker that does not directly communicate with Tomcat but communicates with Tomcat worker. It is responsible for the management of several “real” workers
  • A Tomcat worker is a Tomcat instance that is waiting to execute servlets on behalf of some web server
  • It distributes the HTTP request load across the servers
  • The load balancer management includes but not limited to
    • Providing status and load metrics for the load balancer itself and all members via the status worker interface.
    • Allowing to dynamically reconfiguring load-balancing via the status worker interface.

Load Balancer architecture/setup

What we need?

  1. The Apache HTTP Web Server (You can download it from http://httpd.apache.org/download.cgi)
  2. The Apache Tomcat Server (Download it from http://tomcat.apache.org/)
  3. mod_jk: Tomcat-Apache plug-in that handles the communication between Tomcat and Apache. Download it from http://tomcat.apache.org/tomcat-3.3-doc/mod_jk-howto.html#s6
  • It gives better support for SSL.
  • Simple configuration

 

How to configure Apache?

  • You need to modify only 2 files in Apache HTTP Web Server
    • httpd.conf: This is the main Apache HTTP server configuration file. We will do mapping of URLs of Tomcat apps and Jboss apps defined in worker.properties
    • workers.properties: This file defines list of workers and its properties
  • Worker name is defined in worker.list property

In workers.properties

worker.list=tomcat_balancer1,tomcat_balancer2,…..

  • A worker is configured as a load balancer by setting its worker type to lb

worker.tomcat_balancer1.type=lb

  • Each worker has its own list of sub workers

worker. tomcat_balancer1.balance_workers=tomcat1

  • For each sub-worker define type, host and port to which the Apache load balancer will connect
  • For tomcat worker the type is ajp13

worker.tomcat1.type=ajp13

worker.tomcat1.host=<IP_Address_of_the_machine_on_which_tomcat_is_running>

worker.tomcat1.port=7007

To know the type and port number you can open <CATALINA_HOME>\conf\server.xml

Tomcat server conf

What is AJP? A short introduction

  • AJP is used to communicate between the web server and the servlet container
  • The Apache JServ Protocol (AJP) is a binary protocol that can proxy inbound requests from a web server through to an application server that sits behind the web server. The web server is a “reverse proxy,” meaning, its purpose is to handle incoming traffic from the Internet on behalf of the application server.
  • Gal Shachor was the original designer of this protocol. There is, apparently, no current documentation of how the protocol works.
  • Apache JServ is a 100% pure Java servlet engine fully compliant with the JavaSoft Java Servlet APIs 2.0 specification
  • Web implementers typically use AJP in a load-balanced deployment where one or more front-end web servers feed requests into one or more application servers.
  • AJP runs in Apache HTTP Server using the mod_jk plugin.
  • When it was developed the goals of this protocol was
    • Increasing performance (speed, specifically).
    • Adding support for SSL, so that isSecure() and getScheme() will function correctly within the servlet container.
  • A binary format was presumably chosen over the more readable plain text for reasons of performance.

In httpd.conf

You need to set the port on which the apache HTTP web server will listen to user request.

Listen 80

If port 80 is used by some other process then you need to change it to something else (8081)

LoadModule jk_module “C:/Apache_HTTP_2_2/modules/mod_jk.so”

JkWorkersFile “C:/Apache_HTTP_2_2/conf/workers.properties”

JkLogFile “C: /Apache_HTTP_2_2/conf/from_apache_mod_jk.log”

JkLogLevel info

JkLogStampFormat “[%a %b %d %H:%M:%S %Y]”

  • Mount the URL with the worker

JkMount /App1 tomcat_balancer1

JkMount /App2/* tomcat_balancer1

JkMount /App3/* tomcat_balancer1

JkMount /App1 tomcat _balancer2

JkMount /App2/* tomcat _balancer2

JkMount /App3/* tomcat _balancer2

Save the files. Start Tomcat server and wait till it is completely started. After that start apache http web server. Go to the browser and type the URL as

http://<Your_machine_name>:<port_number>/App1

Conclusion:

With these simple steps you can do the load balancer setup on any machine.

References

 

 


About Shashikant Kulkarni

Oracle Certified Master Java EE 5 Enterprise Architect, Sun Certified Java Programmer, Wordpress programmer having 14 year of experience in software development