天天看点

负载均衡工具 haproxy剖析

haproxy 进阶 应用场景

只是作为负载均衡的工具,至于是什么应用集群都可以,例如:上面的RabbitMQ集群

下面那redis集群再给大家举个例子:

创建+配置haproxy配置文件

#创建haproxy配置文件
touch /etc/haproxy/haproxy.cfg
#Haproxy配置
PS:haproxy 配置文件haproxy.cfg详解
vim /etc/haproxy/haproxy.cfg      

添加内容如下:

#logging options
global
  log 127.0.0.1 local0 info #日志输出配置,所有日志都记录在本机,通过local0输出
  maxconn 5120
  chroot /app/haproxy #haproxy 安装路径
  uid 99 #所属运行的用户uid
  gid 99 #所属运行的用户组
  daemon  #后台运行
  quiet
  nbproc 20
  pidfile /var/run/haproxy.pid #指定PID文件路径

defaults
  log global
  #使用4层代理模式,"mode http"为7层代理模式
  mode tcp
  #if
  option tcplog
  option dontlognull
  retries 3
  option redispatch
  maxconn 2000
  #连接超时时间
  timeout connect 5s
        #客户端空闲超时时间为 60秒 则HA 发起重连机制
        timeout client 60s
        #服务器端连接超时时间为 15秒 则HA 发起重连机制
        timeout server 15s  

#front-end IP for consumers and producters
listen rabbitmq_cluster
  bind 0.0.0.0:5672  #绑定协议端口
  #配置TCP模式
  #所处理的类别,默认采用http模式,可配置成tcp作4层消息转发
  mode tcp
  #balance url_param userid
  #balance url_param session_id check_post 64
  #balance hdr(User-Agent)
  #balance hdr(host)
  #balance hdr(Host) use_domain_only
  #balance rdp-cookie
  #balance leastconn
  #balance source //ip
  #简单的轮询
  balance roundrobin
  #负载均衡策略
  #rabbitmq集群节点配置 #inter 每隔五秒对mq集群做健康检查, 2次正确证明服务器可用,2次失败证明服务器不可用,并且配置主备机制
        server redis-01 192.168.0.115:5672 check inter 5000 rise 2 fall 2
        server redis-02 192.168.0.117:5672 check inter 5000 rise 2 fall 2
        server redis-03 192.168.0.118:5672 check inter 5000 rise 2 fall 2
#配置haproxy web监控,查看统计信息
listen stats
  bind 192.168.0.119:8100 #前端浏览器中查看统计的WEB界面地址
  mode http
  option httplog #日志类别,采用httplog
  stats enable
  stats auth admin:123456  #设置查看统计的账号密码
  #设置haproxy监控地址为http://localhost:8100/rabbitmq-stats
  stats uri /rabbitmq-stats
  stats refresh 5s  #5s刷新一次      

注:以上配置基于haproxy-2.1.2 版本,低版本有部分差异

不相同部分剖析:

redis集群:

server redis-01 192.168.0.115:5672 check inter 5000 rise 2 fall 2
        server redis-02 192.168.0.117:5672 check inter 5000 rise 2 fall 2
        server redis-03 192.168.0.118:5672 check inter 5000 rise 2 fall 2      
server mq-01 192.168.0.115:5672 check inter 5000 rise 2 fall 2
        server mq-02 192.168.0.117:5672 check inter 5000 rise 2 fall 2
        server mq-03 192.168.0.118:5672 check inter 5000 rise 2 fall 2      

继续阅读