码迷,mamicode.com
首页 > 其他好文 > 详细

centos之Haproxy 负载均衡学习笔记

时间:2016-08-14 07:42:40      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:

HAProxy的特点是:
1、支持两种代理模式:TCP(四层)和HTTP(七层),支持虚拟主机;
2、能够补充Nginx的一些缺点比如Session的保持,Cookie的引导等工作
3、支持url检测后端的服务器出问题的检测会有很好的帮助。
4、更多的负载均衡策略比如:动态加权轮循(Dynamic Round Robin),加权源地址哈希(Weighted Source Hash),加权URL哈希和加权参数哈希(Weighted Parameter Hash)已经实现
5、单纯从效率上来讲HAProxy更会比Nginx有更出色的负载均衡速度。
6、HAProxy可以对Mysql进行负载均衡,对后端的DB节点进行检测和负载均衡。
9、支持负载均衡算法:Round-robin(轮循)、Weight-round-robin(带权轮循)、source(原地址保持)、RI(请求URL)、rdp-cookie(根据cookie)
10、不能做Web服务器即Cache。

源码方式安装Haproxy

#tar xzvf haproxy-1.4.17.tar.gz
#cd haproxy-1.4.17
#make TARGET=linux26
#make install

测试配置文件如下

技术分享
  1 global
  2     log    127.0.0.1 local0
  3 #    log    127.0.0.1 local1
  4     maxconn    4000
  5     ulimit-n 8000
  6     uid    0
  7     gid    0
  8 #    chroot    /tmp
  9 #    nbproc    2
 10 #    daemon
 11 #    debug
 12 #    quiet
 13 
 14 listen proxy1 192.168.207.128:8000
 15     mode    http
 16 #    source    127.0.0.2:0
 17 #    log    127.0.0.1 local0
 18 #    log    127.0.0.1 local1
 19     log global
 20     #mode    tcp
 21         cookie SERVERID insert indirect
 22     balance roundrobin
 23     #dispatch 127.0.0.1:3130
 24     #dispatch 127.0.0.1:31300
 25     #dispatch 127.0.0.1:80
 26     #dispatch 127.0.0.1:22
 27     option httpchk
 28 #    server test 127.0.0.1:80 cookie cookie1 check inter 300
 29 #        server web02 192.168.0.104:80 cookie cookie2 check inter 300
 30 server  web01 127.0.0.1:80 cookie cookie1  check inter 2000 fall 3 weight 30              #定义的多个后端
 31 server  web02 192.168.0.104:80 cookie cookie2 check inter 2000 fall 3 weight 30              #定义的多个后端
 32 
 33 #    server nc 127.0.0.1:8080 cookie cookie1 check inter 300
 34 #    server tuxlocal0 10.101.23.9:80 cookie cookie1 check
 35 #    server tuxlocal1 127.0.0.1:80 cookie cookie1 check
 36 #    server tuxlocal2 127.0.0.1:80 cookie cookie2 check
 37 #    server tuxlocal3 127.0.0.1:80 cookie cookie3 check
 38 #    server tuxlocal4 127.0.0.1:80 cookie cookie4 check
 39 #    server vax 10.101.14.1:80 cookie cookie1 check
 40     #server tuxceleron 10.101.0.1:80 cookie cookie2 check
 41     #server telnet 127.0.0.1:23
 42     #server ssh 127.0.0.1:22
 43     #server local 127.0.0.1:3130 cookie cookie3 check
 44     #server ko 127.0.0.1:0 cookie cookie3 check
 45     #server local 127.0.0.1:8001 cookie cookie3 check
 46     #server local 127.0.0.1:3130
 47     #server celeron 10.101.0.1:80 cookie srv1
 48     #server celeron 10.101.0.1:31300
 49     #server local 10.101.23.9:31300
 50     contimeout    3000
 51     clitimeout    150000
 52     srvtimeout    150000
 53     maxconn 60000
 54     option redispatch
 55     retries    3
 56     grace 3000
 57     #rsprep    ^Server.* Server:\ IIS
 58     #rspdel    ^Server.*
 59     #rspadd Set-Cookie:\ mycookie=0;\ path=/
 60     #rsprep ^(Date:\ )([^,]*)(,\ )(.*) LaDate\ est:\ \4\ (\2)
 61     # force connection:close
 62     #reqidel ^Connection:
 63     #rspidel ^Connection:
 64     #reqadd    Connection:\ close
 65     #rspadd    Connection:\ close
 66     # processing options
 67     #option    keepalive
 68     option    forwardfor
 69     option httplog
 70     option dontlognull
 71 #    reqirep ^(Test:\ ) \0_toto_\1_toto
 72 #    reqidel ^X-Forwarded-for:
 73 #    reqirep ^(GET|POST)\ .* \0
 74 #    reqirep ^(Host:|Connection:|User-agent:|Cookie:)\ .* \0
 75 #    reqideny ^
 76     
 77 listen proxy2 0.0.0.0:8001
 78     mode    http
 79     #mode    tcp
 80     dispatch 127.0.0.1:80
 81     #dispatch 127.0.0.1:31300
 82     #dispatch 127.0.0.1:80
 83     #dispatch 127.0.0.1:22
 84     #server tuxlocal 127.0.0.1:80 cookie cookie1 check
 85     #server tuxceleron 10.101.0.1:80 cookie cookie2 check
 86     #server telnet 127.0.0.1:23
 87     #server ssh 127.0.0.1:22
 88     #server local 127.0.0.1:3130 cookie cookie3 check
 89     #server local 127.0.0.1:3130
 90     #server celeron 10.101.0.1:80 cookie srv1
 91     #server celeron 10.101.0.1:31300
 92     #server local 10.101.23.9:31300
 93     contimeout    3000
 94     clitimeout    150000
 95     srvtimeout    150000
 96     maxconn 60000
 97     option redispatch
 98     retries    3
 99     grace 3000
