我想使用apache负载平衡器,以便一个tomcat服务器是主要的,并且辅助服务器只有在第一个服务器closures时才联机。 如何才能做到这一点?
如果你只是要做负载平衡,我会推荐比Apache更轻量的东西,比如HAproxy ,但是让我们假设你将会使用apache。
有几种负载平衡的方法,所以你应该花一点时间来熟悉它们。 有一篇由Willy Tarreau提供的优秀文章,称为使应用程序可升级的负载平衡 ,值得您花时间阅读。
有一些使Apache成为负载平衡器的工具,也许最简单的(我知道的)是使用mod_proxy_balancer 。 唯一的问题是,它似乎没有做故障转移负载平衡(这是你想要的:高可用性,而不是真正的负载平衡)。
既然你在使用Tomcat,为什么不使用mod_jk ,Apache的tomcat连接器呢?
假设我们正在使用基于Redhat的系统(我使用的是CentOS 5.5),它被configuration为编译东西(例如,有gcc和相应的库)(这在生产系统上不是个好主意。虽然这不是讨论RPM包装的时间或地点)
yum install httpd httpd-devel
这将安装Apache以及用于制作模块的开发工具,configuration目录将是/ etc / httpd /
目前(20110130)Tomcat连接器是1.2.31(但你可以在这里查看最新的),所以下载它们:
wget http://mirror.cc.columbia.edu/pub/software/apache//tomcat/tomcat-connectors/jk/source/jk-1.2.31/tomcat-connectors-1.2.31-src.tar.gz
提取,编译和安装它们:
tar zxvf tomcat-connectors-1.2.31-src.tar.gz cd tomcat-connectors-1.2.31-src/native ./configure --with-apxs=/usr/sbin/apxs make sudo make install
确认已经安装了mod_jk.so:
ls -al /etc/httpd/modules/mod_jk.so -rwxr-xr-x 1 root root 903072 Jan 30 15:21 /etc/httpd/modules/mod_jk.so
现在,我们实际上configuration它。
编辑/etc/httpd/conf.d/jk.conf:
# This actually tells apache to load the module we built LoadModule jk_module modules/mod_jk.so # This file holds the instructions for which servers the proxy will be talking to JKWorkersFile /etc/httpd/conf.d/workers.properties # This is (obviously) the logfile that it will write to. Change to whatever you want JKLogFile /var/log/httpd/mod_jk.log # Simplistically, we'll set up a virtualhost that listens on all IPs on port 80. # HTTPS is left as an exercise for the reader NameVirtualHost *:80 <VirtualHost *:80> ServerAdmin [email protected] ServerName thedomainnameoftheproxy.com # The following line says "send anything to the JK Worker named MyProxyCluster" JKMount /* MyProxyCluster </VirtualHost> #EOF
好的,告诉Apache我们想要使用mod_jk,我们要使用一个名为/etc/httpd/conf.d/workers.properties的文件,并且我们有一个虚拟主机,一切都发送给worker。 太棒了,现在让我们configuration工人。 编辑/etc/httpd/conf.d/workers.properties:
# We're telling mod_jk which workers it needs to look for. We're going to define # a total of 3 workers: MyProxyCluster (the load balancer job), worker1, and worker2 # (the two backend servers) worker.list=MyProxyCluster worker.worker1.port=8009 worker.worker1.host=hostname.or.ip.of.1st.tomcat.server # ajp13 means Apache JServ Protocol version 1.3, and is apparently universal worker.worker1.type=ajp13 worker.worker1.lbfactor=1 # When worker1 dies, we're going to want it to go to worker2 worker.worker1.redirect=worker2 ### End of worker1 config # repeat the same things for worker2 worker.worker2.port=8009 worker.worker2.host=hostname.of.ip.of.2nd.tomcat.server worker.worker2.type=ajp13 worker.worker2.lbfactor=1 # Disable worker2 except when it fails over worker.worker2.activation=disabled # make the actual load balancer worker that we referenced in the beginning worker.MyProxyCluster.type=lb worker.MyProxyCluster.balance_workers=worker1,worker2 # EOF
现在,我们有两个实际的工人,一个虚拟负载平衡器。 你现在可以启动Apache了:
service httpd start
通过浏览到机器进行testing,然后转到第一个tomcat服务器。 你应该可以通过观看/var/log/httpd/mod_jk.log(或者你指向的任何地方)来validation。
祝你好运!
这里使用AJP而不是JK更简单的解决scheme:
<Proxy balancer://cluster> BalancerMember ajp://127.0.0.1:8009 route=jvm1 status=-D BalancerMember ajp://127.0.0.2:8009 route=jvm2 status=+H </Proxy> ProxyPass /app balancer://cluster/app
还可以设置更多的configuration属性,例如,如果有更多的后端,不同的平衡algorithm,平衡器集等,请参阅Apache mod_proxy文档,但上述内容与您指定的内容完全相同。 -D表示“未禁用”,+ H表示“热备份”。 你也可以在运行时交换它们。
你只需要运行两个Tomcat,在端口8009上运行的AJP连接器的地址=“127.0.0.1”,地址=“127.0.0.2”等等,尽可能多的用<Engine jvmRoute="jvm1">和<Engine jvmRoute="jvm2">分别。 注意端口的经济性:您也可以对closures端口执行相同的操作。 扔掉Tomcatconfiguration中引用JK,mod_jk等的任何东西。你也不需要这些可怕的workers.properties文件。 您也可以丢弃所有的HTTP和HTTPS连接器,并在Apache HTTPD中终止入站SSL,这样可以更好地控制SSL。