, HAProxy 介绍
  
反向代理服务器,支持双机热备支持虚拟主机,但其配置简单,拥有非常不错的服务器健康检查功能,当其代理的后端服务器出现故障, HAProxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入。新的1.3引入了frontend,backend,frontend根据任意HTTP请求头内容做规则匹配,然后把请求定向到相关的backend.

二,利用HAProxy实现负载均衡
  1.
利用HAProxy实现负载均衡

192.168.1.222 (haproxy)---------负载均衡----------(192.168.1.100)

安装配置HAproxy

wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.14.2.tar.gz

tar zxvf haproxy-1.3.14.2.tar.gz

mv  haproxy-1.3.14.2  haproxy

cd haproxy

make

2. 创建配置文件

# vi haproxy.cfg

内容:

# this config needs haproxy-1.1.28 or haproxy-1.2.2

global
 log 127.0.0.1 local0
 log 127.0.0.1 local1 notice
 #log loghost local0 info
 maxconn 5555
 chroot /usr/share/haproxy
 uid 99
 gid 99
 daemon
 #debug
 #quiet

defaults
 log global
 mode http
 option httplog
 option dontlognull
 retries 3
 redispatch
 maxconn 2000
 contimeout 5000
 clitimeout 50000
 srvtimeout 50000

listen app1 0.0.0.0:8080
        mode http
        option httplog
        option dontlognull
 cookie SERVERID rewrite
 balance roundrobin
        option httpchk
 server app1_1 192.168.1.222:80 cookie app1inst1 check inter 2000 rise 2 fall 5
 server app1_2 192.168.1.100:80 cookie app1inst2 check inter 2000 rise 2 fall 5
# server app1_3 192.168.0.108:80 cookie app1inst3 check inter 2000 rise 2 fall 5
# server app1_4 220.181.6.6:80 cookie app1inst4 check inter 2000 rise 2 fall 5

        stats uri /my_stats  # 访问时:http://192.168.1.222:8080/my_stats 
        stats realm Statistics\ for\ MyApp1-2
        stats auth admin:uplooking   # 帐号:密码
        stats scope .
        stats scope app2

listen app2 0.0.0.0:8443
        mode tcp
 option ssl-hello-chk
 balance source
 server inst1 192.168.1.222:443 check inter 2000 fall 3
 server inst2 192.168.1.100:443 check inter 2000 fall 3
# server back1 192.168.0.254:443 backup
        stats uri /my_stats
        stats realm Statistics\ for\ MyApp2
        stats scope .
 
 errorloc 502 http://192.168.1.222/error502.html
 errorfile 503 /site/haproxy/errors/503.http

# ./haproxy –f  examples/haproxy.cfg 启动服务.

监控状态图示 http://192.168.0.222:8080/my_stats,输入用户名密码查看状态。

 

三.相关介绍
   #./haproxy –help //haproxy
相关命令参数介绍.
   haproxy  -f  <
配置文件>  [-n 最大并发连接总数] [-N 每个侦听的最大并发数] [-d] [-D] [-q] [-V] [-c] [-p <pid文件>] [-s] [-l] [-dk]
       [-ds] [-de] [-dp] [-db] [-m <
内存限制M>] [{-sf|-st} pidlist...]
       -d     
前台,debug模式
       -D     daemon
模式启动
       -q     
安静模式,不输出信息
       -V     
详细模式
       -c     
对配置文件进行语法检查
       -s     
显示统计数据
       -l     
显示详细统计数据
       -dk   
不使用kqueue
       -ds   
不使用speculative epoll
       -de   
不使用epoll
       -dp   
不使用poll
       -db   
禁用后台模式,程序跑在前台
       -sf <pidlist>
      
程序启动后向pidlist里的进程发送FINISH信号,这个参数放在命令行的最后
       -st <pidlist>
      
程序启动后向pidlist里的进程发送TERMINATE信号,这个参数放在命令行的最后

