Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1478872
  • 博文数量: 228
  • 博客积分: 1698
  • 博客等级: 上尉
  • 技术积分: 3241
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-24 21:49
个人简介

Linux

文章分类

全部博文(228)

文章存档

2017年(1)

2016年(43)

2015年(102)

2014年(44)

2013年(5)

2012年(30)

2011年(3)

分类: LINUX

2015-07-29 17:35:42


1. 基于cookie插入的HTTP负载均衡

点击(此处)折叠或打开

  1.  192.168.1.1    192.168.1.11-192.168.1.14   192.168.1.2
  2. -------+-----------+-----+-----+-----+--------+----    
           |           |     |     |     |       _|_db     
        +--+--+      +-+-+ +-+-+ +-+-+ +-+-+    (___)      
        | LB1 |      | A | | B | | C | | D |    (___)      
        +-----+      +---+ +---+ +---+ +---+    (___)      
        haproxy        4 cheap web servers                 


  3. Config on haproxy (LB1) : 
  4. ------------------------- 
  5.                                                                                    
  6.     listen webfarm 192.168.1.1:80 
  7.        mode http 
  8.        balance roundrobin 
  9.        cookie SERVERID insert indirect 
  10.        option httpchk HEAD /index.html HTTP/1.0 
  11.        server webA 192.168.1.11:80 cookie A check 
  12.        server webB 192.168.1.12:80 cookie B check 
  13.        server webC 192.168.1.13:80 cookie C check 
  14.        server webD 192.168.1.14:80 cookie D check 
  15.                                                                                    
  16.                                                                                    
  17. Description : 
  18. ------------- 
  19.  - LB1 will receive clients requests. 
  20.  - if a request does not contain a cookie, it will be forwarded to a valid server 
  21.  - in return, a cookie "SERVERID" will be inserted in the response holding the server name (eg: "A").
  22.  - when the client comes again with the cookie "SERVERID=A", LB1 will know that 
  23.    it must be forwarded to server A. The cookie will be removed so that the server does not see it.
  24.  - if server "webA" dies, the requests will be sent to another valid server and a cookie will be reassigned.
                                                

点击(此处)折叠或打开

  1. Flows :
  2. -------
  3.                                                                                                
  4. (client) (haproxy) (server A)
  5.   >-- GET /URI1 HTTP/1.0 ------------> |
  6.                ( no cookie, haproxy forwards in load-balancing mode. )
  7.                                        | >-- GET /URI1 HTTP/1.0 ---------->
  8.                                        | <-- HTTP/1.0 200 OK -------------<
  9.                ( the proxy now adds the server cookie in return )
  10.   <-- HTTP/1.0 200 OK ---------------< |
  11.       Set-Cookie: SERVERID=A |
  12.   >-- GET /URI2 HTTP/1.0 ------------> |
  13.       Cookie: SERVERID=A |
  14.       ( the proxy sees the cookie. it forwards to server A and deletes it )
  15.                                        | >-- GET /URI2 HTTP/1.0 ---------->
  16.                                        | <-- HTTP/1.0 200 OK -------------<
  17.    ( the proxy does not add the cookie in return because the client knows it )
  18.   <-- HTTP/1.0 200 OK ---------------< |
  19.   >-- GET /URI3 HTTP/1.0 ------------> |
  20.       Cookie: SERVERID=A |
  21.                                     ( ... )
HaProxy负载均衡选择后端SERVER,首次请求不含cookie,按照负载均衡算法选择后端SERVER。然后在给CLIENT的应答中插入含SERVER ID的cookie,
那么对于后续的请求,根据携带的
SERVER ID cookie直接选择对应的SERVER进行转发。如果后端SERVER出现故障,则重新选择SERVER。

局限性:
1)当client使用keep-alive时,只有第1个的response会被插入cookie,同样只有第1个的request会被解析。当第1个请求到来后,立即选择SERVER,并
被插入第1个response中,后续的request由于在同一条session(四层)上,会被交给同一个后端的SERVER,看起来貌似没有什么问题。但是由于只解析第1
个request,因此后面的request会携带含SERVER ID的cookie到达后端SERVER,此时SERVER必须忽略无法识别的cookie字段,否则就出现了问题。另外
如果客户端使用PIPE LINE时,第一个response不一定是针对第1个request的,不过在当前的场景下暂时没有引入新的问题 <PIPE LINE没有普及,不清楚
HaProxy支持的如何>。

当含SERVER ID的cookie引发了后端SERVER处理故障时,我们需要将Keep-Alive关闭,使用的方法是 <文末会对该选项进行详细介绍>:

点击(此处)折叠或打开

  1. option httpclose
2)由于某些原因,客户端可能无法有效获取/使用response中的cookie信息,此时需要使用"prefix"模式 <见后文介绍>
3)HaProxy可能会挂掉,因此需要使用keepalived来做HaProxy的高可用性
4)有些应用需要Log原始客户端的IP地址,通过配置forwardfor选项,让HaProxy在请求头部中增加X-Forwarded-For字段,来记录原始的客户端IP地址。
此时必须使用httpclose选项来关闭Keepalive,保证HaProxy可以处理每一个请求头部。

点击(此处)折叠或打开

  1. option httpclose
  2. option forwardfor
5)同样,有些应用需要Log客户端初始请求的目的地址,通过配置originalto选项,让HaProxy在请求头部增加X-Original-To字段,来记录原始目的IP地址。
此时必须使用httpclose选项来关闭Keepalive,保证HaProxy可以处理每一个请求头部。

点击(此处)折叠或打开

  1. option httpclose
  2. option originalto
另外,互联网上可能存在一些客户端关闭了cookies功能,虽然可能导致很多问题。此时需要使用"source" balancing algorithm来替换"roundrobin"算法。
这种方式会导致同一个源IP发出的连接,分配到了同一台服务器上,显然可能不太公平,尤其时存在SNAT的场景。
阅读(1694) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~