浅谈squid的几种负载均衡方式( 2,关于haproxy)

2015 年 12 月 25 日6550

本站主要内容均为原创,转帖需注明出处www.alexclouds.net

       主要记录一下带cookie插入的haproxy HTTP负载均衡

       贴一个参考配置:下面就是带cookie的

#---------------------------------------------------------------------
# global config don't change
#---------------------------------------------------------------------
global
    log     127.0.0.1   local0
    log     127.0.0.1   local1 notice
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults don't change
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# admin status please visit http://IP:8888
# user:admin  password:admina68ee9      don't change
#---------------------------------------------------------------------
listen stats
        bind 0.0.0.0:8888
        stats uri /
        stats realm Haproxy\ Statistics
        stats auth admin:adminaxxxxxxxsarfew
        stats refresh 20
#---------------------------------------------------------------------
# HTTP forward
#---------------------------------------------------------------------

listen  HTTP_LB_squid 0.0.0.0:80
        option httplog
        option httpclose
        option forwardfor
        cookie SERVERID insert indirect
        option httpchk HEAD /index.html HTTP/1.0
        balance source
        server  squid1 127.0.0.1:3129 cookie squid1inst1 check inter 2000 rise 2 fall 5
        server  squid2 127.0.0.1:3130 cookie squid2inst2 check inter 2000 rise 2 fall 5
        server  squid3 127.0.0.1:3131 cookie squid2inst2 check inter 2000 rise 2 fall 5

#listen tcp_LB-for_squid 0.0.0.0:80
#        mode tcp
#        option persist
#        server squid1 127.0.0.1:3129 weight 256
#        server squid2 127.0.0.1:3130 weight 256
#        server squid3 127.0.0.1:3131 weight 256

#****************HTTP BACK SETTING*****************
#listen pg_for_production 0.0.0.0:80
#        mode http
#        option httpclose
#        server production 118.242.237.119:80 weight 256

#---------------------------------------------------------------------
# HTTPS forward
#---------------------------------------------------------------------

#listen ssl_for_production 0.0.0.0:443
#        mode tcp
#        option persist
#        server production 118.242.237.119:443 weight 256

#***************HTTPS BACK SETTING****************
#listen  ssl-relay 0.0.0.0:8443
#        option  ssl-hello-chk
#        balance source
#        server  inst1 192.168.110.56:443 check inter 2000 fall 3
#        server  inst2 192.168.110.57:443 check inter 2000 fall 3
#        server  back1 192.168.120.58:443 backup

      

         此时带 cookie测试已经OK。

 

image

        # curl -I 119.18.237.130                                        
HTTP/1.1 200 OK
Last-Modified: Wed, 09 Mar 2016 11:19:48 GMT
Accept-Ranges: bytes
Content-Length: 19
Content-Type: text/html; charset=UTF-8
Date: Thu, 10 Mar 2016 10:00:26 GMT
Server: Apache/2.2.15 (CentOS)
ETag: "20ad0-13-52d9be03e7c9a"
Age: 0
X-Cache: HIT from alexclouds.net
Via: 1.1 alexclouds.net (squid)
Connection: close
Set-Cookie: SERVERID=squid2inst2; path=/

      

        注意option forwardfor, 此外后端WEB服务器上的日志格式需要增加$http_x_forwarded_for 变量  "$http_x_forwarded_for"';

 

Configuration Details (Squid):

Option Name: forwarded_for
Replaces:
Requires:
Default Value: forwarded_for on
Suggested Config:

If set to "on", Squid will append your client's IP address
in the HTTP requests it forwards. By default it looks like:
    X-Forwarded-For: 192.1.2.3
If set to "off", it will appear as
    X-Forwarded-For: unknown
If set to "transparent", Squid will not alter the
X-Forwarded-For header in any way.
If set to "delete", Squid will delete the entire
X-Forwarded-For header.
If set to "truncate", Squid will remove all existing
X-Forwarded-For entries, and place the client IP as the sole entry.
 

        注意balance source , 某些用户会将他们的浏览器的cookie功能关闭。这时候可能没法访问WEB服务器的内容。这种情况下,可以使用HAProxy的 source 负载均衡算法替代  roundroubin 算法。source 算法可以确保来自同一个IP的所有请求都到达同一台WEB服务器。

        开始压测最好选择round robin 以免小范围压测内的压力不均衡。

0 0