Apache implements Tomcat's load-balancing cluster in a mod_jk manner

Source: Internet
Author: User

Directory

1. Environment preparation

2. MOD_JK Configuration

3. Testing

4. Summary

1. Environment preparation

Prepare three hosts, one to provide a httpd environment, and another two to provide a tomcat environment.

Host Planning:

Hostname: HTTPD IP Address: 192.168.0.200 provides httpd service

Host name: Tomcata IP Address: 192.168.0.201 provides tomcat services

Host name: TOMCATB IP Address: 192.168.0.202 provides tomcat services

For JDK and Tomcat installation, please refer to blog post: http://zhaochj.blog.51cto.com/368705/1639740

Description of the configuration for Tomcat:

To implement the reverse proxy tomcat in a mod_jk manner, and to achieve load balancing, the HTTPD connection to Tomcat is no longer the HTTP protocol, but rather the AJP protocol, which cannot be verified when httpd and tomcat communicate "Host" as if the HTTP protocol could be passed Host header, if so, Tomcat can configure a single virtual host to receive requests with the host header, so that it does not have to change the Tomcat's default host, so that the client can access our site only through the hostname, without having to add a URI. But I did not test the AJP protocol to pass the host header, so I modified the Tomcat's default host to point to our own deployed site.

1.1. Modify the Tomcat Default Web site home directory and Jvmroute configuration:

[Email protected] conf]# pwd/usr/local/tomcat/conf[[email protected] conf]# vim server.xml

Specify the Jvmroute parameter in the engine window that must be in the cluster environment:

[Email protected] conf]# vim server.xml<engine name= "Catalina" defaulthost= "localhost" jvmroute= "Tomcata" ># Also set the Jvmroute on the TOMCATB host.

1.2. Prepare our application:

The directory structure of the JSP program has its own format, first look at the default site directory format

[[email protected] webapps]# pwd/usr/local/tomcat/webapps[[email protected] webapps]#  tree -L 2 -d.├── docs│   ├── api│   ├──  appdev│   ├── architecture│   ├── config│   ├──  elapi│   ├── funcspecs│   ├── images│   ├──  jspapi│   ├── servletapi│   ├── tribes│   ├──  web-inf│   └── websocketapi├── examples│   ├── jsp│    ├── servlets│   ├── WEB-INF│   └── websocket├──  host-manager│   ├── images│   ├── meta-inf│   └──  web-inf├── manager│   ├── images│   ├── meta-inf│    └── web-inf├── root│&nBsp;  └── web-inf└── test 

Visible in each app has a web-inf such a directory, this directory has a Web. xml file, do not modify this file, Web-inf this directory directly copied to the new application directory, and then put our application in it.

[[email protected] conf]# cp -r /usr/local/tomcat/webapps/root/web-inf /tomcat/ webapps/test/[[email protected] conf]# vim /tomcat/webapps/test/index.jsp  # Take a test file as my project <%@ page language= "Java"  %>

Both Tomcat application servers provide the same test item above, except that the "Tomcata" in index.jsp is changed to "TOMCATB".

2. MOD_JK Configuration

Back-end Tomcat is ready, now look at the corresponding configuration of the MOD_JK module in the front-end httpd.

MOD_JK is a separate project for Apache, CentOS 6.4 in the default Yum source RPM package does not compile this module, so you need to compile the installation itself, compile and install is relatively simple, please refer to: http://zhaochj.blog.51cto.com/ 368705/1641939

Configuration mod_jk mainly has three files, mod_jk.conf, workers.properties, uriworkersmap.properties, these three file names can be customized.

Because HTTPD's main profile has "Include conf.d/*.conf" enabled, the top mod_jk.conf is loaded into the master profile, separating the functional module configuration from being too verbose for easy administration.

