Haproxy是一款提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费的、快速并且可靠的一种解决方案。TCP代理服务器。
RabbitMQ集群镜像模式中,Haproxy用于做TCP代理,提供节点负载均衡,(LB-LoadBalance)与故障发现。
————————————————
首先进入此机器,若IP为192.168.3.3(用于后边的验证)
安装haproxy:
sudo yum install haproxy
备份配置文件:
sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg_bak
修改配置rabbitmq负载:
sudo 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 local2
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 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-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
#---------------------------------------------------------------------
#对MQ集群进行监听
listen rabbitmq_cluster
bind 0.0.0.0:5673 #通过5673对m1和m2进行映射
option tcplog #记录TCP连接状态和时间
mode tcp #四层协议代理,即对TCP进行转发
option clitcpka #开启TCP的Keep Alive(长连接模式)
timeout connect 1s #haproxy与mq建立连接的超时时间
timeout client 10s #客户端与haproxy最大空闲时间
timeout server 10s #服务器与haproxy最大空闲时间
balance roundrobin #采用轮询转发消息
#每5秒发送一次心跳包,如果连续两次有响应则代表状态良好
#如果连续3次没有响应,则视为服务故障,该节点将被剔除
server 85node 192.168.7.85:5672 check inter 5s rise 2 fall 3
server 86node 192.168.7.86:5672 check inter 5s rise 2 fall 3
server 87node 192.168.7.87:5672 check inter 5s rise 2 fall 3
listen rabbitmq_admin
bind 0.0.0.0:8100
server 85node 192.168.7.85:15672
server 86node 192.168.7.86:15672
server 87node 192.168.7.87:15672
#开启监控服务
listen http_front
bind 0.0.0.0:1080 #监听端口
stats refresh 30s #每30秒刷新一次
stats uri /haproxy?stats #统计页面uri
stats auth admin123:admin123 #统计页面用户名和密码设置
#---------------------------------------------------------------------
#---------------------------------------------------------------------
启动haproxy:
sudo haproxy -f /etc/haproxy/haproxy.cfg
检查进程:
ps -ef|grep haproxy
检查端口情况:
sudo netstat -nplt|grep haproxy
image.png
网页登录:
192.168.3.3:1080/haproxy?stats
账号密码:admin123:admin123
image.png
网页打开:
192.168.7.85:8100或者192.168.7.86:8100或者192.168.7.87:8100都可以查看单台rabbitmq的管理界面,账号密码是在rabbitmq设置的用户名密码,示例如下:
image.png