Tomcat Server Load-Balancing Using Multiple Tomcat Server Instances in Ubuntu

Hello, in here I’m going to describe how to configure load balancing among multiple Apache tomcat servers, before write this article I searched on web and found many solutions but nothing worked correctly, getting some errors. So then somehow I managed to set up tomcat server load balancing. So here are the steps that I followed to configure load balancing on tomcat servers.

Before configure multiple server load balancing, you have to install and configure multiple tomcat server instances correctly, that has been described on my previous blog post about “How to configure multiple tomcat server instances”

After completing setup multiple tomcat server instances, let go through via those steps.

  1. install apache tomcat server with libapache
    $ sudo apt-get install apache2 libapache2-mod-jk
  2. open /etc/libapache2-mod-jk/workers.properties and edit the file with following configurations.
    $ sudo nano /etc/libapache2-mod-jk/workers.properties
    
    workers.tomcat_home=/usr/share/tomcat7
    workers.java_home=/usr/lib/jvm/java-7-openjdk-amd64
    ps=/
    worker.list=loadbalancer
    worker.worker_a.port=8010
    worker.worker_a.host=localhost
    worker.worker_a.type=ajp13
    
    worker.worker_b.port=8011
    worker.worker_b.host=localhost
    worker.worker_b.type=ajp13
    
    worker.worker_c.port=8012
    worker.worker_c.host=localhost
    worker.worker_c.type=ajp13
    
    worker.worker_a.lbfactor=1
    worker.worker_b.lbfactor=1
    worker.worker_c.lbfactor=1
    worker.loadbalancer.type=lb
    worker.loadbalancer.balance_workers=worker1,worker2,worker3
    worker.loadbalacer.sticky_session=1
    worker.status.type=status
  3. Go to the “/etc/apache2/mods-available/jk.conf” and change the necessary configuration.
    JkMount /*.cfm loadbalancer
    JkMount /*.cfc loadbalancer
    JkMount /*.do loadbalancer
    JkMount /*.jsp loadbalancer
    JkMount /*.cfchart loadbalancer
    JkMount /*.cfm/* loadbalancer
    JkMount /*.cfml/* loadbalancer
    
    JkMountCopy all
    JkLogFile /var/log/apache2/mod_jk.log
  4. Go to the /etc/apache2/sites-available/ and copy the default file to the same location and edit for following changes.
    $ sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/loadbalance.com
    <VirtualHost *:80>
      ServerAdmin admin@mysite.com
      DocumentRoot "/var/www/mysite/"
      DirectoryIndex index.cfm index.html
      ServerName mysite.com
      ServerAlias www.mysite.com
      ErrorLog ${APACHE_LOG_DIR}/mysite.error.log
      CustomLog ${APACHE_LOG_DIR}/mysite.access.log Combined
    </VirtualHost>
  5. Then enable the tomcat default instances.
    $ sudo a2ensiteloadbalance.com
    $ sudo service apache2 restart
  6. Then go to the tomcat server.xml file in each instances are available. And add the “jvmRoute” for each server.xml files.
    $ sudo nano .../conf/server.xml
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="worker_a">
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="worker_b">
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="worker_c">
  7. Now, Tomcat server load-balancing is done. Try to load the URL : http://localhost , then You will see the tomcat home page. In here You can see load-balancing and session management using sample java application.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s