一、系统环境
CentOS6.4X64最小化安装
二、环境准备
1.实验拓扑
2.同步各节点时间
apache,tomcat1,tomcat2的操作都是一样的
[root@apache ~]# yum install ntp -y [root@apache ~]# ntpdate asia.pool.ntp.org [root@apache ~]# hwclock -w [root@apache ~]# echo "*/10 * * * * /usr/sbin/ntpdate asia.pool.ntp.org &>/dev/null" >/var/spool/cron/root
3.安装jdk,tomcat1和tomcat2的安装过程一样,这里只给出tomcat1的安装过程
[root@tomcat1 ~]# tar xf jdk-8u45-linux-x64.tar.gz -C /usr/local/ #编辑环境变量配置文件 [root@tomcat1 ~]# cat /etc/profile.d/java.sh JAVA_HOME=/usr/local/jdk1.8.0_45 JAVA_BIN=$JAVA_HOME/bin JRE_HOME=$JAVA_HOME/jre PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin CLASSPATH=$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/jre/lib/charsets.jar #让环境变量生效 [root@tomcat1 ~]# source /etc/profile.d/java.sh #测试结果 [root@tomcat1 ~]# java -version java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
4.安装tomcat,安装过程tomcat1和tomcat2一样
#下载软件 [root@tomcat1 ~]# wget http://apache.fayea.com/tomcat/tomcat-7/v7.0.62/bin/apache-tomcat-7.0.62.tar.gz [root@tomcat1 ~]# tar xf apache-tomcat-7.0.62.tar.gz -C /usr/local/ [root@tomcat1 ~]# ln -s /usr/local/apache-tomcat-7.0.62/ /usr/local/tomcat [root@tomcat1 ~]# cp /usr/local/tomcat/bin/catalina.sh /etc/init.d/tomcat #编辑启动脚本在第二行添加如下内容 [root@tomcat1 ~]# vim /etc/init.d/tomcat # chkconfig: 2345 63 37 # description: tomcat server init script # Source Function Library . /etc/init.d/functions JAVA_HOME=/usr/local/jdk1.8.0_45 CATALINA_HOME=/usr/local/tomcat #赋予脚本执行权限,并添加到开机自启动 [root@tomcat1 ~]# chmod 755 /etc/init.d/tomcat [root@tomcat1 ~]# chkconfig --add tomcat [root@tomcat1 ~]# chkconfig tomcat on #启动tomcat [root@tomcat1 ~]# service tomcat start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8.0_45 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@tomcat1 ~]# netstat -anpt |grep java tcp 0 0 :::8080 :::* LISTEN 1583/java tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 1583/java tcp 0 0 :::8009 :::* LISTEN 1583/java [root@tomcat1 ~]# service iptables stop
测试访问结果,访问地址http://192.168.3.74:8080和http://192.168.3.75:8080
到此tomcat安装成功
5.修改配置文件
tomcat1:
[root@tomcat1 ~]# vim /usr/local/tomcat/conf/server.xml#增加jvmRoute #增加一个虚拟主机 #增加虚拟主机的目录和测试文件 [root@tomcat1 ~]# mkdir -p /web/webapp [root@tomcat1 ~]# vim /web/webapp/index.jsp [root@tomcat1 ~]# cat /web/webapp/index.jsp <%@ page language="java" %> TomcatA TomcatA
#检查配置文件并重新启动tomcat [root@tomcat1 ~]# service tomcat stop Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8.0_45 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [root@tomcat1 ~]# service tomcat configtest #该命令只能在tomcat停止的时候执行,否则会报错 Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8.0_45 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Jun 19, 2015 10:58:16 AM org.apache.catalina.core.StandardContext setPath WARNING: A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/] does not meet these criteria and has been changed to [] Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Server version: Apache Tomcat/7.0.62 Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Server built: May 7 2015 17:14:55 UTC Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Server number: 7.0.62.0 Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: OS Name: Linux Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: OS Version: 2.6.32-358.el6.x86_64 Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Architecture: amd64 Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Java Home: /usr/local/jdk1.8.0_45/jre Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Version: 1.8.0_45-b14 Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Vendor: Oracle Corporation Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_BASE: /usr/local/tomcat Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_HOME: /usr/local/tomcat Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.endorsed.dirs=/usr/local/tomcat/endorsed Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.base=/usr/local/tomcat Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.home=/usr/local/tomcat Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp Jun 19, 2015 10:58:16 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib Jun 19, 2015 10:58:16 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-bio-80"] Jun 19, 2015 10:58:16 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["ajp-bio-8009"] Jun 19, 2015 10:58:16 AM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 1097 ms #重新启动tomcat [root@tomcat1 ~]# service tomcat start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8.0_45 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@tomcat1 ~]# netstat -tunlp |grep java tcp 0 0 :::80 :::* LISTEN 1757/java tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 1757/java tcp 0 0 :::8009 :::* LISTEN 1757/java
Session ID <% session.setAttribute("abc","abc"); %><%= session.getId() %> Created on <%= session.getCreationTime() %>
测试tomcat1的服务,需要在windows是做本地hosts解析,即192.168.3.74 www.test.com
tomcat2:
[root@tomcat2 ~]# vim /usr/local/tomcat/conf/server.xml#增加jvmRoute #增加虚拟主机的目录和测试文件 [root@tomcat2 ~]# mkdir -p /web/webapp [root@tomcat2 ~]# vim /web/webapp/index.jsp [root@tomcat2 ~]# cat /web/webapp/index.jsp <%@ page language="java" %> TomcatB TomcatB
#检查配置文件并重新启动tomcat服务 [root@tomcat2 ~]# service tomcat stop Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8.0_45 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [root@tomcat2 ~]# service tomcat configtest Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8.0_45 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Jun 19, 2015 11:08:04 AM org.apache.catalina.core.StandardContext setPath WARNING: A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/] does not meet these criteria and has been changed to [] Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Server version: Apache Tomcat/7.0.62 Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Server built: May 7 2015 17:14:55 UTC Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Server number: 7.0.62.0 Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: OS Name: Linux Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: OS Version: 2.6.32-358.el6.x86_64 Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Architecture: amd64 Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Java Home: /usr/local/jdk1.8.0_45/jre Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Version: 1.8.0_45-b14 Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Vendor: Oracle Corporation Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_BASE: /usr/local/tomcat Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_HOME: /usr/local/tomcat Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.endorsed.dirs=/usr/local/tomcat/endorsed Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.base=/usr/local/tomcat Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.home=/usr/local/tomcat Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp Jun 19, 2015 11:08:04 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib Jun 19, 2015 11:08:04 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-bio-80"] Jun 19, 2015 11:08:04 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["ajp-bio-8009"] Jun 19, 2015 11:08:04 AM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 1155 ms #重新启动tomcat服务 [root@tomcat2 ~]# service tomcat start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8.0_45 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@tomcat2 ~]# netstat -tunlp |grep java tcp 0 0 :::80 :::* LISTEN 1755/java tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 1755/java tcp 0 0 :::8009 :::* LISTEN 1755/java
Session ID <% session.setAttribute("abc","abc"); %><%= session.getId() %> Created on <%= session.getCreationTime() %>
测试结果,将本地hosts解析调整到192.168.3.75 www.test.com上
6.安装apache
安装apr
[root@apache ~]# wget http://mirror.bit.edu.cn/apache//apr/apr-1.5.2.tar.gz [root@apache ~]# tar xf apr-1.5.2.tar.gz [root@apache ~]# cd apr-1.5.2 [root@apache apr-1.5.2]# ./configure --prefix=/usr/local/apr --disable-ipv6 [root@apache apr-1.5.2]# make && make install
安装apr-util
[root@apache ~]# wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.5.4.tar.gz [root@apache ~]# tar xf apr-util-1.5.4.tar.gz [root@apache ~]# cd apr-util-1.5.4 [root@apache apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ [root@apache apr-util-1.5.4]# make && make install
安装apache
#解决依赖关系 [root@apache ~]# yum install pcre-devel openssl-devel -y #下载软件包 [root@apache ~]# wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.12.tar.gz [root@apache ~]# tar xf httpd-2.4.12.tar.gz [root@apache ~]# cd httpd-2.4.12 #配置编译参数 [root@apache httpd-2.4.12]# ./configure \ > --prefix=/usr/local/apache \ > --sysconfdir=/etc/httpd \ > --enable-so \ > --enable-ssl \ > --enable-cgi \ > --enable-rewrite \ > --with-zlib \ > --with-pcre \ > --with-apr=/usr/local/apr \ > --with-apr-util=/usr/local/apr-util \ > --enable-mpms-shared=all \ > --with-mpm=event \ > --enable-proxy \ > --enable-proxy-http \ > --enable-proxy-ajp \ > --enable-proxy-balancer \ > --enable-lbmethod-heartbeat \ > --enable-heartbeat \ > --enable-slotmem-shm \ > --enable-slotmem-plain \ > --enable-watchdog [root@apache httpd-2.4.12]# make && make install
创建启动脚本
[root@apache ~]# cat /etc/init.d/httpd #!/bin/bash # # httpd Startup script for the Apache HTTP Server # # chkconfig: - 85 15 # description: Apache is a World Wide Web server. It is used to serve \ # HTML files and CGI. # processname: httpd # config: /etc/httpd/conf/httpd.conf # config: /etc/sysconfig/httpd # pidfile: /var/run/httpd.pid # Source function library. . /etc/rc.d/init.d/functions if [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpd fi # Start httpd in the C locale by default. HTTPD_LANG=${HTTPD_LANG-"C"} # This will prevent initlog from swallowing up a pass-phrase prompt if # mod_ssl needs a pass-phrase from the user. INITLOG_ARGS="" # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server # with the thread-based "worker" MPM; BE WARNED that some modules may not # work correctly with a thread-based MPM; notably PHP will refuse to start. # Path to the apachectl script, server binary, and short-form for messages. apachectl=/usr/local/apache/bin/apachectl httpd=${HTTPD-/usr/local/apache/bin/httpd} prog=httpd pidfile=${PIDFILE-/var/run/httpd.pid} lockfile=${LOCKFILE-/var/lock/subsys/httpd} RETVAL=0 start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} -d 10 $httpd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} } reload() { echo -n $"Reloading $prog: " if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then RETVAL=$? echo $"not reloading due to configuration syntax error" failure $"not reloading $httpd due to configuration syntax error" else killproc -p ${pidfile} $httpd -HUP RETVAL=$? fi echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart) if [ -f ${pidfile} ] ; then stop start fi ;; reload) reload ;; graceful|help|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" exit 1 esac exit $RETVAL #给脚本添加执行权限,并添加到开机自启动 [root@apache ~]# chmod +x /etc/init.d/httpd [root@apache ~]# chkconfig --add httpd [root@apache ~]# chkconfig httpd on #启动httpd服务 [root@apache ~]# echo "ServerName localhost:80" >>/etc/httpd/httpd.conf [root@apache ~]# service httpd start Starting httpd: [ OK ] [root@apache ~]# service iptables stop
三、负载均衡实现
两种方式都能实现负载均衡:
基于mod_proxy模块实现负载均衡
基于mod_jk模块实现负载均衡
下面我们就来分别演示一下,
1.基于mod_proxy实现负载均衡
首先我们来修改一下httpd.conf配置文件
[root@apache ~]# vim /etc/httpd/httpd.conf Include /etc/httpd/extra/httpd-proxy.conf #增加这一行
编辑httpd-proxy.conf文件
[root@apache ~]# cat /etc/httpd/extra/httpd-proxy.conf ProxyRequests OffBalancerMember ajp://192.168.3.74:8009 loadfactor=1 route=TomcatA BalancerMember ajp://192.168.3.75:8009 loadfactor=1 route=TomcatB ProxySet lbmethod=bytraffic ServerAdmin [email protected] ServerName www.test.com ProxyPass / balancer://lbcluster/ ProxyPa***everse / balancer://lbcluster/ stickysession=JSESSIONID Require all granted Require all granted
注,关于如上apache指令的说明:
ProxyPreserveHost {On|Off}:如果启用此功能,代理会将用户请求报文中的Host:行发送给后端的服务器,而不再使用ProxyPass指定的服务器地址。如果想在反向代理中支持虚拟主机,则需要开启此项,否则就无需打开此功能。
ProxyVia {On|Off|Full|Block}:用于控制在http首部是否使用Via:,主要用于在多级代理中控制代理请求的流向。默认为Off,即不启用此功能;On表示每个请求和响应报文均添加Via:;Full表示每个Via:行都会添加当前apache服务器的版本号信息;Block表示每个代理请求报文中的Via:都会被移除。
ProxyRequests {On|Off}:是否开启apache正向代理的功能;启用此项时为了代理http协议必须启用mod_proxy_http模块。同时,如果为apache设置了ProxyPass,则必须将ProxyRequests设置为Off。
ProxyPass [path] !|url [key=value key=value ...]]:将后端服务器某URL与当前服务器的某虚拟路径关联起来作为提供服务的路径,path为当前服务器上的某虚拟路径,url为后端服务器上某URL路径。使用此指令时必须将ProxyRequests的值设置为Off。需要注意的是,如果path以“/”结尾,则对应的url也必须以“/”结尾,反之亦然。
另外,mod_proxy模块在httpd 2.1的版本之后支持与后端服务器的连接池功能,连接在按需创建在可以保存至连接池中以备进一步使用。连接池大小或其它设定可以通过在ProxyPass中使用key=value的方式定义。常用的key如下所示:
min:连接池的最小容量,此值与实际连接个数无关,仅表示连接池最小要初始化的空间大小。
max:连接池的最大容量,每个MPM都有自己独立的容量;都值与MPM本身有关,如Prefork的总是为1,而其它的则取决于ThreadsPerChild指令的值。
loadfactor:用于负载均衡集群配置中,定义对应后端服务器的权重,取值范围为1-100。
retry:当apache将请求发送至后端服务器得到错误响应时等待多长时间以后再重试。单位是秒钟。
如果Proxy指定是以balancer://开头,即用于负载均衡集群时,其还可以接受一些特殊的参数,如下所示:
lbmethod:apache实现负载均衡的调度方法,默认是byrequests,即基于权重将统计请求个数进行调度,bytraffic则执行基于权重的流量计数调度,bybusyness通过考量每个后端服务器的当前负载进行调度。
maxattempts:放弃请求之前实现故障转移的次数,默认为1,其最大值不应该大于总的节点数。
nofailover:取值为On或Off,设置为On时表示后端服务器故障时,用户的session将损坏;因此,在后端服务器不支持session复制时可将其设置为On。
stickysession:调度器的sticky session的名字,根据web程序语言的不同,其值为JSESSIONID或PHPSESSIONID。
上述指令除了能在banlancer://或ProxyPass中设定之外,也可使用ProxySet指令直接进行设置,如:
BalancerMember http://www1.test.com:8080 loadfactor=1 BalancerMember http://www2.test.com:8080 loadfactor=2 ProxySet lbmethod=bytraffic
ProxyPa***everse:用于让apache调整HTTP重定向响应报文中的Location、Content-Location及URI标签所对应的URL,在反向代理环境中必须使用此指令避免重定向报文绕过proxy服务器。
好了,配置文件说明就到这里。接下来我们检查一下配置文件并重启httpd
[root@apache ~]# /usr/local/apache/bin/httpd -t Syntax OK [root@apache ~]# service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
访问结果
好了,大家可以看到基于mod_proxy模块的tomcat负载均衡配置完成。下面我们来配置一下基于mod_jk的模块的负载均衡!
2.基于mod_jk模块实现负载均衡
说明:
为了避免用户直接访问后端Tomcat实例,影响负载均衡的效果,建议在Tomcat 7的各实例上禁用HTTP/1.1连接器。
为每一个Tomcat 7实例的引擎添加jvmRoute参数,并通过其为当前引擎设置全局惟一标识符。如下所示。需要注意的是,每一个实例的jvmRoute的值均不能相同。
安装mod_jk
[root@apache ~]# wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.40-src.tar.gz [root@apache ~]# tar xf tomcat-connectors-1.2.40-src.tar.gz [root@apache ~]# cd tomcat-connectors-1.2.40-src/native/ [root@apache native]# ./configure --with-apxs=/usr/local/apache/bin/apxs [root@apache native]# make && make install
同样的首先来修改httpd.conf配置文件
#禁用前面的httpd-proxy.conf文件,启用httpd-jk.conf文件 [root@apache ~]# vim /etc/httpd/httpd.conf #Include /etc/httpd/extra/httpd-proxy.conf Include /etc/httpd/extra/httpd-jk.conf #编辑httpd-jk.conf文件 [root@apache ~]# cat /etc/httpd/extra/httpd-jk.conf LoadModule jk_module modules/mod_jk.so #加载mod_jk模块 JkWorkersFile /etc/httpd/extra/workers.properties #配置文件位置 JkLogFile logs/mod_jk.log #日志 JkLogLevel debug #日志级别 JkMount /* lbcluster #负载均衡器名称 JkMount /jkstatus/ stat1 #状态信息
接下来,编辑/etc/httpd/extra/workers.properties,添加如下内容:
[root@apache ~]# cat /etc/httpd/extra/workers.properties worker.list = lbcluster,stat1 #列表信息 worker.TomcatA.type = ajp13 #支持ajp协议 worker.TomcatA.host = 192.168.3.74 #TomcatA实例IP worker.TomcatA.port = 8009 #TomcatA实例端口号 worker.TomcatA.lbfactor = 1 #负载均衡权重为1 worker.TomcatB.type = ajp13 worker.TomcatB.host = 192.168.3.75 worker.TomcatB.port = 8009 worker.TomcatB.lbfactor = 1 worker.lbcluster.type = lb #负载均衡work,lb内置的类 worker.lbcluster.sticky_session = 0 #会话是否绑定 worker.lbcluster.balance_workers = TomcatA, TomcatB #TomcatA, TomcatB 集群中的实列 worker.stat1.type = status #状态信息
注,apache相关指令说明:
workers.properties文件一般由两类指令组成:一是mod_jk可以连接的各worker名称列表,二是每一个worker的属性配置信息。它们分别遵循如下使用语法。
worker.list = < a comma separated list of worker names > worker.. =
其中worker.list指令可以重复指定多次,而worker name则是Tomcat中engine组件jvmRoute参数的值。如:
worker.TomcatA.host=172.16.100.1
根据其工作机制的不同,worker有多种不同的类型,这是需要为每个worker定义的一项属性woker.
◇ ajp13:此类型表示当前worker为一个运行着的Tomcat实例。
◇ lb:lb即load balancing,专用于负载均衡场景中的woker;此worker并不真正负责处理用户请求,而是将用户请求调度给其它类型为ajp13的worker。
◇ status:用户显示分布式环境中各实际worker工作状态的特殊worker,它不处理任何请求,也不关联到任何实际工作的worker实例。具体示例如请参见后文中的配置。
worker其它常见的属性说明:
◇ host:Tomcat 7的worker实例所在的主机;
◇ port:Tomcat 7实例上AJP1.3连接器的端口;
◇ connection_pool_minsize:最少要保存在连接池中的连接的个数;默认为pool_size/2;
◇ connection_pool_timeout:连接池中连接的超时时长;
◇ mount:由当前worker提供的context路径,如果有多个则使用空格格开;此属性可以由JkMount指令替代;
◇ retries:错误发生时的重试次数;
◇ socket_timeout:mod_jk等待worker响应的时长,默认为0,即无限等待;
◇ socket_keepalive:是否启用keep alive的功能,1表示启用,0表示禁用;
◇ lbfactor:worker的权重,可以在负载均衡的应用场景中为worker定义此属性;
另外,在负载均衡模式中,专用的属性还有:
◇balance_workers:用于负载均衡模式中的各worker的名称列表,需要注意的是,出现在此处的worker名称一定不能在任何worker.list属性列表中定义过,并且worker.list属性中定义的worker名字必须包含负载均衡worker。具体示例请参见后文中的定义。
◇ method:可以设定为R、T或B;默认为R,即根据请求的个数进行调度;T表示根据已经发送给worker的实际流量大小进行调度;B表示根据实际负载情况进行调度。
◇sticky_session:在将某请求调度至某worker后,源于此址的所有后续请求都将直接调度至此worker,实现将用户session与某worker绑定。默认为值为1,即启用此功能。如果后端的各worker之间支持session复制,则可以将此属性值设为0。
下面我们来检查一下配置文件并重新启动一下httpd
[root@apache ~]# /usr/local/apache/bin/httpd -t Syntax OK [root@apache ~]# service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
测试结果