配置基于mod_jk的负载均衡
mod_jk文章
1、 为了避免用户直接访问后端Tomcat实例,影响负载均衡的效果,建议在Tomcat 7的各实例上禁用HTTP/1.1连接器。
2、为每一个Tomcat 7实例的引擎添加jvmRoute参数,并通过其为当前引擎设置全局惟一标识符。如下所示。需要注意的是,每一个实例的jvmRoute的值均不能相同。
<Engine name=”Standalone” defaultHost=”localhost” jvmRoute=” TomcatA ”>
而后去配置apache,修改/etc/httpd/extra/httpd-jk.conf为如下内容:
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/extra/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /* lbcluster1
JkMount /jkstatus/ stat1
编辑/etc/httpd/extra/workers.properties,添加如下内容:
worker.list = lbcluster1,stat1
worker.TomcatA.type = ajp13
worker.TomcatA.host = 172.16.100.1
worker.TomcatA.port = 8009
worker.TomcatA.lbfactor = 5
worker.TomcatB.type = ajp13
worker.TomcatB.host = 172.16.100.2
worker.TomcatB.port = 8009
worker.TomcatB.lbfactor = 5
worker.lbcluster1.type = lb
worker.lbcluster1.sticky_session = 1
worker.lbcluster1.balance_workers = TomcatA, TomcatB
worker.stat1.type = status
范例:
10.0.0.53/54是tomcat机器,10.0.0.91是httpd负载
安装jdk
[root@NFS-WEB2 local]# rpm -ivh jdk-7u9-linux-x64.rpm
Preparing... ########################################### [100%]
1:jdk ########################################### [100%]
安装apa-tomat
[root@NFS-WEB2 local]# tar xf apache-tomcat-7.0.67.tar.gz -C /usr/local/
[root@NFS-WEB2 local]# ln -sv apache-tomcat-7.0.67 tomcat
`tomcat' -> `apache-tomcat-7.0.67'
[root@NFS-WEB2 bin]# cat /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
[root@NFS-WEB2 bin]# cat /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
tomcat的init.d启动脚本
[root@NFS-WEB2 bin]# cat /etc/init.d/tomcat
#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
JAVA_HOME=/usr/java/latest
CATALINA_HOME=/usr/local/tomcat
export JAVA_HOME CATALINA_HOME
case $1 in
start)
exec $CATALINA_HOME/bin/catalina.sh start;;
stop)
exec $CATALINA_HOME/bin/catalina.sh stop;;
restart)
$CATALINA_HOME/bin/catalina.sh stop
sleep 2
exec $CATALINA_HOME/bin/catalina.sh start;;
*)
echo "Usage: `basename $0` {start|stop|restart}"
exit 1
;;
esac
[root@NFS-WEB2 bin]#
[root@NFS-WEB2 local]# chmod +x /etc/init.d/tomcat
[root@NFS-WEB2 local]# chkconfig --add tomcat
在两个节点分别提供两个不同的测试页面:
10.0.0.53
[root@NFS-WEB2 webapps]# cd /usr/local/tomcat/webapps
[root@NFS-WEB2 webapps]# mkdir testapp
[root@NFS-WEB2 webapps]# cd testapp/
[root@NFS-WEB2 testapp]# mkdir -p WEB-INF/{classes,lib}
[root@NFS-WEB2 webapps]# vim index.jsp
演示效果,在TomcatA上某context中(如/test),提供如下页面
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA </font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
10.0.0.54
[root@NFS-WEB1 webapps]# cd /usr/local/tomcat/webapps
[root@NFS-WEB1 webapps]# mkdir testapp
[root@NFS-WEB1 webapps]# cd testapp/
[root@NFS-WEB1 testapp]# mkdir -p WEB-INF/{classes,lib}
[root@NFS-WEB1 webapps]# vim index.jsp
演示效果,在TomcatB上某context中(如/test),提供如下页面
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="blue">TomcatA </font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
mod_jk 10.0.0.91
[root@nginx-proxy2 modules]# cat /etc/httpd/conf.d/mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel notice
JkMount /* lbcA
JkMount /jkstatus/ statA
[root@nginx-proxy2 modules]# cat /etc/httpd/conf.d/workers.properties
worker.list=lbcA,statA
worker.TomcatA.port=8009
worker.TomcatA.host=10.0.0.53
worker.TomcatA.type=ajp13
worker.TomcatA.lbfactor=1
worker.TomcatB.port=8009
worker.TomcatB.host=10.0.0.54
worker.TomcatB.type=ajp13
worker.TomcatB.lbfactor=1
worker.lbcA.type=lb
worker.lbcA.sticky_session=0 回话不保持
worker.lbcA.balance_workers = TomcatA,TomcatB
worker.statA.type = status
[root@nginx-proxy2 modules]#