Server Load balancer and cluster configuration for Apache + Tomcat

Source: Internet
Author: User
Tags http redirect ldap

 

Server Load balancer and cluster configuration for Apache + Tomcat

Experiment topology:

I. Matching Environment

(1 ). Tomcat installation and configuration

Tomcat_a's IP Address: 192.168.55.229

Tomcat_ B IP: 192.168.55.231

Tomcat installation of JDK and tomcat package (such as apache-tomcat-6.0.30.zip and jdk-6u5-linux-x64.bin)

For installation and configuration of Tomcat, refer to my blog:

Http://blog.csdn.net/zhuying_linux/article/details/6583096

 

Test: Access http: // 192.168.55.229: 8080 and http: // 192.168.55.231: 8080 respectively.

If Tom appears, it indicates the operation is successful.

 

(2 ). Apache installation: (source code compilation and installation)

Apache address: 192.168.50.50

# Tar xzvf httpd-2.2.17.tar.gz

# Cd httpd-2.2.17

#./Configure -- With-LDAP -- enable-mod-shared = "all SSL ldap cache proxy authn_alias mem_cache file_cache authnz_ldap charset_lite dav_lock disk_cache"[This option is used to compile all modules]

1. Test

/Usr/local/apache2/CONF/httpd. conf

Servername localhost

# Cd/usr/local/apache2/bin

#./Apachectl start

The output is similar to httpd (PID 17040) already running.

Access address: http: // 192.168.50.50

It works! Hehe...

It indicates that the operation is successful ~ \ (Too many rows )/~ La la ,(~ O ~)~ Zz

 

II. (1)Mod_proxy Server Load balancer Configuration

1. Load the proxy Module

All the Agent modules to be loaded are mod_proxy.so, mod_proxy.ajp.so, mod_proxy.http.so, mod_proxy.ftp.so, mod_proxy.connect.so, mod_proxy.balancer.so

Because all modules have been loaded, you do not need to modify the configuration, that is, the following command in the/usr/local/apache2/CONF/httpd. conf file:

Loadmodule proxy_module modules/mod_proxy.so

Loadmodule proxy_ftp_module modules/mod_proxy_ftp.so

Loadmodule proxy_http_module modules/mod_proxy_http.so

Loadmodule proxy_ajp_module modules/mod_proxy_ajp.so

Loadmodule proxy_balancer_module modules/mod_proxy_balancer.so

2. Configure the virtual host proxybalancer

AddInclude CONF/app1.conf

Add the following content to CONF/app1.conf:

Listen 8051.
<Virtualhost *: 8051>

Serveradmin angel.zhu@99bill.com# Specify the server administrator's E-MAIL address, which the server automatically reports errors

Servername localhost# Host name searched by the Web Client

Errorlog "/opt/apache_log/apppolicerror.log"

Customlog "/opt/apache_log/app1_access.log"Common

Proxypass/balancer: // cluster/stickysession = JSESSIONID | JSESSIONID nofailover = on lbmethod = byrequests timeout = 5 maxattempts = 3

#Balancer: the method used to copy sessions, includingJSESSIONIDOrPHPSESSIONID
Nofailover: On indicates that the session is interrupted when the worker fails or stops. It is set to on when the backend server does not support session replication. lbmethod: select the scheduling algorithm of the load. The default value isbyrequestsIndicates round robin scheduling ),bytrafficIndicates weighted scheduling.LoadfactorSpecify the weight.

Proxypassreverse/Balancer: // cluster/
# This command enables Apache to adjust the URL in the location, content-location, and Uri header in the HTTP redirect response. This avoids the problem of bypassing reverse proxy caused by HTTP redirection of backend servers when Apache is used as a reverse proxy.

# The proxyrequests directive shoshould usually be set off when using proxypass.

Proxyrequests off# Forward proxy not allowed

Proxypreservehost on # When enabled, this option will pass the "host:" line of the incoming request to the host to be proxy, rather than the host name specified in proxypass.

<Proxy balancer: // cluster>

Balancermember AJP: // 192.168.55.229: 8009 route =Tomcat_a

Balancermember AJP: // 192.168.55.231: 8009 route =Tomcat_ B

# The rout value is appended to the session ID.

</Proxy>

</Virtualhost>

At this time, when you access http: // 192.168.50.50: 8051, Tom appears, indicating that Apache has directed access from Port 8051 to the Tomcat server. However, it cannot be seen which Tomcat server is used. The test page is added to the two Tomcat servers to make the effect more obvious.

