Load Balancing Using Apache with Tomcat Servers

General discussion about PLUC and Linux in Pakistan.

Load Balancing Using Apache with Tomcat Servers

Postby kbukhari » Wed Jan 31, 2007 3:08 pm

For better experience see
We will cover Three Steps


1. Running Multiple Tomcat Servers and One Apache as a Request Dispatcher to the available server.
2. Having a sticky session maintenance.
3. Making Clusters of Tomcat Servers so that they can share session also.

Step: 1 Running Multiple Tomcat Servers and One Apache as a Request Dispatcher to the available server.


it will follow 6 steps

1. Install Apache2
2. Load mod_jk2
3. Install Tomcat5 (Same Version) Instances
4. Configure JK2
5. Make a sample application
6. Test Scenario

1. Install Apache2

First of all Install Apache2 and run it.
After installation To Configure Apache2 go to the …/Apache2/conf/httpd.conf File and read it carefully to update server settings. It has some sections in it , Go to the

Code: Select all

[Section 1: Global Environment]


Here you will find Listen Entry it is by default [Listen:80] Change it to your requirement where to install Apache be running.
e.g Listen localhost:8083

If you will have installed Apache2 as a service you will find Service Monitor running near time bar. Otherwise you can install Apache2 as a service

Code: Select all

Apache –k install

If the service monitor is not running then start it from Apache2/bin and handle to start and stop Apache2 service.
Till now it is assumed that Apache2 is successfully running on the machine. Test it from browser.

2. Load mod_jk2

Download mode_jk2 binaries (jakarta-tomcat-connectors-jk2.0.4-win32-apache2.0.49) on your local drive and extract it you will find a modules directory copy mod_jk2 module from modules directory and place it in Apache2/modules directory. And write the following line in Apache2/cong/http2.conf File

Code: Select all

LoadModule jk2_module modules/mod_jk2.so

It is also in [Section 1: Global Environment]

3. Install Tomcat5 Instances


Install 2 or 3 (Tomcat1, Tomcat2, Tomcat3) instances of Tomcat5 on different ports. To change the required ports and to do other settings follow these steps.

a.Go to Tomcat1/conf/server.xml
b.This port should be different from all so arrange any 3 ports like 8005, 8006, 8007 for Tomcat1, Tomcat2 and Tomcat3 respectively.

Code: Select all

<Server port="8005" shutdown="SHUTDOWN">

c.This is http port it must be different also let we start from 8091, to 8093 for all three servers respectively.

Code: Select all

 <Connector port="8091" maxHttpHeaderSize="8192"                   
       maxThreads="150" minSpareThreads="25" maxSpareThreads="75"         
       enableLookups="false" redirectPort="8443" acceptCount="100"               
       connectionTimeout="20000" disableUploadTimeout="true" />

d.Now set the port and protocol for AJP 1.3 Connector this should also be different let here we start from 8009, to 8011 for all three servers.

Code: Select all

  <!-- Define an AJP 1.3 Connector on port 8009 -->
          <Connector port="8009" enableLookups="false"
           redirectPort="8443" protocol="AJP/1.3" />

e.Now finally set jvmRoute for all the servers. Like server1 , server2 and server3.

Code: Select all

 <!-- You should set jvmRoute to support load-balancing via AJP 
         ie :-->
    <Engine name="Standalone" defaultHost="localhost"
    jvmRoute="server1">         

Till now it is done that all three servers are running successfully on defined ports. Try them also by accessing from browser one by one.


4. Configure JK2


Create a new file workers2.properties in Apache2/conf directory and write the following contents in it…

Code: Select all

[shm:]
info=Shared memory
file=anonymous

[channel.socket:server1]
host=127.0.0.1
port=8009
tomcatId=server1
ver=0

[channel.socket:server2]
host=127.0.0.1
port=8010
tomcatId=server2
ver=0

[channel.socket:server3]
host=127.0.0.1
port=8011
tomcatId=server3
ver=0
[lb:lb]
info=Load balancer
ver=1
sticky=0

