apache负载均衡服务器备份

use*_*145 3 load-balancing apache-2.2

我想使用 apache 负载平衡器,以便一个 tomcat 服务器是主服务器,只有当第一台服务器关闭时,辅助服务器才在线。如何才能做到这一点?

Mat*_*ons 8

如果你只想做负载平衡,我会推荐比 Apache 更轻量级的东西,比如HAproxy,但让我们假设您将使用 apache。

负载平衡有多种方法,因此您应该花一点时间来熟悉它们。Willy Tarreau 提供了一篇名为“使用负载平衡使应用程序可扩展”的优秀文章,值得您花时间阅读。

有几个工具可以让 Apache 成为负载均衡器,也许最简单的(我知道的)是使用mod_proxy_balancer。唯一的问题是它似乎没有进行故障转移负载平衡(这是您想要的:高可用性,而不是真正的负载平衡)。

既然您使用的是 Tomcat,为什么不使用mod_jk Apache tomcat 连接器呢?

假设我们在一个基于 Redhat 的系统上(我使用的是 CentOS 5.5),它被配置为编译东西(例如,有 gcc 和适当的库(这在生产系统上不是一个好主意。不过,这不是讨论RPM 包装的时间或地点)

 yum install httpd httpd-devel
Run Code Online (Sandbox Code Playgroud)

这将安装 Apache 以及用于制作模块的开发材料,配置目录将是 /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
Run Code Online (Sandbox Code Playgroud)

提取、编译和安装它们:

 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 
Run Code Online (Sandbox Code Playgroud)

验证 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
Run Code Online (Sandbox Code Playgroud)

现在,我们开始实际配置它。

编辑/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 you@yourdomain.com 
     ServerName thedomainnameoftheproxy.com

     # The following line says "send anything to the JK Worker named MyProxyCluster"
     JKMount /* MyProxyCluster

 </VirtualHost>
 #EOF
Run Code Online (Sandbox Code Playgroud)

好的,这告诉 Apache 我们要使用 mod_jk,我们要使用一个名为 /etc/httpd/conf.d/workers.properties 的文件,并且我们有一个虚拟主机,所有内容都发送给工作人员。太好了,现在让我们配置工作人员。编辑 /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
Run Code Online (Sandbox Code Playgroud)

现在,我们有两个实际的工作人员和一个虚拟负载均衡器。您现在可以启动 Apache:

 service httpd start 
Run Code Online (Sandbox Code Playgroud)

通过浏览到机器进行测试,它应该转到第一个 tomcat 服务器。您应该能够通过查看 /var/log/httpd/mod_jk.log(或您指向的任何地方)来验证这一点。

祝你好运!