2. Add the file/usr/local/Apache-Tomcat-6.0.30/webapps/root/to tomcat_a/Test. jspThe content is <font color = Red> aaaaaaaaaaaaaaaa </font> <B>

 

Add the file on the other tomcat_ B:

/Usr/local/Apache-Tomcat-6.0.30/webapps/root/Test. jsp, Content is

<Font color = blue> bbbbbbbbbbbbbbbbbbb </font> <B>

 

3. Modify the server. xml file <engine name = "Catalina" defaulthost = "localhost" jvmroute ="Tomcat_a">

<Engine name = "Catalina" defaulthost = "localhost" jvmroute ="Tomcat_ B">

In additionJetty ApplicationModify $ jetty_home/etc/Jetty. xml.

<! -- ===================================================== ================================ -->

<Set name = "sessionidmanager">

<New class ="Org. mortbay. Jetty. servlet. hashsessionidmanager">

<Set name = "workername">
Tomcat_a
</Set>

</New>

</Set>

 

When you ask http: // 192.168.50.50: 8080/test. jsp, AAAAAAAAAAAAAAAAAA and bbbbbbbbbbbbbbbbbbbbb are displayed alternately, indicating that the test is successful.

II. (2) mod_jk Server Load balancer Configuration

1. BecauseMod_jkFor third-party modules, need to download to the Tomcat official website, URL for http://mirror.bjtu.edu.cn/apache//tomcat/tomcat-connectors/jk/source/jk-1.2.31/tomcat-connectors-1.2.31-src.tar.gz.

2. Compile the JK module into Apache on Apache server:

# Tar xvfz tomcat-connectors-1.2.31-src.tar.gz

 

# Cd tomcat-connectors-1.2.31-src/Native/

 

./Configure -- With-apxs =/usr/local/apache2/bin/apxs -- With-Java-home =/usr/local/JDK

Make & make install

 

At this time, a mod_jk.so will appear under/usr/local/apache2/modules, and the compilation is successful.

3. Configure the Apache configuration file

1 VI/usr/local/apache2/CONF/httpd. conf Add the following content:

Loadmodule jk_module modules/mod_jk.so

 

<Ifmodule jk_module>

Jkworkersfile CONF/workers. Properties

Jklogfile logs/mod_jk.log

JkloglevelWarn

</Ifmodule>

 

Include CONF/app2.conf

2. Create and configure the app2.conf File

Listen 8052.

<Virtualhost *: 8052>

DocumentRoot "/usr/local/apache2/htdocs"

Servername localhost

Serveradmin app2.Admin@99bill.com

Errorlog "/usr/local/apache2/logs/app2 error_log"

Transferlog"/Usr/local/apache2/logs/app2 access_log"

Jkmount /*
Tomcatbalancer

</Virtualhost>

3. Create and configure the workers. properties File

Worker is a tomcat instance used to process servlet requests from the Web server. One worker or multiple workers can be used to achieve load balancing or website partitioning. Each worker registers the IP address, port number, and Communication Protocol of its host with the Web server.

 

VI/usr/local/apache2/CONF/Workers. Properties, Add the following content:

Worker. List = tomcattest1

Worker. tomcattest1.port = 8009

Worker. tomcattest1.host = 192.168.55.229

Worker. tomcattest1.type = ajp13

Worker. tomcattest1.lbfactor = 2

 

Worker. List = tomcattest2

Worker. tomcattest2.port = 8009

Worker. tomcattest2.host = 192.168.55.231

Worker. tomcattest2.type =Ajp13

Worker. tomcattest2.lbfactor = 1

 

Worker. List =Tomcatbalancer

Worker. tomcatbalancer. type =LB

Worker. tomcatbalancer.Balance_workers= Tomcattest1, tomcattest2

 

 

1. Modify the server. xml file <engine name = "Catalina" defaulthost = "localhost" jvmroute ="Tomcat_a">

<Engine name = "Catalina" defaulthost = "localhost" jvmroute ="Tomcat_ B">

For jetty applications, modify $ jetty_home/etc/Jetty. xml.

<! -- ===================================================== ================================ -->

<Set name = "sessionidmanager">

<New class = "org. mortbay. Jetty. servlet. hashsessionidmanager">

<Set name = "workername">Tomcat_a</Set>

</New>

</Set>

 

5 test:

When you ask http: // 192.168.50.50: 8052/test. jsp, The AAAAAAAAAAAAAAAAAA and bbbbbbbbbbbbbbbbbbb will alternate, And the bbbbbbbbbbbbbbbbbbb will appear twice on average, becauseWorker. tomcattest2.lbfactor = 1

Worker. tomcattest1.lbfactor = 2 weight settings

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.