四,更高级的应用参考相关文档

 官方参考手册: http://haproxy.1wt.eu/download/1.3/doc/haproxy-en.txt
http://www.howtoforge.com/high-availability-load-balancer-haproxy-heartbeat-debian-etch

using haproxy to proxy multiple named-based virtual hosts

haproxy is one of the best layer 7 loadbalancing solutions for web administrators. It is feature rich in Access Control Lists and header manipulation capabilities.It is a fast and easy-to-configure web proxy.
The following shows how to configure haproxy to proxy multiple named-based virtual hosts typically configured in Apache and Nginx.

OS: CentOS release 4.7 (Final)
Kernel: Linux 2.6.9-34.ELsmp

  1. [root@localhost ~]# haproxy -v  
  2. HA-Proxy version 1.3.20 2009/08/09  
  3. Copyright 2000-2009 Willy Tarreau <w@1wt.eu> 

Content of the runtime configuration file: /etc/haproxy.cfg

  1. global  
  2.         maxconn         20000  
  3.         log             127.0.0.1 local0  
  4.         uid             99  
  5.         gid             99
  6. chroot /var/empty  
  7.         daemon  
  8.    
  9. frontend public  
  10.         bind            192.168.1.10:80  
  11.         mode            http  
  12.         log             global  
  13.    
  14.         option          httplog  
  15.         option          dontlognull  
  16.         option          httpclose  
  17.         option      forwardfor  
  18.    
  19.         maxconn         20000  
  20.    
  21.         timeout client 6000  
  22.    
  23.         acl site_1  hdr_beg(host)-i i.yaknet.cn  
  24.         use_backend  i    if site_1   
  25.         acl site_2  hdr_beg(host)-i s.yaknet.cn  
  26.         use_backend  s    if site_2   
  27.         default_backend s  
  28.    
  29. backend i  
  30.         mode            http  
  31.         balance         source 
  32.         cookie          SERVERID    prefix    
  33.         retries         3  
  34.    
  35.         timeout connect 6000  
  36.         timeout server  6000  
  37.    
  38.         server          srv_i_101 192.168.1.101:80 cookie i101 weight 5 check inter 15000 rise 2 fall 3  
  39.         server          srv_i_102 192.168.1.102:80 cookie i102 weight 5 check inter 15000 rise 2 fall 3  
  40.         server          srv_i_103 192.168.1.103:80 cookie i102 weight 5 check inter 15000 rise 2 fall 3  
  41.    
  42. backend s  
  43.         mode            http  
  44.         balance         source  
  45.         cookie          SERVERID    prefix  
  46.         retries         3  
  47.    
  48.         timeout connect 6000  
  49.         timeout server  6000  
  50.    
  51.         server          srv_s_201 192.168.1.201:80 cookie s201 weight 5 check inter 15000 rise 2 fall 3  
  52.         server          srv_s_202 192.168.1.202:80 cookie s202 weight 5 check inter 15000 rise 2 fall 3  
  53.         server          srv_s_203 192.168.1.203:80 cookie s202 weight 5 check inter 15000 rise 2 fall 3 

To make your haproxy log properly,you should also modify /etc/syslog.conf and one line:

  1. local0.*/var/log/haproxy.log 

and then start syslogd with `syslogd -r -m 0`.
See, how easy it is to set up such a smart and fast proxy!
Also, for avoiding single point of failure (SPOF), you can turn to keepalived or heartbeat, then you will have high-availability haproxy loadbalanced web server farms.

此文章由 flyinweb 于 2009-09-24 08:45:24 编辑

本日志由 flyinweb 于 2009-09-21 14:27:43 发表,目前已经被浏览 6174 次,评论 0 次;

作者添加了以下标签: HAProxy负载均衡

引用通告:http://www.517sou.net/Article/255/Trackback.ashx

评论订阅:http://www.517sou.net/Article/255/Feeds.ashx

评论列表

    暂时没有评论
(必填)
(必填,不会被公开)