Experimental system: CentOS 6.6_x86_64
Lab Prerequisites: Prepare the build environment in advance, firewall and SELinux are off
The experiment shows that there are 2 hosts in this experiment, such as the topology of IP assignment
Experimental software: jdk-8u60-linux-x64 apache-tomcat-8.0.24 tomcat-connectors-1.2.41 httpd-2.2.15 httpd-devel-2.2.15
Experimental topology:
One, two machines install the Java environment
1. Install Java:
wget http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.tar.gz tar XF jdk-8u60-linux-x64. tar. gz-c/usr/local//usr/local/ln -sv jdk1. 8. 0_60 JDK
2. Configure Environment variables:
Vim/etc/profile.d/jdk. sh--------------------------------------------------->java_home=/usr/local/Jdkpath = $JAVA _home/bin: $PATHexport java_home PATH<--------------------------------------------------- /etc/profile.d/jdk. SH
Second, two machines to install Tomcat
1. Install Tomcat:
wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.0.24/bin/ apache-tomcat-8.0.24.tar.gztar XF apache-tomcat-8.0. A. tar. gz-c/usr/local//usr/local/ln -sv apache-tomcat-8.0. Tomcat
2. Configure Environment variables:
Vim/etc/profile.d/tomcat. sh--------------------------------------------------->catalina_base=/usr/local/ Tomcatpath= $CATALINA _base/bin: $PATHexport PATH catalina_base<---------------------------------- -----------------/etc/profile.d/tomcat. SH
3. View Tomcat and Java status:
Catalina. SH version
4. Provide a startup script:
vim/etc/init.d/Tomcat-------------------------------------------------------->#!/bin/SH# Tomcat Init script forlinux.## chkconfig:2345 the -# description:the Apache Tomcat servlet/JSP container.# java_opts='-xms64m-xmx128m'Java_home=/usr/local/Jdkcatalina_home=/usr/local/tomcatexport java_home catalina_home Case$1 inchstart) Exec $CATALINA _home/bin/catalina.SHstart;; Stop) exec $CATALINA _home/bin/catalina.SHstop;; Restart) $CATALINA _home/bin/catalina.SHStopSleep 2exec $CATALINA _home/bin/catalina.SHstart;;*) Echo "Usage: ' basename $ ' {Start|stop|restart}"Exit1 ;;Esac<--------------------------------------------------------chmod+x/etc/init.d/Tomcatchkconfig--add Tomcat
5. Edit the Tomcat configuration file and add only the Jvmroute parameter:
On the 19.66:
vim/usr/local/tomcat/conf/server.xml-----------------------------------------------><engine Name ="Catalina" defaulthost="localhost" jvmroute=" Tomcata "> // indicates that this is a tomcata host
On the 19.74:
vim/usr/local/tomcat/conf/server.xml-----------------------------------------------><engine Name ="Catalina" defaulthost="localhost" jvmroute=" TOMCATB ">
6. Provide the test page:
On the 19.66:
mkdir-pv/usr/local/tomcat/webapps/test/web-inf/{Classes,lib}vim/usr/local/tomcat/webapps/test/index.jsp------------------------------------------------------------------------><%@ Page language="Java"%><%@ page import="java.util.*"%>Head> <title>test</title> </Head> <body> <%Out.println ("This is tomcata"); %> </body>Service Tomcat Start
On the 19.74:
mkdir-pv/usr/local/tomcat/webapps/test/web-inf/{Classes,lib}vim/usr/local/tomcat/webapps/test/index.jsp------------------------------------------------------------------------><%@ Page language="Java"%><%@ page import="java.util.*"%>Head> <title>test</title> </Head> <body> <%Out.println ("This is TOMCATB"); %> </body>Service Tomcat Start
Third, use the MOD_JK module to load balance Tomcat
There are two ways to use the httpd reverse proxy tomcat, which is to use the two modules of Mod_proxy and MOD_JK respectively. MOD_JK requires additional compilation and installation, and mod_proxy needs to use httpd proxy_module, Proxy_balancer_module, Proxy_http_module, proxy_ajp_module modules, Make sure that the modules are installed at the time of installation. From httpd2.2 onwards, these modules are installed by default, so we can install them directly with Yum.
The following first demonstrates load balancing of Tomcat using the MOD_JK module. This module only needs to be installed on one machine, here we install on 19.66.
1. Install httpd:
Yum Install httpd httpd-devel // installation Httpd-devel in order to provide APXS
2. Install the Mod_jk.so module:
wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-connectors/jk/ tomcat-connectors-1.2.41-src.tar.gztar XF tomcat-connectors-1.2. -src. Tar . GZCD Tomcat-connectors-1.2. -src/native/. /configure--with-apxs=/usr/sbin/apxsmake make andinstall
3. Provide additional HTTPD module configuration files:
vim/etc/httpd/conf.d/httpd-jk.conf------------------------------------------------------># Load The Mod_jkloadmodule jk_module modules/mod_jk.so // load mod_jk.so module Jkworkersfile /etc/httpd/conf.d/workers.properties // configuration file to be used by this module jklogfile logs/mod_jk.logjkloglevel debugjkmount *// All content under the access root is sent to the cluster LB1 jkmount /status/ stat1
4. Configure the configuration file for the MOD_JK module workers.properties:
vim/etc/httpd/conf.d/workers.properties-------------------------------------------------------->worker.list=Lb1,stat1worker. Tomcata.type= Ajp13//httpd mode used to connect to TomcatWorker. Tomcata.host =192.168.19.66worker. Tomcata.port=8009worker. Tomcata.lbfactor=1 //WeightsWorker. Tomcatb.type =Ajp13worker. Tomcatb.host=192.168.19.74worker. Tomcatb.port=8009worker. Tomcatb.lbfactor=1Worker.lb1.type= LB//Dispatch ModeWorker.lb1.sticky_session =0 //whether to use session bindingWorker.lb1.balance_workers =Tomcata, TomcatBworker.stat1.type= Status
5. Start the test:
Service httpd Start
With the browser open http://192.168.19.66/test, we constantly refresh, you can see the experimental results.
Iv. load Balancing tomcat using the Mod_proxy module
1. You can still edit on 19.66, using the HTTP protocol:
cd/etc/httpd/conf.d/MVHttpd-jk.conf httpd-Jk.conf.bakvim httpd-proxy.conf--------------------------------------------------------------><proxy Balancer://lb1>Balancermember http://192.168.19.66:8080 loadfactor=1 Route=tomcataBalancermember http://192.168.19.74:8080 loadfactor=1 ROUTE=TOMCATBProxyset lbmethod=byrequests//scheduling the number of statistics requests based on weights</Proxy>Proxypass/balancer://lb1/Proxypassreverse/balancer://lb1/<--------------------------------------------------------------Service httpd Restart
2. Using the AJP protocol:
cd/etc/httpd/conf.d/MVHttpd-jk.conf httpd-Jk.conf.bakvim httpd-proxy.conf--------------------------------------------------------------><proxy Balancer://lb1>Balancermember AJP://192.168.19.66:8009 loadfactor=1 Route=tomcataBalancermember AJP://192.168.19.74:8009 loadfactor=1 ROUTE=TOMCATBProxyset lbmethod=byrequests</Proxy>Proxypass/balancer://lb1/Proxypassreverse/balancer://lb1/<--------------------------------------------------------------Service httpd Restart
The above two methods can achieve load balancing, refresh the page can still see the effect, here is not given the picture. At this point, the experiment is all over, thank you! If you have any questions, please contact me, qq:82800452.
Load-balance configuration of Tomcat with HTTPD