[uri:/test/*]
info=Examples Web application
group=lb:lb
ver=1

[status:status]
info=Status worker, displays runtime informations

[uri:/jkstatus/*]
info=Display status information and checks the config file for changes.
group=status:status


[shm:] is used to define a shared memory it is specified to have a status of servers will be discussed later

[channel.socket:server1]
host=127.0.0.1
port=8009
tomcatId=server1
ver=0

is used to add server. server1 is the jvmRoute that we have defined in server.xml file. 8009 port is the port that we have defined for AJP 1.3
ver we will discuss later if required…


[lb:lb]
info=Load balancer
ver=0
sticky=0

We define the load balancer by this property lb:loadbalancername
We will discuss sticky later


[uri:/test/*]
info=Examples Web application
group=lb:lb
ver=1

Using this property we define the context uri in our tomcat servers to have random requests.
Other two are not necessary to be explained now.


5. Make A Sample Application
As we have defined in context uri make a test application having only one servlet in it ActionServlet ..

Code: Select all

ackage com.jmobiles.loadbalancing;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class ActionServlet extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html";

  //Initialize global variables
  public void init() throws ServletException {
  }

  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
  ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    HttpSession session = request.getSession();
   
    System.out.println("From Server 1");
    out.println("<html>");
    out.println("<head><title>ActionServlet</title></head>");
    out.println("<body bgcolor=\"#ffffff\">");

    if(session.getAttribute("USER") == null){
      out.print("<h2>Session Not Set</h2><br/>");
      session.setAttribute("USER", "hello");
    }else{
      out.print("<h2>Session Set ["+session.getAttribute("USER")+"] </h2><br/>");
    }
 
    out.println("<p>I am from Server 1 ....................</p>");
    out.println("</body></html>");
  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
  ServletException, IOException {
    doGet(request, response);
  }

  //Clean up resources
  public void destroy() {
  }
}

Map this servlet in web.xml as

Code: Select all

   <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>com.jmobiles.loadbalancing.ActionServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>/ac</url-pattern>
    </servlet-mapping>

Change the message for remaining servers and test individually that the sample application is running on all three servers. Like for Tomcat1 test
http://localhost:8091/test/ac
it will display messages for Server 1 on console of the server and also on the browser.
6. Test Scenario
Start Apache2 service and all three tomcat servers Tomcat1, Tomcat2, Tomcat3. And access the url
http://localhost:8083/test/ac
You will see it will first show messages for Server1 then for Server2 and then for Server3 and it will continue in a round robin fashion on consecutive requests.

Step: 2 Having sticky session maintenanace

JK2 has two methods for request dispatching

1.Round Robin Fashion
2.Sticky Fashion

We have seen the Round Robin Fashion. First of all we see that what is a sticky fashion?
Sticky fashion means Apache once found an available server will redirect all the requests to that server during that session. So the session can be maintained in this way but if that server is down then Apache will find the other available server and will stick to that until it is dead. But switching the server the session will be lost.

To achieve this we have to change the load balancer property from workers2.properties as by adding the variable sticky=1 by default the value of sticky is 1.

Step: 3 Making Clusters of Tomcat Servers so that they can share session als
Making Clusters of the servers it is possible to share the session anyway if one goes down other is up and vise versa running at least one at a time.
To enable this behaviour follow the steps defined below

1.Go to the server.xml and uncomment the Cluster tag and keep in mind to have different Reciever tcpListenPort like in this case start from 4001 –to- 4003 for all three servers.
2.Now go to the web.xml of the test application and add
<distributable /> after the display name tag.
3.Restart the servers you will see on console that they will find each other to have a shared session
4.Now again test by accessing the URL
http://localhost:8083/test/ac
Test it by stopping restarting different servers it will never loose the session
value until all the servers are dead.

Note: It is necessary to have serialized objects in session because they have to travel on network in this case.
[/url]
--
Syed Kashif Ali Bukhari
+92-345-8444420
http://sysadminsline.com
http://kashifbukhari.com
kbukhari
Major General
 
Posts: 1222
Joined: Sat Dec 31, 2005 12:29 am
Website: http://kashifbukhari.com
Location: Lahore

Re: Load Balancing Using Apache with Tomcat Servers

Postby lambda » Wed Jan 31, 2007 3:13 pm

how is that any better? it looks like the same information you've posted here.
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby kbukhari » Wed Jan 31, 2007 3:17 pm

it is short length code is not expanded
--

Syed Kashif Ali Bukhari

+92-345-8444420

http://sysadminsline.com

http://kashifbukhari.com
kbukhari
Major General
 
Posts: 1222
Joined: Sat Dec 31, 2005 12:29 am
Website: http://kashifbukhari.com
Location: Lahore

Postby nomankhn » Fri Feb 02, 2007 11:51 am

kbukhari wrote:it is short length code is not expanded


Dear kashif

Thanks for sharing info.

Regards
Noman Liaquat Khanzada Rajput
Linux means productivity and fun.
We all love Linux, but it's also a fact that some people might not be able to migrate.
nomankhn
Colonel
 
Posts: 714
Joined: Wed Aug 07, 2002 8:00 pm

Postby nayyares » Wed Feb 14, 2007 12:12 am

Hello,

Nice try, but i have seen this few years back and it took me some time to search my notebook browser book marks :)

http://www.linuxjournal.com/article/8561

thanks
Last edited by nayyares on Wed Feb 14, 2007 12:27 am, edited 1 time in total.
Nayyar Ahmad
RHCE, CCNA, OCP DBA
nayyares aT fedoraproject DoT org
blogs: nayyares.blogspot.com
nayyares
Battalion Quarter Master Havaldaar
 
Posts: 237
Joined: Tue Dec 13, 2005 10:47 pm
Website: http://fedoraproject.org/wiki/NayyarAhmad
WLM: nayyares@hotmail.com
Location: JNB, SA

Postby masud » Wed Feb 14, 2007 12:23 am

nayyares wrote:Hello,

Nice try, but i have seen this few years back and took some time to watch my notebook browser book marks :)

http://www.linuxjournal.com/article/8561

thanks


It's ok we know him, it's not the first time.
--SP--
masud
Havaldaar
 
Posts: 108
Joined: Thu Aug 05, 2004 12:15 am
Website: http://fedoraproject.org/wiki/MasoodMehmood
WLM: silentplayer@internet-criminals.com
Yahoo Messenger: xlx_silentplayer_xlx
Location: Fremont, CA

Postby kbukhari » Wed Feb 14, 2007 9:36 am

masud wrote:
nayyares wrote:Hello,

Nice try, but i have seen this few years back and took some time to watch my notebook browser book marks :)

http://www.linuxjournal.com/article/8561

thanks


It's ok we know him, it's not the first time.



my child see the configurations in both
--

Syed Kashif Ali Bukhari

+92-345-8444420

http://sysadminsline.com

http://kashifbukhari.com
kbukhari
Major General
 
Posts: 1222
Joined: Sat Dec 31, 2005 12:29 am
Website: http://kashifbukhari.com
Location: Lahore

Postby kbukhari » Wed Feb 14, 2007 9:55 am

see my workers.properties
[shm:]
info=Shared memory
file=anonymous

[channel.socket:server1]
host=127.0.0.1
port=8009
tomcatId=server1
ver=0

[channel.socket:server2]
host=127.0.0.1
port=8010
tomcatId=server2
ver=0

[channel.socket:server3]
host=127.0.0.1
port=8011
tomcatId=server3
ver=0
[lb:lb]
info=Load balancer
ver=1
sticky=0

[uri:/test/*]
info=Examples Web application
group=lb:lb
ver=1

[status:status]
info=Status worker, displays runtime informations

[uri:/jkstatus/*]
info=Display status information and checks the config file for changes.
group=status:status



and one is used in you sended link
worker.list=worker1,worker2,worker3
# Set properties for worker1
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
# Set properties for worker2
worker.worker2.type=ajp13
worker.worker2.host=localhost
worker.worker2.port=8109
# Set properties for worker3
worker.worker3.type=ajp13
worker.worker3.host=localhost
worker.worker3.port=8209


ik andhy ko bhi farak nazar atta hay lakin sar kamran k student ko nahi
i was just pointing that if a teacher is getting help from a hand book duruing teaching you then what you will say him
yes i do googling and most of the time a copy and past the searching in my configuration i dont try to teach those things which i realy dont know
i am 100% sure your Sir kamran wont be able to to configure full qmail with out qmailrocks.com
--

Syed Kashif Ali Bukhari

+92-345-8444420

http://sysadminsline.com

http://kashifbukhari.com
kbukhari
Major General
 
Posts: 1222
Joined: Sat Dec 31, 2005 12:29 am
Website: http://kashifbukhari.com
Location: Lahore

Postby nayyares » Wed Feb 14, 2007 12:02 pm

Hello Mr. Bukhari:

So this is the only difference you have between two howtos :) ok let me give you the clear picture that you when wrote this howto, have abuse many posting not one :)

here is the rest of your s***:

http://www.searchfull.net/blog/2006/11/ ... 04402.html

read this one clear fully, btw, i will paste a part of his blog to show what you call a difference and your R&D is written by some body in his blog, means the actual author took it so casual that even he wrote that thing in his blog :)

[shm:]
info=Shared memory
file=anonymous
[channel.socket:server1]
host=127.0.0.1
port=8009
tomcatId=server1
[channel.socket:server2]
host=127.0.0.1
port=8010
tomcatId=server2
ver=0
[lb:lb]
info=Load balancer
sticky=1
ver=0
[uri:/servlets-examples/*]
info=Examples Web application
group=lb:lb
ver=1
[status:status]
info=Status worker, displays runtime informations
[uri:/jkstatus/*]
info=Display status information and checks the config file for changes.
group=status:status


I think Mr. Bukhari that you now reliase that you are in wrong direction, i will advice you to stop insulting your self in front of whole Paksitan and rest of the world readers, and accept that you have made wrong statments and you should feel sorry for that.

Btw, i tell you one thing more that I am proud to be Mr. Kamran student and i am even feeling more pride to give you better treatment. :)

cheers
thanks
Nayyar Ahmad

RHCE, CCNA, OCP DBA

nayyares aT fedoraproject DoT org

blogs: nayyares.blogspot.com
nayyares
Battalion Quarter Master Havaldaar
 
Posts: 237
Joined: Tue Dec 13, 2005 10:47 pm
Website: http://fedoraproject.org/wiki/NayyarAhmad
WLM: nayyares@hotmail.com
Location: JNB, SA

Postby kbukhari » Wed Feb 14, 2007 12:28 pm

nayyares wrote:Hello Mr. Bukhari:

So this is the only difference you have between two howtos :) ok let me give you the clear picture that you when wrote this howto, have abuse many posting not one :)

here is the rest of your s***:

http://www.searchfull.net/blog/2006/11/ ... 04402.html

read this one clear fully, btw, i will paste a part of his blog to show what you call a difference and your R&D is written by some body in his blog, means the actual author took it so casual that even he wrote that thing in his blog :)

[shm:]
info=Shared memory
file=anonymous
[channel.socket:server1]
host=127.0.0.1
port=8009
tomcatId=server1
[channel.socket:server2]
host=127.0.0.1
port=8010
tomcatId=server2
ver=0
[lb:lb]
info=Load balancer
sticky=1
ver=0
[uri:/servlets-examples/*]
info=Examples Web application
group=lb:lb
ver=1
[status:status]
info=Status worker, displays runtime informations
[uri:/jkstatus/*]
info=Display status information and checks the config file for changes.
group=status:status


I think Mr. Bukhari that you now reliase that you are in wrong direction, i will advice you to stop insulting your self in front of whole Paksitan and rest of the world readers, and accept that you have made wrong statments and you should feel sorry for that.

Btw, i tell you one thing more that I am proud to be Mr. Kamran student and i am even feeling more pride to give you better treatment. :)

cheers
thanks

do u know ? Graham King, he is working in go internet UK
he is the source from i learn the tomcat. and other java stuff he also teach me how to run java application on open/net/freeBSD
i know him personally and and respect him u can also find him on apache mailling list
--

Syed Kashif Ali Bukhari

+92-345-8444420

http://sysadminsline.com

http://kashifbukhari.com
kbukhari
Major General
 
Posts: 1222
Joined: Sat Dec 31, 2005 12:29 am
Website: http://kashifbukhari.com
Location: Lahore

Postby nayyares » Wed Feb 14, 2007 12:47 pm

Here You wrote two things, one is good and one is even more good.

do u know ? Graham King, he is working in go internet UK
he is the source from i learn the tomcat. and other java stuff he also teach me how to run java application on open/net/freeBSD
i know him personally and and respect him u can also find him on apache mailling list


No 1. (Good Thing) So now atleast you have personally accepted that you are a coppier, you have written somebody else work in your howto.

No 2. (More Good Thing) you know Graham King and still you have abuse his work by saying that you did all by your self, so now i am going to email him your howto link and i hope he will join the team
Nayyar Ahmad

RHCE, CCNA, OCP DBA

nayyares aT fedoraproject DoT org

blogs: nayyares.blogspot.com
nayyares
Battalion Quarter Master Havaldaar
 
Posts: 237
Joined: Tue Dec 13, 2005 10:47 pm
Website: http://fedoraproject.org/wiki/NayyarAhmad
WLM: nayyares@hotmail.com
Location: JNB, SA


Return to “%s” General

Who is online

Users browsing this forum: No registered users and 1 guest

cron