01 tomcat lb cluster
配置环境
node1 192.168.1.131 CentOS 7.2
node4 192.168.1.132 CentOS 7.2
node3 192.168.1.133 CentOS 7.2
1、如何部署多个tomcat实例向用户提供服务
#node4结点配置tomcat
[[email protected] ~]# tar xf apache-tomcat-8.0.38.tar.gz -C /usr/local/
[[email protected] ~]# cd /usr/local/
[[email protected] local]# ln -s apache-tomcat-8.0.38/ tomcat
[[email protected] local]# vim /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
[[email protected] local]# . /etc/profile.d/tomcat.sh
[[email protected] local]# catalina.sh version
[[email protected] local]# mkdir -p /data/webapps/ROOT
[[email protected] local]# cd tomcat/conf/
[[email protected] conf]# cp server.xml{,.bak}
[[email protected] conf]# vim server.xml
修改
<Engine name="Catalina" defaultHost="localhost">
为
<Engine name="Catalina" defaultHost="node4.magedu.com">
添加Host主机
<Host name="node4.magedu.com" appBase="/data/webapps" autoDeploy="true">
<Context path="" docBase="ROOT" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"
prefix="web1_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
[[email protected] conf]# mkdir /data/logs
[[email protected] conf]# catalina.sh configtest
[[email protected] conf]# mkdir -p /data/webapps/ROOT/{lib,classes,META-INF,WEB-INF}
[[email protected] conf]# vim /data/webapps/ROOT/index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>JSP Test Page on Web1</title>
</head>
<body>
<% out.println("Hello,web1."); %>
</body>
</html>
[[email protected] conf]# catalina.sh start
[[email protected] conf]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.131 node1 node1.magedu.com
192.168.1.132 node4 node4.magedu.com
192.168.1.133 node3 node3.magedu.com
[[email protected] conf]# curl node4.magedu.com:8080
#node3结点配置tomcat
[[email protected] ~]# scp apache-tomcat-8.0.38.tar.gz node3:/root
[[email protected] ~]# scp -rp /data/ node3:/data
[[email protected] ~]# tar xf apache-tomcat-8.0.38.tar.gz -C /usr/local/
[[email protected] ~]# cd /usr/local/
[[email protected] local]# ln -s apache-tomcat-8.0.38/ tomcat
[[email protected] ~]# cd /usr/local/tomcat/conf/
[[email protected] conf]# scp server.xml node3:/usr/local/tomcat/conf/
[[email protected] local]# cd tomcat/conf/
[[email protected] conf]# vim server.xml
修改
<Engine name="Catalina" defaultHost="localhost">
为
<Engine name="Catalina" defaultHost="node3.magedu.com">
修改
node4.magedu.com
为
node3.magedu.com
[[email protected] conf]# scp /etc/profile.d/tomcat.sh node3:/etc/profile.d/
[[email protected] conf]# . /etc/profile.d/tomcat.sh
[[email protected] conf]# catalina.sh start
[[email protected] ~]# curl node3.magedu.com:8080
[[email protected] conf]# vim /data/webapps/ROOT/index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>JSP Test Page on Web1</title>
</head>
<body>
<% out.println("Hello,web2."); %>
</body>
</html>
[[email protected] ~]# yum -y install nginx
[[email protected] ~]# cd /etc/nginx/
[[email protected] nginx]# cp nginx.conf{,.bak}
[[email protected] nginx]# vim nginx.conf
在server段前添加
upstream tcsrvs {
ip_hash; #对客户端锁定服务器
server node4.magedu.com:8080;
server node3.magedu.com:8080;
}
在server段内添加
location ~* \.(jsp|do)$ {
proxy_pass http://tcsrvs;
}
[[email protected] nginx]# nginx -t
[[email protected] nginx]# systemctl start nginx.service
2、如何使用httpd反代用户请求
(1)方法一
apache:
mod_proxy
mod_proxy_http
mod_proxy_balancer
tomcat:
http connector
[[email protected] nginx]# systemctl stop nginx.service
[[email protected] nginx]# systemctl disable nginx.service
[[email protected] nginx]# yum -y install httpd
[[email protected] ~]# cd /etc/httpd/conf
[[email protected] conf]# vim httpd.conf
注释掉
#DocumentRoot "/var/www/html"
[[email protected] conf]# cd ../conf.d/
[[email protected] conf.d]# vim vhost.conf
<proxy balancer://lbcluster1>
BalancerMember http://192.168.1.132:8080 loadfactor=10 route=TomcatA
BalancerMember http://192.168.1.133:8080 loadfactor=10 route=TomcatB
</proxy>
<VirtualHost *:80>
ServerName web1.magedu.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://lbcluster1/
ProxyPassReverse / balancer://lbcluster1/
<Location />
Require all granted
</Location>
</VirtualHost>
如果需要会话绑定,可使用下面的内容:
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e;path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://lbcluster1>
BalancerMember http://192.168.1.132:8080 loadfactor=10 route=TomcatA
BalancerMember http://192.168.1.133:8080 loadfactor=10 route=TomcatB
ProxySet stickysession=ROUTEID
</proxy>
<VirtualHost *:80>
ServerName web1.magedu.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://lbcluster1/
ProxyPassReverse / balancer://lbcluster1/
<Location />
Require all granted
</Location>
</VirtualHost>
[[email protected] conf.d]# systemctl start httpd.service
[[email protected] conf]# vim server.xml
修改
<Engine name="Catalina" defaultHost="node4.magedu.com">
为
<Engine name="Catalina" defaultHost="node4.magedu.com" jvRoute="TomcatA">
[[email protected] conf]# vim server.xml
修改
<Engine name="Catalina" defaultHost="node3.magedu.com">
为
<Engine name="Catalina" defaultHost="node3.magedu.com" jvRoute="TomcatB">
[[email protected] conf]# catalina.sh stop
[[email protected] conf]# catalina.sh stop
[[email protected] conf]# vim /data/webapps/ROOT/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.magedu.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
[[email protected] conf]# vim /data/webapps/ROOT/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB.magedu.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
[[email protected] conf]# catalina.sh start
[[email protected] conf]# catalina.sh start
测试
http://192.168.1.131/index.jsp
结果:成功
(2)方法二
apache:
mod_proxy
mod_proxy_ajp
mod_proxy_balancer
tomcat:
http connector
[[email protected] conf.d]# cp vhost.conf vhost.ajp.conf
[[email protected] conf.d]# mv vhost.conf vhost.http.conf.bak
[[email protected] conf.d]# vim vhost.ajp.conf
#Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e;path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://lbcluster1>
BalancerMember ajp://192.168.1.132:8009 loadfactor=10 route=TomcatA
BalancerMember ajp://192.168.1.133:8009 loadfactor=10 route=TomcatB
#ProxySet stickysession=ROUTEID
</proxy>
<VirtualHost *:80>
ServerName web1.magedu.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://lbcluster1/
ProxyPassReverse / balancer://lbcluster1/
<Location />
Require all granted
</Location>
</VirtualHost>
[[email protected] conf.d]# systemctl restart httpd.service
(3)方法3
apache
mod_jk
tomcat:
ajp connector
[[email protected] ~]# tar xf tomcat-connectors-1.2.42-src.tar.gz
[[email protected] ~]# cd tomcat-connectors-1.2.42-src/native/
[[email protected] native]# yum -y install httpd-devel gcc glibc-devel
[[email protected] native]# which apxs
/usr/bin/apxs
[[email protected] native]# ./configure --with-apxs=/usr/bin/apxs
[[email protected] native]# make -j 4 && make install
[[email protected] native]# cd /etc/httpd/
02 tomcat lb cluster
1、jk_mod:额外编译安装
(1)反向代理
[[email protected] ~]# cd /etc/httpd/conf.d/
[[email protected] conf.d]# mv vhost.ajp.conf{,.bak}
[[email protected] conf.d]# vim mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /* TomcatA
JkMount /status/ stat1
[[email protected] conf.d]# vim /etc/httpd/conf.d/workers.properties
worker.list=TomcatA,stat1
worker.TomcatA.port=8009
worker.TomcatA.host=192.168.1.132
worker.TomcatA.type=ajp13
worker.TomcatA.lbfactor=1
worker.stat1.type = status
[[email protected] conf.d]# httpd -t
[[email protected] conf.d]# systemctl restart httpd.service
(2)负载均衡
[[email protected] ~]# cd /etc/httpd/conf.d/
[[email protected] conf.d]# vim mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /* lbcluster1
JkMount /status/ stat1
[[email protected] conf.d]# vim /etc/httpd/conf.d/workers.properties
worker.list = lbcluster1,stat1
worker.TomcatA.type = ajp13
worker.TomcatA.host = 192.168.1.132
worker.TomcatA.port = 8009
worker.TomcatA.lbfactor = 1
worker.TomcatB.type = ajp13
worker.TomcatB.host = 192.168.1.133
worker.TomcatB.port = 8009
worker.TomcatB.lbfactor = 1
worker.lbcluster1.type = lb
worker.lbcluster1.sticky_session = 0
worker.lbcluster1.balance_workers = TomcatA, TomcatB
worker.stat1.type = status
注:如果要实现会话绑定,可以设置worker.lbcluster1.sticky_session = 1
[[email protected] conf.d]# httpd -t
[[email protected] conf.d]# systemctl restart httpd.service
2、Balancer内置管理功能
[[email protected] conf.d]# systemctl stop httpd.service
[[email protected] conf.d]# mv mod_jk.conf{,.bak}
[[email protected] conf.d]# cp vhost.ajp.conf.bak vhost.ajp.conf
[[email protected] conf.d]# vim vhost.ajp.conf
添加
<location /balancer-manager>
SetHandler balancer-manager
ProxyPass !
Require all granted
</Location>
[[email protected] conf.d]# systemctl restart httpd.service
3、Session Cluster
[[email protected] conf]# catalina.sh stop
[[email protected] ~]# cd /usr/local/tomcat/conf/
[[email protected] conf]# vim server.xml
在Host内部添加
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.1.132"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
[[email protected] conf]# catalina.sh stop
[[email protected] ~]# cd /usr/local/tomcat/conf/
[[email protected] conf]# vim server.xml
在Host内部添加
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.1.133"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
[[email protected] conf]# cp web.xml /data/webapps/ROOT/WEB-INF/
[[email protected] conf]# vim /data/webapps/ROOT/WEB-INF/web.xml
在web-app段内(</web-app>前)添加
<distributable/>
[[email protected] conf]# scp /data/webapps/ROOT/WEB-INF/web.xml node3:/data/webapps/ROOT/WEB-INF/
[[email protected] conf]# catalina.sh start
[[email protected] conf]# catalina.sh start
[[email protected] conf]# tail /usr/local/tomcat/logs/catalina.out
[[email protected] conf.d]# systemctl stop httpd.service
[[email protected] conf.d]# rm -f vhost.http.conf
[[email protected] conf.d]# cp vhost.ajp.conf.bak vhost.ajp.conf
[[email protected] conf.d]# systemctl start httpd.service
[[email protected] conf.d]# vim vhost.ajp.conf
[[email protected] conf.d]# systemctl stop httpd.service
[[email protected] conf.d]# rm -f vhost.ajp.conf
[[email protected] conf.d]# cp mod_jk.conf.bak mod_jk.conf
[[email protected] conf.d]# systemctl start httpd.service
[[email protected] conf.d]# vim workers.properties
[[email protected] conf.d]# systemctl stop httpd.service
[[email protected] conf.d]# cd /etc/nginx/
[[email protected] nginx]# vim nginx.conf
修改location段的内容为:
location / {
proxy_pass http://tcsrvs;
}
[[email protected] nginx]# systemctl start nginx.service
03 memcached和msm
[[email protected] ~]# yum -y install memcached
[[email protected] ~]# systemctl start memcached.service
[[email protected] ~]# yum -y install telnet
[[email protected] ~]# telnet localhost 11211
[[email protected] ~]# systemctl stop memcached.service
#显示memcached详细信息
[[email protected] ~]# memcached -u memcached -vv
[[email protected] ~]# vim /etc/sysconfig/memcached
修改OPTIONS的值为
OPTIONS="-f 1.2"
[[email protected] ~]# systemctl restart memcached.service
[[email protected] lib]# yum -y install memcached
[[email protected] lib]# systemctl start memcached.service
[[email protected] conf]# cd /usr/local/tomcat/lib/
[[email protected] conf]# cd /usr/local/tomcat/lib/
[[email protected] msm]# ls *
memcached-session-manager-1.8.3.jar
memcached-session-manager-tc8-1.8.3.jar
msm-javolution-serializer-1.8.3.jar
spymemcached-2.11.1.jar
[[email protected] msm]# scp * 192.168.1.132:/usr/local/tomcat/lib
[[email protected] msm]# scp * 192.168.1.133:/usr/local/tomcat/lib
[[email protected] lib]# cd ../conf/
[[email protected] conf]# vim server.xml
删除之前配置的Cluster
在Context内部添加
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.1.131:11211,n3:192.168.1.133:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
/>
[[email protected] conf]# scp server.xml node3:/usr/local/tomcat/conf/
[[email protected] lib]# catalina.sh start
[[email protected] lib]# catalina.sh start
[[email protected] ~]# cd /etc/nginx/
[[email protected] conf]# rm -f /data/webapps/ROOT/WEB-INF/web.xml
[[email protected] conf]# rm -f /data/webapps/ROOT/WEB-INF/web.xml
[[email protected] conf]# catalina.sh stop
[[email protected] conf]# catalina.sh start
[[email protected] conf]# catalina.sh stop
[[email protected] conf]# catalina.sh start
05 tcpdump和nc工具的使用
tcp协议目标端口是80
tcpdump -i eth0 tcp dst port 80
[[email protected] ~]# yum -y install wireshark
[[email protected] ~]# yum -y install wireshark-gnome
[[email protected] ~]# yum -y install nmap
[[email protected] ~]# yum -y install nc