Apache的负载均衡之mod_jk模块实现
先配置mod_jk的代理功能,
[root@org ~]# yum install -y httpd-devel
[root@org ~]# rpm -ql httpd-devel
/usr/sbin/apxs
apxs是httpd安装外部模块必备的
[root@org ~]# yum groupinstall"Development Tools" "Server Platform Development" -y //所需要的包组。
编译安装需要的环境。
[root@org ~]# tar xftomcat-connectors-1.2.40-src.tar.gz
[root@org ~]# ls
a install.log oracle
apache-tomcat-7.0.56.tar.gz install.log.syslog root20141030092112523.zip
apache-tomcat-8.0.20.tar.gz jdk-8u31-linux-i586.rpm tomcat-connectors-1.2.40-src
b lamp tomcat-connectors-1.2.40-src.tar.gz
[root@org ~]# cd tomcat-connectors-1.2.40-src
[root@org tomcat-connectors-1.2.40-src]# ls
conf docs HOWTO-RELEASE.txt jkstatus LICENSE native NOTICE README.txt support tools xdocs
[root@org tomcat-connectors-1.2.40-src]# cdnative/
[root@org native]# ls
aclocal.m4 apache-2.0 BUILDING.txt configure docs Makefile.am netscape scripts TODO.txt
apache-1.3 buildconf.sh common configure.ac iis Makefile.in README.txt STATUS.txt
[root@org native]# ./configure--with-apxs=/usr/sbin/apxs
[root@org native]# make && makeinstall
。
。
。
----------------------------------------------------------------------
chmod 755/usr/lib64/httpd/modules/mod_jk.so
Please be sure to arrange/etc/httpd/conf/httpd.conf...
make[1]: Leaving directory`/root/tomcat-connectors-1.2.40-src/native/apache-2.0'
make[1]: Entering directory`/root/tomcat-connectors-1.2.40-src/native'
make[2]: Entering directory`/root/tomcat-connectors-1.2.40-src/native'
make[2]: Nothing to be done for`install-exec-am'.
make[2]: Nothing to be done for`install-data-am'.
make[2]: Leaving directory`/root/tomcat-connectors-1.2.40-src/native'
make[1]: Leaving directory`/root/tomcat-connectors-1.2.40-src/native'
[root@org ~]# cd /etc/httpd
[root@org httpd]# ls
conf conf.d logs modules run
[root@org httpd]# cd conf.d/
[root@org conf.d]# ls
php.conf README welcome.conf
[root@org conf.d]# vim mod_jk.conf
# Load the mod_jk
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/ statA
~
[root@org conf.d]# vim/etc/httpd/conf.d/workers.properties
worker.list = TomcatA,statA
worker.TomcatA.port=8009
worker.TomcatA.host=192.168.1.116
worker.TomcatA.type=ajp13
worker.TomcatA.lbfactor=1
worker.statA.type = status
[root@org conf.d]# httpd -t
Syntax OK
[root@org conf.d]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@org conf.d]#
至此,一个基于mod_jk模块与后端名为TomcatA的worker通信的配置已经完成,重启httpd服务即可生效。
还有状态信息
要实现负载均衡,至少需要两个tomcat还在本机上操作,配置另一个版本的tomcat,配置完成之后访问http://192.168.1.116:9080
mod_jk可直接支持负载均衡,需要启用一个特殊型的worker:lb。
[root@org conf.d]# vim mod_jk.conf
#Load the mod_jk
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/ statA
~
[root@org conf.d]# vim workers.properties
worker.list = lbcluster1,statA
worker.TomcatA.port=8009
worker.TomcatA.host=192.168.1.116
worker.TomcatA.type=ajp13
worker.TomcatA.lbfactor=1
worker.TomcatB.port=9009
worker.TomcatB.host=192.168.1.116
worker.TomcatB.type=ajp13
worker.TomcatB.lbfactor=1
worker.lbcluster1.type = lb
sorker.lbcluster1.sticky_session = 0
worker.lbcluster1.balance_workers =TomcatA, TomcatB
worker.statA.type = status
~
[root@org conf.d]# httpd -t
Syntax OK
[root@org bin]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@org bin]#
到此mod_jk实现负载均衡就配置好了,测试如下。
访问http://192.168.1.116
再次刷新