Load Balancing Apache Tomcat using IIS

December 8 2011

9 comments 0
motif

Front-ending Apache Tomcat with Apache Web Server or is sometimes thought to improve performance. However, performance of standalone has already been known to be very good. So why add or Apache web server in front of it? – the answer is scalability and maintenance. Front-ending Tomcat with such web servers allows you to add more instances in case of increased load and also bring down instances for maintenance/upgrades.

This blog shows you end-to-end, step-by-step detailed instructions on how to setup such an environment. It should take you about an hour to configure this setup for yourself. Follow the instructions slowly and carefully to make sure you do not skip/miss any step. Here is a higher level view of the setup.

Environment: Windows 7 Professional, , IIS 7.5, Java SE 6 Update 29

You need to perform the following steps:-

  1. Install Java
  2. Install IIS
  3. Install Tomcat
  4. Configure Tomcat
  5. Configure IIS
  6. Create and deploy a web application
  7. Test

Install Java

You will be installing Java SE 6 Update 29. Download the JDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html. Once downloaded, double-click on the exe file to kick-start the installation. In the installation wizard, accept the defaults and finish the installation.

You now need to set the JAVA_HOME environment variable to point to the JDK installation directory.

  1. Open Windows Explorer
  2. Right-click on Computer and select Properties. This opens the System Properties window. Click on the Advanced tab and then click on Environment Variables
  3. In the section User variables for Administrator, click on the New button. Enter JAVA_HOME for Variable name:. For Variable value, enter the absolute path of the directory where the JDK is installed. For example, on my machine it is installed at C:\Program Files\Java\jdk1.6.0_29

Install IIS
Installing IIS is fairly straightforward. Apart from IIS, you will also install some extra components, which you will see shortly.

  1. Go to the Control Panel and click on Programs and Features. Click on Turn Windows features on or off.
  2. In the Windows Features window, check Internet Information Services and Microsoft .NET Framework 3.5.1.
  3. Expand Internet Information Services and under World Wide Web Services – Application Development Features, check ISAPI Extensions  and  ISAPI Filters.
  4. Click OK to finish installation.

Install Tomcat 7

  1. Download Tomcat 7 from http://tomcat.apache.org/download-70.cgi. Download the 64-bit Windows Zip file.
  2. Unzip the file to your c:\ drive and a directory named apache-tomcat-7.0.xx will be created, where xx is the minor version number.
  3.  Rename that directory to tomcat.
  4. This finishes your Tomcat installation.

In order to distribute load across multiple instances of Tomcat, we need to create two more instances of Tomcat.

  1. Create two directories named tomcat1 and tomcat2, directly under c: drive.
  2. Within both these directories, create the following sub-directories – bin, conf, logs, webapps, temp, work.
  3. From c:\tomcat\conf, copy server.xml, web.xml, context.xml and tomcat-users.xml, and paste them into the conf directories of tomcat1 and tomcat2
  4. Copy startup.bat and shutdown.bat from c:\tomcat\bin and paste them in the bin directories of c:\tomcat1\bin and c:\tomcat2\bin
  5. Edit startup.bat and shutdown.bat of tomcat1 and at the very top of these files, add the following lines:

set CATALINA_HOME=c:\tomcat
set CATALINA_BASE=c:\tomcat1

Repeat step 5 for tomcat2 with the exception that CATALINA_BASE should be set to c:\tomcat2

Configure Tomcat
Lets go through some concepts/terminology before we begin configuration.

A connector in Tomcat is something which allows a client to connect to Tomcat. Tomcat ships will multiple connectors e.g. HTTP, AJP etc. By default, the HTTP connector is configured to listen on port 8080. In our case, the IIS server will act as a client to Tomcat. IIS web server will communicate with Tomcat using the AJP protocol. We need to ensure that we have the ports for the AJP connector configured. We also want all requests to be routed through IIS and do not want anybody to bypass it, i.e we do not want anybody to access the web application through port 8080 using the HTTP connector. Hence, we will disable (comment) the HTTP connector from our configuration.

