Powered by Blogger.

Load balancing tomcat with Apache

In some circumstances, it is mandatory to deploy your application on multiple tomcat instances instead of a single one, this usually happens when working with a heavy application which consumes a big amount of memory storage and has a big number of active users. The main advantage of using multiple instances is to distribute the requests and the load on multiple nodes, to provide high availability and to speed up serving requests.
Load balancing is the process of distributing client requests to multiple available nodes , normally the application is exposed to the clients through a web server which receives the requests and distributes them to the configured application servers based on the availability and strength of each one. In this article, we describe how to configure a simple tomcat load balancer through an apache web server.
Apache Load Balancer

1. Installing tomcat instances

Install 2 tomcat instances on 2 different windows machines using this tutorial. Normally it’s okay to configure multiple tomcat instances on the same machine, but it is always recommended to install each instance on a separate machine so that you distribute the load to 2 physical memories instead of 1.
2. Download and install Apache
Download the latest stable Apache version from here, you can set up Apache on one of tomcat machines or dedicate for it a separate machine.
Download Apache
Unzip the downloaded file into D: drive and edit httpd.conf under conf folder through setting SRVROOT variable as the following:
To install Apache, run cmd as administrator and change your location to D:\Apache24\bin and execute the following command:
Now that Apache web server is successfully installed on your machine.
To start Apache, run as administrator the following command under D:\Apache24\bin:
By default apache runs on port 80, to check if it starts successfully, try localhost on any browser and you’ll get the following screen:
Apache runs successfully

 3. Download mod_jk module

Download mod_jk module from here, this plugin is responsible for the communication between Apache and tomcat.
Download mod_jk module
Extract mod_jk zip file and copy mod_jk.so into D:Apache24\modules.

4. Configure Apache load balancer

Edit D:Apahe24\conf\http.conf as below:
  • add the following at the end of LoadModule directives section:
  • add the following just before the end of the file:
Create workers.properties file under conf directory, this file defines the different tomcat instances to be load balanced, in addition to a load balance worker which manage the load balancing process. Below is the content of worker.properties:
  • worker.<workerName>.port: Defines the ajp port which tomcat instance runs onto.
  • worker.<workerName>.host: The ip address or host name of the tomcat machine.
  • worker.<workerName>.type: The type of worker, in this example we use 2 types of workers
    • ajp13: This worker communicates directly with a tomcat instance.
    • lb: a load balancer worker which manage and load balance several ajp13 workers.
  • worker.<workerName>.lbfactor: Define the strength of the tomcat node, the load balancer worker will take this value into consideration when forwarding requests to tomcat nodes.
  • worker.loadbalancer.balance_workers: Define the name of ajp13 workers to be managed by the load balancer.

5. Configure tomcat instances

Modify the startup configuration of each tomcat instance in order to match the corresponding worker properties in Apache.
Edit server.xml under TOMCAT_HOME/conf for tomcat1 as the following:
Edit server.xml under TOMCAT_HOME/conf for tomcat2 as the following:
After restarting tomcat nodes and Apache server, the load balance is set up successfully and Apache is ready to consume requests and distribute them on tomcat1 and tomcat2.
P.S The application is now accessed through Apache host name and port.
    Blogger Comment
    Facebook Comment