100     #rsprep    ^Server.* Server:\ IIS
101     #rspdel    ^Server.*
102     rspadd Set-Cookie:\ SERVERID=12345678;\ path=/
103     #rsprep ^(Date:\ )([^,]*)(,\ )(.*) LaDate\ est:\ \4\ (\2)
104     
105 listen proxy3 0.0.0.0:3128
106     disabled
107     mode    http
108         cookie SERVERID insert indirect
109     #dispatch 127.0.0.1:8080
110     server srv1 127.0.0.1:8080
111      #server srv2 192.168.12.3:8080
112     contimeout    3000
113     clitimeout    450000
114     srvtimeout    450000
115     maxconn 60000
116     option redispatch
117     retries    3
118     grace 3000
119     rspdel ^Via:.*
120     monitor-net    192.168.12.252/30
121     
122 
123 listen proxy4 0.0.0.0:3129
124     disabled
125     mode    http
126     transparent
127 #    dispatch 127.0.0.1:80
128     contimeout    3000
129     clitimeout    150000
130     srvtimeout    150000
131     maxconn 60000
132     retries    3
133     grace 3000
134 
135 #    log    10.101.11.1 local1
136 #    log    10.101.11.1 local2
137 
138 #    cliexp    ^(.*ASPSESSIONID.*=)(.*) \1FENICGGCBECLFFEEOAEAIFGF
139 #    cliexp    ^(GET.*)(.free.fr)(.*) \1.online.fr\3
140 #    cliexp    ^(POST.*)(.free.fr)(.*) \1.online.fr\3
141 #    cliexp    ^Proxy-Connection:.*    Proxy-Connection:\ close
142 #    srvexp    ^(Location:\ )([^:]*://[^/]*)(.*) \1\3
143 
144 listen health 0.0.0.0:3130
145     mode    health
146     clitimeout    1500
147     srvtimeout    1500
148     maxconn 6000
149     grace 0
150 
151 
152 listen health2 0.0.0.0:31300
153     mode    health
154     option  httpchk
155     clitimeout    1500
156     srvtimeout    1500
157     maxconn 6000
158     grace 0
Haproxy配置

 

运行一下命令启动Haproxy

[wilson@localhost sbin]$ ./haproxy -f /usr/local/haproxy/examples.cfg

启动效果如下

技术分享

 

server web01 127.0.0.180 check inter 2000 fall 3 weight 30 #定义的多个后端
server web02 192.168.0.104:80 check inter 2000 fall 3 weight 30 #定义的多个后端

配置的第一个80端口为centos本机的Apache 服务器,第二个为 iis,用IE和火狐打开可以看到请求被转发到不同的web服务器,效果如下

技术分享

技术分享

参考:

http://blog.csdn.net/tantexian/article/details/50056199

http://www.cnblogs.com/kgdxpr/p/3272861.html

centos之Haproxy 负载均衡学习笔记

标签:

原文地址:http://www.cnblogs.com/weiweictgu/p/5769242.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!