A worker is simply an instance of Tomcat to which IIS will dispatch requests. In this case, all three Tomcat instances will be workers. Each worker needs to have a unique id, which will be assigned to it using the jvmRoute attribute of the <Engine> element in server.xml. You will see later how and where this id is used by IIS server. Lets start configuring the Tomcat instances:

  • Change Shutdown ports:- All three Tomcat instances will reside on the same machine, hence we need to ensure that ports do not conflict. Shutdown ports are where Tomcat listens for the shutdown command. Open server.xml of each tomcat1, locate the <Server > element start tag and change the port number to 8006. Repeat this step for tomcat2 and change the port number to 8007.
  • Change AJP ports :- In server.xml of tomcat1, locate the <Connector> element whose protocol=”AJP/1.3″ and change the port number to 8010. Similarly for tomcat2, change the port number to 8011
  • Disable HTTP Connectors:- In the server.xml of each Tomcat instance, locate the <Connector> element shown below and either delete it or comment it out

    <Connector port=”8080″ protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”8443″ />

  • Assign worker ids:- In server.xml of tomcat, locate the <Engine> element and add the following attribute to its start tag jvmRoute=”worker0″ (the value of the attribute is workerZERO and not workerOH). Repeat this step for tomcat1 and assign the value of worker1. Similarly repeat for tomcat2 and assign the value of worker2.

Configure IIS
You will now configure ISAPI redirector plug-in provided by Tomcat. The redirector requires 4 entities:-

  • isapi_redirect.dll : This is the IIS plugin which you will download later
  • worker.properties: A file that describes the hosts and ports of workers (Tomcat instances/processes)
  • uriworkermap.properties: A file that maps URL patterns to workers. Allows you to specify as to which worker will handle the matching URL path request
  • isapi_redirect.properties: A file which tells IIS the location of all of the above files.

Lets start the configuration. After you installed IIS, it would have created a directory named c:\inetpub. All configuration will be done in the c:\inetpub directory.

  1. Under c:\inetpub, create a subdirectory called isapi. Under c:\inetpub\isapi, create three subdirectories – bin, logs, conf.
  2. To download the isapi_redirect.dll, go to  http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/ and download the zip appropriate for your system i.e. 32-bit or 64-bit.
  3. Unzip the downloaded file and copy isapi_redirect.dll and paste it under c:\inetpub\isapi\bin
  4. Inside c:\inetpub\isapi\conf, create a file named worker.properties. In this file you will create workers, their host and the port on which they will communicate with IIS. Workers are of different types e.g. ajp13 is a worker which communicates with web server  i.e. tomcat instances , lb is the worker which will act as the load balancer, stats worker type will give you load balancing statistics and status information. You will be adding key-value pairs, each key must begin with the word worker. The complete worker.properties file is shown below. Copy/paste the below to your worker.properties file and save it. Explanation of each line is given above the line in a comment
  5. Inside c:\inetpub\isapi\conf, create a file named uriworkermap.properties. In this file you will map the workers to the URL path patterns. This will tell IIS, which worker will handle a request for a given URL path. As you can see below, the /foo web application is mapped to the load balancer worker, which the /jkstatus path is mapped to the stats worker.
  6. Inside c:\inetpub\isapi\bin, create a file named isapi_redirect.properties. This file will tell IIS, the location of isapi_redirect.dll, worker.properties, uriworkermap.properties and the location of the log file for the redirector plugin. You can copy/paste contents from the file as shown below.

The remainder of the configuration is through the IIS Manager:

  1. To open the IIS Manager, open  Administrative Tools from the Control Panel and then click on Internet Information Services (IIS) Manager
  2. You will now create a Virtual Directory named tomcat. Under Connections, navigate to [[Root Node]] > Sites > Default Web Site. Right-click on Default Web Site  and select Add Virtual Directory. For Alias enter tomcat, and for Physical Path, enter C:\inetpub\isapi\bin. Click OK to close the Edit Virtual Directory window.
  3. To configure Handler Mappings, under Default Web Site, select tomcat. In the Features View pane, double-click on Handler Mappings and then select ISAPI-dll, which should be disabled at the moment. Now in the Actions pane, select Edit feature permissions. In the Edit Feature Permissions window, check the Execute option and click OK to close the window. ISAPI-dll should now be enabled.
  4. You now need to configure the ISAPI Filter and specify the location of the dll. Select Default Web Site . In the Features View pane, double-click on ISAPI Filters. From the Actions pane, click on Add. For Filter Name, enter isapi_redirect. For Executable, enter the absolute path of the location of the isapi_redirect.dll — C:\inetpub\isapi\bin\isapi_redirect.dll.
  5. Finally, you have to now enable the ISAPI extension which you have configured above. In the Connections pane, click on the root node. In the Features View pane, double-click on ISAPI and CGI Restrictions. In the Actions pane, click on Add. This will open the Edit ISAPI or CGI Restrictions window. For the ISAPI or CGI Path field, enter the absolute path of isapi_redirect.dll — C:\inetpub\isapi\bin\isapi_redirect.dll. In the Description field, enter isapi_redirect. Also make sure to check the option Allow extension path to execute. Click OK to close this window.
  6. This step  is only for those who have downloaded a 32 bit version of isapi_redirect.dll, but have a 64 bit operating system. If you already downloaded the 64 bit version, then please ignore this step. Click on [[Root Node]] > Application Pools. In the Features View pane, select DefaultAppPool and then click on Advanced Settings… in the Actions pane. In the Advanced Settings window, under the (General) section, set Enable 32-Bit Applications to true. Click OK to close the Advanced Settings window.

