来源:网络收集
一般来说,负载均衡就是将客户端的请求分流给后端的各个真实服务器,达到负载均衡的目的。还有一种方式是用两台服务器,一台作为主服务器 (Master),另一台作为热备份(Hot Standby),请求全部分给主服务器,在主服务器当机时,立即切换到备份服务器,以提高系统的整体可
第一次看到这个标题时我也很惊讶,Apache居然还能做负载均衡?真是太强大了。经过一番调查后发现的确可以,而且功能一点都不差。这都归功于 mod_proxy 这个模块。不愧是强大的Apache啊。
废话少说,下面就来解释一下负载均衡的设置方法。
一般来说,负载均衡就是将客户端的请求分流给后端的各个真实服务器,达到负载均衡的目的。还有一种方式是用两台服务器,一台作为主服务器 (Master),另一台作为热备份(Hot Standby),请求全部分给主服务器,在主服务器当机时,立即切换到备份服务器,以提高系统的整体可靠性。
1. 负载均衡的设置
1).基本配置
Apache可以应对上面这两种需求。先来讨论一下如何做负载均衡。假设一台apache服务器域名为www.a.com, 首先需要启用Apache的几个模块:
Httpd.conf代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://koda.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1253944211015" src="http://koda.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=LoadModule%20proxy_module%20modules%2Fmod_proxy.so%0ALoadModule%20proxy_balancer_module%20modules%2Fmod_proxy_balancer.so%0ALoadModule%20proxy_http_module%20modules%2Fmod_proxy_http.so%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">
- LoadModule proxy_module modules/mod_proxy.so
- LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
- LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
mod_proxy提供代理服务器功能,mod_proxy_balancer提供负载均衡功能, mod_proxy_http让代理服务器能支持HTTP协议。如果把mod_proxy_http换成其他协议模块(如mod_proxy_ftp),或许能支持其他协议的负载均衡,有兴趣的朋友可以自己尝试一下。
然后要添加以下配置:
Httpd.conf代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://koda.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1253944211056" src="http://koda.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=ProxyRequests%20Off%0A%3CProxy%20balancer%3A%2F%2Fmycluster%3E%0A%20%20%20%20BalancerMember%20http%3A%2F%2Fnode-a.myserver.com%3A8080%0A%20%20%20%20BalancerMember%20http%3A%2F%2Fnode-b.myserver.com%3A8080%0A%3C%2FProxy%3E%0AProxyPass%20%2F%20balancer%3A%2F%2Fmycluster%2F%0A%23%20%E8%AD%A6%E5%91%8A%EF%BC%9A%E4%BB%A5%E4%B8%8B%E8%BF%99%E6%AE%B5%E9%85%8D%E7%BD%AE%E4%BB%85%E7%94%A8%E4%BA%8E%E8%B0%83%E8%AF%95%EF%BC%8C%E7%BB%9D%E4%B8%8D%E8%A6%81%E6%B7%BB%E5%8A%A0%E5%88%B0%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%EF%BC%81%EF%BC%81%EF%BC%81%0A%3CLocation%20%2Fbalancer-manager%3E%0A%20%20%20%20SetHandler%20balancer-manager%0A%20%20%20%20order%20Deny%2CAllow%0A%20%20%20%20Deny%20from%20all%0A%20%20%20%20Allow%20from%20localhost%0A%3C%2FLocation%3E%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">
- ProxyRequests Off
- <Proxy balancer://mycluster>
- BalancerMember http://node-a.myserver.com:8080
- BalancerMember http://node-b.myserver.com:8080
- </Proxy>
- ProxyPass / balancer://mycluster/
- # 警告:以下这段配置仅用于调试,绝不要添加到生产环境中!!!
- <Location /balancer-manager>
- SetHandler balancer-manager
- order Deny,Allow
- Deny from all
- Allow from localhost
- </Location>
ProxyRequests Off
<Proxy balancer://mycluster>
BalancerMember http://node-a.myserver.com:8080
BalancerMember http://node-b.myserver.com:8080
</Proxy>
ProxyPass / balancer://mycluster/
# 警告:以下这段配置仅用于调试,绝不要添加到生产环境中!!!
<Location /balancer-manager>
SetHandler balancer-manager
order Deny,Allow
Deny from all
Allow from localhost
</Location>
注:node-a.myserver.com,node-b.myserver.com是另外两台服务器的域名,不是当前服务器的域名
从上面的 ProxyRequests Off 这条可以看出,实际上负载均衡器就是一个反向代理,只不过它的代理转发地址不是某台具体的服务器,而是一个 balancer:// 协议:
ProxyPass / balancer://mycluster协议地址可以随便定义。然后,在<Proxy>段中设置该balancer协议的内容即可。 BalancerMember指令可以添加负载均衡组中的真实服务器地址。
下面那段<Location /balancer-manager>是用来监视负载均衡的工作情况的,调试时可以加上(生产环境中禁止使用!),然后访问 http://localhost/balancer-manager/ 即可看到负载均衡的工作状况。
OK,改完之后重启服务器,访问你的Apache所在服务器的地址(www.a.com),即可看到负载均衡的效果了。
出错提示:
访问网页提示Internal Serveral Error,察看error.log文件
Error.log代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://koda.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1253944211071" src="http://koda.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%5Bwarn%5D%20proxy%3A%20No%20protocol%20handler%20was%20valid%20for%20the%20URL%20%2Fadmin%2Flogin_form.%20If%20you%20are%20using%20a%20DSO%20version%20of%20mod_proxy%2C%20make%20sure%20the%20proxy%20submodules%20are%20included%20in%20the%20configuration%20using%20LoadModule.%20%20" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">
- [warn] proxy: No protocol handler was valid for the URL /admin/login_form. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule.
[warn] proxy: No protocol handler was valid for the URL /admin/login_form. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule.
原因是配置:# ProxyPass / balancer://mycluster 可能少了一个/
2). 负载比例分配
打开 balancer-manager 的界面,可以看到请求是平均分配的。
如果不想平均分配怎么办?给 BalancerMember 加上 loadfactor 参数即可,取值范围为1-100。比如你有三台服务器,负载分配比例为 7:2:1,只需这样设置:
Httpd.conf代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://koda.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1253944211086" src="http://koda.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=ProxyRequests%20Off%0A%3CProxy%20balancer%3A%2F%2Fmycluster%3E%0A%20%20%20%20BalancerMember%20http%3A%2F%2Fnode-a.myserver.com%3A8080%20loadfactor%3D7%0A%20%20%20%20BalancerMember%20http%3A%2F%2Fnode-b.myserver.com%3A8080%20loadfactor%3D2%0A%20%20%20%20BalancerMember%20http%3A%2F%2Fnode-c.myserver.com%3A8080%20loadfactor%3D1%0A%3C%2FProxy%3E%0AProxyPass%20%2F%20balancer%3A%2F%2Fmycluster%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">
- ProxyRequests Off
- <Proxy balancer://mycluster>
- BalancerMember http://node-a.myserver.com:8080 loadfactor= 7
- BalancerMember http://node-b.myserver.com:8080 loadfactor= 2
- BalancerMember http://node-c.myserver.com:8080 loadfactor= 1
- </Proxy>
- ProxyPass / balancer://mycluster
ProxyRequests Off
<Proxy balancer://mycluster>
BalancerMember http://node-a.myserver.com:8080 loadfactor=7
BalancerMember http://node-b.myserver.com:8080 loadfactor=2
BalancerMember http://node-c.myserver.com:8080 loadfactor=1
</Proxy>
ProxyPass / balancer://mycluster
3).负载分配算法
默认情况下,负载均衡会尽量让各个服务器接受的请求次数满足预设的比例。如果要改变算法,可以使用 lbmethod 属性。如:
Httpd.conf代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://koda.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1253944211100" src="http://koda.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=ProxyRequests%20Off%0A%3CProxy%20balancer%3A%2F%2Fmycluster%3E%0A%20%20%20%20BalancerMember%20http%3A%2F%2Fnode-a.myserver.com%3A8080%20loadfactor%3D7%0A%20%20%20%20BalancerMember%20http%3A%2F%2Fnode-b.myserver.com%3A8080%20loadfactor%3D2%0A%20%20%20%20BalancerMember%20http%3A%2F%2Fnode-c.myserver.com%3A8080%20loadfactor%3D1%0A%3C%2FProxy%3E%0AProxyPass%20%2F%20balancer%3A%2F%2Fmycluster%0AProxySet%20lbmethod%3Dbytraffic%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">
- ProxyRequests Off
- <Proxy balancer://mycluster>
- BalancerMember http://node-a.myserver.com:8080 loadfactor= 7
- BalancerMember http://node-b.myserver.com:8080 loadfactor= 2
- BalancerMember http://node-c.myserver.com:8080 loadfactor= 1
- </Proxy>
- ProxyPass / balancer://mycluster
- ProxySet lbmethod=bytraffic
ProxyRequests Off
<Proxy balancer://mycluster>
BalancerMember http://node-a.myserver.com:8080 loadfactor=7
BalancerMember http://node-b.myserver.com:8080 loadfactor=2
BalancerMember http://node-c.myserver.com:8080 loadfactor=1
</Proxy>
ProxyPass / balancer://mycluster
ProxySet lbmethod=bytraffic
lbmethod可能的取值有:
lbmethod=byrequests 按照请求次数均衡(默认)
lbmethod=bytraffic 按照流量均衡
lbmethod=bybusyness 按照繁忙程度均衡(总是分配给活跃请求数最少的服务器)
各种算法的原理请参见 Apache的文档 。
2. 热备份(Hot Standby)
热备份的实现很简单,只需添加 status=+H 属性,就可以把某台服务器指定为备份服务器:
Httpd.conf代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://koda.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1253944211137" src="http://koda.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=ProxyRequests%20Off%0A%3CProxy%20balancer%3A%2F%2Fmycluster%3E%0A%20%20%20%20BalancerMember%20http%3A%2F%2Fnode-a.myserver.com%3A8080%0A%20%20%20%20BalancerMember%20http%3A%2F%2Fnode-b.myserver.com%3A8080%20status%3D%2BH%0A%3C%2FProxy%3E%0AProxyPass%20%2F%20balancer%3A%2F%2Fmycluster%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">
- ProxyRequests Off
- <Proxy balancer://mycluster>
- BalancerMember http://node-a.myserver.com:8080
- BalancerMember http://node-b.myserver.com:8080 status=+H
- </Proxy>
- ProxyPass / balancer://mycluster
ProxyRequests Off
<Proxy balancer://mycluster>
BalancerMember http://node-a.myserver.com:8080
BalancerMember http://node-b.myserver.com:8080 status=+H
</Proxy>
ProxyPass / balancer://mycluster
从 balancer-manager 界面中可以看到,请求总是流向 node-a ,一旦node-a挂掉, Apache会检测到错误并把请求分流给 node-b。Apache会每隔几分钟检测一下 node-a 的状况,如果node-a恢复,就继续使用node-a。