一.HAProxy 介绍
HAProxy
是一个特别适用于高可用性环境TCP/HTTP开源的反向代理负载均衡软件。在7层负载均衡方面的功能很强大(支持cookie track, header rewrite等等),支持双机热备,支持虚拟主机,支持健康检查(通过patch可以支持ECV),同时还提供直观的监控页面,可以清晰实时的监控服务集群运行状况。同时支持Linux2.6内核System Epoll,通过简化系统调用,大幅的提高了网络I/O性能

Ha
proxy包括以下一些特征:
根据静态分配的cookie 分配HTTP请求
分配负载到各个服务器,同时保证服务器通过使用HTTP Cookie实现连接保持;
当主服务器宕机时切换到备份服务器; 允许特殊端口的服务监控;
做维护时通过热配置可以保证业务的连续性,更加人性化;
添加/修改/删除HTTP RequestResponse 头;
通过特定表达式Block HTTP请求;
根据应用cookie做连接保持;
带有用户验证的详细的HTML监控报告;



新的1.3版本引入了frontend,backend配置段,frontend根据任意HTTP请求头内容做规则匹配,然后把请求定向到相关的backend,通过ACL可以实现类似与F5irules的功能。功能非常强大。目前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

-------------------------------------------

 

   
   
   
   
  1. global 
  2.  
  3.     log         127.0.0.1 local2 
  4.  
  5.     chroot      /var/lib/haproxy 
  6.  
  7.     pidfile     /var/run/haproxy.pid 
  8.  
  9.     maxconn     4000 
  10.  
  11.     user        haproxy 
  12.  
  13.     group       haproxy 
  14.  
  15.     daemon 
  16.  
  17.  
  18. defaults 
  19.  
  20.     mode        http 
  21.  
  22.     log         global 
  23.  
  24.     option      dontlognull 
  25.  
  26.     option      httpclose 
  27.  
  28.     option      httplog 
  29.  
  30.     option      forwardfor 
  31.  
  32.     option      redispatch 
  33.  
  34.     timeout connect 10000 # default 10 second time out if a backend is not found 
  35.  
  36.     timeout client 300000 
  37.  
  38.     timeout server 300000 
  39.  
  40.     maxconn     60000 
  41.  
  42.     retries     3 
  43.  
  44.     stats enable 
  45.  
  46.     stats uri /admin/stats 
  47.  
  48.     stats realm "test_123 monitor" 
  49.  
  50.     stats auth admin:admin 
  51.  
  52.  
  53.  
  54. frontend http-in 
  55.  
  56.     bind 0.0.0.0:80 
  57.  
  58.     mode http 
  59.  
  60.     log  global 
  61.  
  62.     option httplog 
  63.  
  64.     option httpclose 
  65.  
  66.     option forwardfor 
  67.  
  68.  
  69.     capture request header Host len 20 
  70.  
  71.     capture request header User-Agent len 16 
  72.  
  73.     capture request  header Content-Length len 10 
  74.  
  75.     capture request  header Referer        len 20 
  76.  
  77.     capture response header Content-Length len 10 
  78.  
  79.  
  80.     acl api_php url_reg -i \.php$ 
  81.  
  82.     acl api_jsp url_reg -i \.jsp$ 
  83.  
  84.     use_backend apache-server if api_php 
  85.  
  86.     use_backend tomcat-server if api_jsp 
  87.  
  88.     default_backend apache-server 
  89.  
  90.  
  91. backend apache-server 
  92.  
  93.         mode http 
  94.  
  95.         balance   roundrobin 
  96.  
  97.         server    web1 172.16.20.103:80 weight 3 check 
  98.  
  99.         server    web2 172.16.20.105:80 weight 3 check 
  100.  
  101.         option    httpchk GET /index.php 
  102.  
  103.  
  104. backend tomcat-server 
  105.  
  106.         mode http 
  107.  
  108.         balance   roundrobin 
  109.  
  110.         server    web3 172.16.20.104:8080 weight 3 check 
  111.  
  112.         server    web4 172.16.20.106:8080 weight 3 check 
  113.  
  114.         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   

负载均衡七层 Haproxy_第1张图片