liyeho 发表于 2019-1-2 09:09:55

负载均衡七层 Haproxy

  一.HAProxy 介绍
HAProxy是一个特别适用于高可用性环境的TCP/HTTP开源的反向代理和负载均衡软件。在7层负载均衡方面的功能很强大(支持cookie track, header rewrite等等),支持双机热备,支持虚拟主机,支持健康检查(通过patch可以支持ECV),同时还提供直观的监控页面,可以清晰实时的监控服务集群的运行状况。同时支持Linux2.6内核中System Epoll,通过简化系统调用,大幅的提高了网络I/O性能。
Haproxy包括以下一些特征:
根据静态分配的cookie 分配HTTP请求
分配负载到各个服务器,同时保证服务器通过使用HTTP Cookie实现连接保持;
当主服务器宕机时切换到备份服务器; 允许特殊端口的服务监控;
做维护时通过热配置可以保证业务的连续性,更加人性化;
添加/修改/删除HTTP Request和Response 头;
通过特定表达式Block HTTP请求;
根据应用的cookie做连接保持;
带有用户验证的详细的HTML监控报告;

新的1.3版本引入了frontend,backend配置段,frontend根据任意HTTP请求头内容做规则匹配,然后把请求定向到相关的backend,通过ACL可以实现类似与F5的irules的功能。功能非常强大。目前haproxy支持以下5种负载均衡算法,同时也支持通过weight来实现负载比率的调整和通过cookie来实现连接保持。
1. 轮询roundrobin
2. 最少连接数Leastconn
3. 根据源IP source
4. 根据URI uri
5. 根据URL里的参数url_param(根据请求串中的数据hush后做lb,譬如需要一个userid永远在某台服务器上,该策略是静态的)
  二六台设备如下:
  1 Client:195.168.5.2 GW:192.168.5.1
  2 DGW Director:eth0 192.168.5.1 eth1:172.16.20.102
  3 Realserver: 172.16.20.103 GW:172.16.20.102---------------------------------------------1
  4 Realserver: 172.16.20.104 GW:172.16.20.102---------------------------------------------2
  5 Realserver: 172.16.20.105 GW:172.16.20.102---------------------------------------------3
  6 Realserver: 172.16.20.106 GW:172.16.20.102---------------------------------------------4
  Director:安装haproxy
  vim /etc/haproxy/haproxy.cfg
  -------------------------------------------


[*]global
[*]
[*]    log         127.0.0.1 local2
[*]
[*]    chroot      /var/lib/haproxy
[*]
[*]    pidfile   /var/run/haproxy.pid
[*]
[*]    maxconn   4000
[*]
[*]    user      haproxy
[*]
[*]    group       haproxy
[*]
[*]    daemon
[*]
[*]
[*]defaults
[*]
[*]    mode      http
[*]
[*]    log         global
[*]
[*]    option      dontlognull
[*]
[*]    option      httpclose
[*]
[*]    option      httplog
[*]
[*]    option      forwardfor
[*]
[*]    option      redispatch
[*]
[*]    timeout connect 10000 # default 10 second time out if a backend is not found
[*]
[*]    timeout client 300000
[*]
[*]    timeout server 300000
[*]
[*]    maxconn   60000
[*]
[*]    retries   3
[*]
[*]    stats enable
[*]
[*]    stats uri /admin/stats
[*]
[*]    stats realm "test_123 monitor"
[*]
[*]    stats auth admin:admin
[*]
[*]
[*]
[*]frontend http-in
[*]
[*]    bind 0.0.0.0:80
[*]
[*]    mode http
[*]
[*]    logglobal
[*]
[*]    option httplog
[*]
[*]    option httpclose
[*]
[*]    option forwardfor
[*]
[*]
[*]    capture request header Host len 20
[*]
[*]    capture request header User-Agent len 16
[*]
[*]    capture requestheader Content-Length len 10
[*]
[*]    capture requestheader Referer      len 20
[*]
[*]    capture response header Content-Length len 10
[*]
[*]
[*]    acl api_php url_reg -i \.php$
[*]
[*]    acl api_jsp url_reg -i \.jsp$
[*]
[*]    use_backend apache-server if api_php
[*]
[*]    use_backend tomcat-server if api_jsp
[*]
[*]    default_backend apache-server
[*]
[*]
[*]backend apache-server
[*]
[*]      mode http
[*]
[*]      balance   roundrobin
[*]
[*]      server    web1 172.16.20.103:80 weight 3 check
[*]
[*]      server    web2 172.16.20.105:80 weight 3 check
[*]
[*]      option    httpchk GET /index.php
[*]
[*]
[*]backend tomcat-server
[*]
[*]      mode http
[*]
[*]      balance   roundrobin
[*]
[*]      server    web3 172.16.20.104:8080 weight 3 check
[*]
[*]      server    web4 172.16.20.106:8080 weight 3 check
[*]
[*]      option    httpchk GET /index.jsp

  -----------------------------------------------------------------------
  /etc/init.d/tomcat5 start
  1 3 apache 2 4 tomcat
  yum install httpd -y
  yum install tomcat* -y
  vim /var/www/html/index.php
  vim /usr/share/tomcat5/webapps/ROOT/index.jsp




页: [1]
查看完整版本: 负载均衡七层 Haproxy