apache实现负载均衡
apache一般采用代理和反向代理方式做负载均衡
1、启用Apache的几个模块
httpd.conf代码
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
如果modules目录下没有以上文件,说明没有安装,需要先安装
进入apache的源代码解压目录
cdapache的解压目录/modules/proxy
/usr/local/apache/bin/apxs-c-imod_proxy.cproxy_util.c
注意:上面如果不加proxy_util.c的话,LoadModuleproxy_modulemodules/mod_proxy.so会报错的,提示你找不到mod_proxy.so文件
/usr/local/apache2/bin/apxs-c-imod_proxy_balancer.c
/usr/local/apache2/bin/apxs-c-imod_proxy_http.c
2、配置负载均衡
httpd.conf代码
ServerName uhz001710.chinaw3.com ProxyRequests off <Proxy balancer://mycluster/> BalancerMember http://172.21.1.101:8080/ BalancerMember http://172.21.1.102:8080/ BalancerMember http://172.21.1.103:8080/ </Proxy> ProxyPass /balancer-manager ! ProxyPass / balancer://mycluster/ ProxyPassReverse / balancer://mycluster/ #警告:以下这段配置仅用于调试,绝不要添加到生产环境中!!! #<Location /balancer-manager/> #SetHandler /balancer-manager/ #Order Deny,Allow #Deny from all #Allow from all #</Location>
2).负载比例分配
打开balancer-manager的界面,可以看到请求是平均分配的。
如果不想平均分配怎么办?给BalancerMember加上loadfactor参数即可,取值范围为1-100。比如你有三台服务器,负载分配比例为7:2:1,只需这样设置:
Httpd.conf代码
ProxyRequests Off <Proxy balancer://mycluster/> BalancerMember http://172.21.1.101:8080 loadfactor=7 BalancerMember http://172.21.1.102:8080 loadfactor=2 BalancerMember http://172.21.1.103:8080 loadfactor=1 </Proxy> ProxyPass / balancer://mycluster/
3).负载分配算法
默认情况下,负载均衡会尽量让各个服务器接受的请求次数满足预设的比例。如果要改变算法,可以使用lbmethod属性。如:
Httpd.conf代码
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按照繁忙程度均衡(总是分配给活跃请求数最少的服务器)
相关推荐
azhuye 2020-11-12
liupengqwert 2020-10-28
YzhilongY 2020-08-31
crazyjingling 2020-08-16
swtIrene 2020-08-14
slovyz 2020-08-14
tinydu 2020-08-09
tinydu 2020-08-03
Studynutlee 2020-08-03
快乐de馒头 2020-07-29
yungame 2020-07-27
buaichidoufu 2020-07-28
wanjichun 2020-07-26
极地雪狼 2020-07-26
yungame 2020-07-04
畅聊架构 2020-06-28
极地雪狼 2020-06-27
廖先贵 2020-06-23