-------+-----------+-----+-----+-----+--------+----
| | | | | _|_db
+--+--+ +-+-+ +-+-+ +-+-+ +-+-+ (___)
| LB1 | | A | | B | | C | | D | (___)
+-----+ +---+ +---+ +---+ +---+ (___)
haproxy 4 cheap web servers
Config on haproxy (LB1) :
-------------------------
listen webfarm 192.168.1.1:80
mode http
balance roundrobin
cookie SERVERID insert indirect
option httpchk HEAD /index.html HTTP/1.0
server webA 192.168.1.11:80 cookie A check
server webB 192.168.1.12:80 cookie B check
server webC 192.168.1.13:80 cookie C check
server webD 192.168.1.14:80 cookie D check
Description :
-------------
- LB1 will receive clients requests.
- if a request does not contain a cookie, it will be forwarded to a valid server
- in return, a cookie "SERVERID" will be inserted in the response holding the server name (eg: "A").
- when the client comes again with the cookie "SERVERID=A", LB1 will know that
it must be forwarded to server A. The cookie will be removed so that the server does not see it.
- if server "webA" dies, the requests will be sent to another valid server and a cookie will be reassigned.
点击(此处)折叠或打开
Flows :
-------
(client)(haproxy)(server A)
>-- GET /URI1 HTTP/1.0 ------------>|
( no cookie, haproxy forwards in load-balancing mode.)
|>-- GET /URI1 HTTP/1.0 ---------->
|<-- HTTP/1.0 200 OK -------------<
( the proxy now adds the server cookie in return )
<-- HTTP/1.0 200 OK ---------------<|
Set-Cookie: SERVERID=A |
>-- GET /URI2 HTTP/1.0 ------------>|
Cookie: SERVERID=A |
( the proxy sees the cookie. it forwards to server A and deletes it )
|>-- GET /URI2 HTTP/1.0 ---------->
|<-- HTTP/1.0 200 OK -------------<
( the proxy does notadd the cookie in return because the client knows it )
<-- HTTP/1.0 200 OK ---------------<|
>-- GET /URI3 HTTP/1.0 ------------>|
Cookie: SERVERID=A |
(...)
HaProxy负载均衡选择后端SERVER,首次请求不含cookie,按照负载均衡算法选择后端SERVER。然后在给CLIENT的应答中插入含SERVER ID的cookie,
那么对于后续的请求,根据携带的SERVER ID cookie直接选择对应的SERVER进行转发。如果后端SERVER出现故障,则重新选择SERVER。