Create and deploy a web application
You will now create a web application named foo and deploy it to all the Tomcat instances. You can perform the following steps with Notepad, no need to fire up an IDE. You will be able to copy/paste most of the stuff.

  1. In c:\tomcat\webapps, create a directory named foo. Within this directory, create a subdirectory named WEB-INF. Within WEB-INF, create two sub-directories named classes and lib.
  2. Within the foo directory, create a file named index.jsp. This JSP will simply display the Session ID and Session Creation Time . You can copy the code snippet (below) into the JSP.
  3. Copy the foo directory (and all of its contents) into c:\tomcat1\webapps and c:\tomcat2\webapps

Test
Its now time to test your setup.

  1. Start each tomcat instance, but double-clicking on startup.bat in the bin directories of each tomcat.
  2. To start IIS,  go to IIS Manager and Start or Restart (whichever is applicable) IIS from the Actions pane.
  3. Open a browser and navigate to http://localhost/foo. You should see the output from index.jsp. Notice how the value of jvmRoute attribute has been suffixed to the session id. This tells you the tomcat instance serving this request
  4. Open another browser and navigate to http://localhost/foo, you should now see a different tomcat instance servicing the request. Try it with a third browser and you should see the response from the third instance.

Congratulations, you have configured load balancing using Tomcat 7 and IIS 7.5 !

You’ve had to edit configuration files for different Tomcat instances. This requires you to switch between those instances frequently. MuleSoft Tcat – Enterprise Tomcat , has the capability to provide you with a unified front-end for all your Tomcat instances. You would have been able to perform operations (startup and shutdown) and edit configurations of multiple Tomcat instances, all from the Administration Console.


We'd love to hear your opinion on this post

9 Responses to “Load Balancing Apache Tomcat using IIS”

  1. I’m running tomcat w/o a front end.

    Do you have Pros/Cons documentation on running TC w/ and w/o IIS front end? Looking for topics such as performance and stability.

    Putting this page as a video is highly recommended.

    Agree(0)Disagree(0)Comment
  2. […] are two links that are very helpful: /load-balancing-tomcat-7-using-iis-7-5/ […]

    Agree(0)Disagree(0)Comment
  3. Hi,

    This guide is excellent and very use full for me as a basic tomcat user. I need to run my server in “HTTPS”. I am not clear about installing ssl certificate.(whether ssl certificate to be installed on iis or tomcat) .Please post any guide for this need.

    Thanks
    Saravana K

    Agree(0)Disagree(0)Comment
  4. I don’t even know how I ended up here, but
    I thought this post was good. I don’t know who you are but definitely you are going
    to a famous blogger if you aren’t already
    😉 Cheers!

    Agree(0)Disagree(0)Comment
  5. Is there a way to connect Apache Webserver to Mule using AJP

    Agree(0)Disagree(0)Comment
  6. thanks

    Agree(0)Disagree(0)Comment
  7. Thanks for this awesome write up! It says the workers.properties file is available below, but I am not seeing where that is. Any chance you could update the post to contain the contents of that file. I think that’s the last thing I need to get this up and running!

    Agree(0)Disagree(0)Comment
  8. Tim,

    The contents of the file are shown in the post itself. Here is the link to the actual file – https://gist.github.com/karanmalhi/1440048#file-worker-properties

    Agree(0)Disagree(0)Comment