Haproxy 负载均衡

      HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速而且可靠的一种解决方案。
     若是说在功能上,能以proxy反向代理方式实现 WEB均衡负载,这样的产品有不少。包括 Nginx,ApacheProxy,lighttpd等。可是 Haproxy 并非 Http服务器,以上提到全部带反向代理均衡负载的产品,都是 WEB 服务器,他们能提供静态(html,jpg,gif..)或动态(PHP,cgi..)文件的传输以及处理。而Haproxy仅仅,并且专门是一款的用于均衡负载的应用代理。其自身并不能提供http服务。
      其配置简单,拥有很是不错的服务器健康检查功能还有专门的系统状态监控页面,当其代理的后端服务器出现故障, HAProxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入。自1.3版本开始还引入了frontend, backend, frontend根据任意HTTP请求头内容作规则匹配,而后把请求定向到相关的backend。html

  1. 安装Haproxy
    wdget http://www.haproxy.org/download/1.7/src/haproxy-1.7.5.tar.gz
    tar vxf haproxy-1.7.5.tar.gz

    进入目录编译安装
    make TARGET=linux26 PREFIX=/usr/local/haproxy
    make install PREFIX=/usr/local/haproxy
     
  2. 配置Haproxy
    建立配置文件
    mkdir /usr/local/haproxy/conf
    mkdir /usr/local/haproxy/logs
    vim conf/haproxy.cfg

    1) haproxy.cfg内容以下(tcp负载均衡):
    global
        log 127.0.0.1 local0 info #[err warning info debug]
        maxconn 4096
        user root
        group root
        daemon
        nbproc 1
        pidfile /usr/local/haproxy/logs/haproxy.pid

    defaults
        maxconn 2000
        timeout connect 5000
        timeout client 30000
        timeout server30000
        retries 3          #后端服务器失败重连次数,失败后标记服务器不可用

    frontend server_read
        bind *:8888         #监听端口
        default_backend cluster_server

    backend cluster_server
        mode tcp                    #tcp负载均衡
        balance roundrobin    #负载均衡算法,roundrobin, leastconn, source, static-rr
        server svr1 192.168.1.100:3306 weight 5 check inter 5000 rise 2 fall 3     #后端服务器权重和检测
        server svr2 192.168.1.101:3306 weight 5 check inter 5000 rise 2 fall 3

    listen admin_stats
        bind 0.0.0.0:1080
        mode http
        log 127.0.0.1 local0 err
        stats uri /stats      #监控访问地址 localhost:1080/stats
        stats refresh 5s    #监控页面刷新的间隔

    2) haproxy.cfg内容以下(http负载均衡):
    global
        log 127.0.0.1 local0 info #[err warning info debug]
        maxconn 4096
        user root
        group root
        daemon
        nbproc 1
        pidfile /usr/local/haproxy/logs/haproxy.pid

    defaults
        maxconn 2000
        timeout connect 5000
        timeout client 30000
        timeout server30000
        retries 3          #后端服务器失败重连次数,失败后标记服务器不可用

    frontend server_read
        bind *:8888         #监听端口
        default_backend cluster_server

    backend cluster_server
        mode http                   #http负载均衡
        balance roundrobin    #负载均衡算法,roundrobin, leastconn, source, static-rr
        option httpchk GET /index.html    #健康检测方式
        server svr1 192.168.1.100:3306 weight 5 check inter 5000 rise 2 fall 3     #后端服务器权重和检测
        server svr2 192.168.1.101:3306 weight 5 check inter 5000 rise 2 fall 3

    listen admin_stats
        bind 0.0.0.0:1080
        mode http
        log 127.0.0.1 local0 err
        stats uri /stats      #监控访问地址 localhost:1080/stats
        stats refresh 5s    #监控页面刷新的间隔
     
  3. 启动haproxy
    进入sbin目录
    ./haproxy -f ../conf/haproxy.cfg

    查看8888端口
    netstat -anp | grep 8888

    查看监控页面
    http://localhost:1080/stats