[[email protected] conf.d]# pwd/etc/httpd/conf.d[[email  protected] conf.d]# vim mod_jk.confloadmodule jk_module modules/mod_ jk.sojkworkersfile /etc/httpd/conf.d/workers.propertiesjkmountfile /etc/httpd/conf.d/ uriworkersmap.propertiesjklogfile logs/mod_jk.logjkloglevel warn  # You can set the log level to info or debug#jkmount /* lbcluster1#jkmount /jkstatus stat# while debugging Workers.properties and uriworkersmap.properties two files are related by Jkorkersfile and jkmonutfile two instructions respectively, if you do not want to enable uriworkersmap.properties this file, that straight The mod_jk.conf file is followed by a "jkmount" command, which is equivalent to using the Proxypass directive in the Proxy_mod module. 
[[email protected] conf.d]# vim workers.properties# Defines the list of Woker worker.list=lbcluster1,stat#tomcata  Tomcata host is defined here worker.tomcata.host= 192.168.0.201worker.tomcata.port=8009worker.tomcata.type=ajp13worker.tomcata.lbfactor=2#tomcab , This defines the TOMCATB host worker.tomcatb.host=192.168.0.202worker.tomcatb.port=8009worker.tomcatb.type= ajp13worker.tomcatb.lbfactor=2   #定义主机权重 #balance , define cluster properties worker.lbcluster1.type=lbworker.retries= 3worker.lbcluster1.sticky_session=0   #这里定义session信息的粘滞, binding, 0 for unbound, 1 for binding Worker.lbcluster1.balance_ WORKERS=TOMCATA,TOMCATB#MONITOR&NBSP;STATUS&NBSP, defining the state output thus worker.stat.type=status 
[Email protected] conf.d]# vim uriworkersmap.properties/*=lbcluster1/jkstatus=stat!/*.jpg=lbcluster1!/*.gif= lbcluster1!/*.png=lbcluster1!/*.bmp=lbcluster1!/*.html=lbcluster1!/*.htm=lbcluster1!/*.css=lbcluster1!/*.js= lbcluster1# This file defines the rules for backward forwarding, all resources under the site root are assigned to the cluster Lbcluster1 for processing, and some static files are httpd processed locally.


Finally restart the HTTPD service to confirm that the MOD_JK module is properly loaded:

[[Email protected] conf.d]# service httpd restart[[email protected] conf.d]# tail/var/log/httpd/mod_jk.log[tue May 05 17: 02:04.876] [8161:140100989429728] [info] init_jk::mod_jk.c (3383): mod_jk/1.2.40 initialized# First set the log level to info, You can see the above information to prove that the MOD_JK module has been loaded correctly.

3. Testing

Test to see if we can access the projects we deployed on Tomcata and TOMCATB by visiting the httpd (192.168.0.200) host.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/64/wKioL1VIkR3RDjChAADBeRFjbWw720.jpg "title=" 1.jpg " alt= "Wkiol1vikr3rdjchaadberfjbww720.jpg"/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/69/wKiom1VIj8fhiatbAADIGzKHa6c515.jpg "title=" 1.jpg " alt= "Wkiom1vij8fhiatbaadigzkha6c515.jpg"/>

First access to the Tomcata host, and then a refresh and access to the TOMCATB host, this is because the value of worker.lbcluster1.sticky_session in the workers.properties file is "0", Indicates that the session binding feature is not enabled, if we deploy the deployment is only for browsing, it is not a problem, if the site is a user needs to log on to the site, that is mod_jk distributed to different back-end tomcat will require users to re-login, so in this scenario should be set to "1", The session binding feature is enabled.

4. Summary

Using Apache's MOD_JK module for Tomcat load Balancing, Apache and Tomcat communicate using the AJP protocol, so the entire environment can be tested to disable the HTTP connector of the two tomcat servers on the backend. In a load-balanced cluster environment, the back-end tomcat servers in the Server.xml master configuration file must add the name defined in the "jvmrote=" cluster list to the engine container.


This article is from the "focus on operations, and Linux Dances" blog, please be sure to keep this source http://zhaochj.blog.51cto.com/368705/1642199

Apache implements Tomcat's load-balancing cluster in a mod_jk manner

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.