今天公司需要给hue做高可用,以及负载均衡,整体方案是:haproxy+keepalived;下面介绍怎么使用haproxy做负载均衡:
安装:
yum install haproxy
配置:vim /etc/haproxy/haproxy.cfg
global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the '-r' option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local6 debug chroot /var/lib/haproxy pidfile /var/run/haproxy.pid #maxconn 4000 maxconn 100000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats #--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- 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-request 5s timeout connect 5s timeout server 10s timeout client 10s timeout http-keep-alive 10s timeout check 10s stats refresh 30s #统计页面自动刷新时间 stats uri /stats #统计页面url stats realm baison-test-Haproxy #统计页面密码框上提示文本 stats auth admin:admin #统计页面用户名和密码设置 stats hide-version #隐藏统计页面上HAProxy的版本信息 #--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend main *:80 # 配置静态文件,指定静态文件在哪加载 #acl url_static path_beg -i /static /images /javascript /stylesheets #acl url_static path_end -i .jpg .gif .png .css .js #use_backend static if url_static default_backend app #--------------------------------------------------------------------- # static backend for serving up images, stylesheets and such #--------------------------------------------------------------------- backend static balance roundrobin server static 127.0.0.1:4331 check #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend app log global mode http stats enable # 负载均衡策略 balance source # 应用默认健康检查策略,健康检查间隔和超时时间为2000ms,两次成功视为节点UP,三次失败视为节点DOWN server hue1 host:port cookie h166 check inter 2000 fall 3 server hue2 host:port cookie h158 check inter 2000 fall 3
haproxy负载均衡策略种类:
1.balance roundrobin # 轮询,软负载均衡基本都具备这种算法
2.balance static-rr # 根据权重,建议使用
3.balance leastconn # 最少连接者先处理,建议使用
4.balance source # 根据请求源IP,建议使用
5.balance uri # 根据请求的URI
6.balance url_param,# 根据请求的URl参数'balance url_param' requires an URL parameter name
7.balance hdr(name) # 根据HTTP请求头来锁定每一次HTTP请求
8.balance rdp-cookie(name) # 根据据cookie(name)来锁定并哈希每一次TCP请求