Haproxy是一款提供高可用性告丢、負載均衡以及基于TCP(第四層)和HTTP(第七層)應用的代理軟件,支持虛擬主機损谦,它是免費的岖免、快速并且可靠的一種解決方案。TCP代理服務器照捡。
RabbitMQ集群鏡像模式中颅湘,Haproxy用于做TCP代理,提供節(jié)點負載均衡栗精,(LB-LoadBalance)與故障發(fā)現(xiàn)闯参。
————————————————
首先進入此機器,若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集群進行監(jiān)聽
listen rabbitmq_cluster
bind 0.0.0.0:5673 #通過5673對m1和m2進行映射
option tcplog #記錄TCP連接狀態(tài)和時間
mode tcp #四層協(xié)議代理赢赊,即對TCP進行轉發(fā)
option clitcpka #開啟TCP的Keep Alive(長連接模式)
timeout connect 1s #haproxy與mq建立連接的超時時間
timeout client 10s #客戶端與haproxy最大空閑時間
timeout server 10s #服務器與haproxy最大空閑時間
balance roundrobin #采用輪詢轉發(fā)消息
#每5秒發(fā)送一次心跳包,如果連續(xù)兩次有響應則代表狀態(tài)良好
#如果連續(xù)3次沒有響應级历,則視為服務故障释移,該節(jié)點將被剔除
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
#開啟監(jiān)控服務
listen http_front
bind 0.0.0.0:1080 #監(jiān)聽端口
stats refresh 30s #每30秒刷新一次
stats uri /haproxy?stats #統(tǒng)計頁面uri
stats auth admin123:admin123 #統(tǒng)計頁面用戶名和密碼設置
#---------------------------------------------------------------------
#---------------------------------------------------------------------
啟動haproxy:
sudo haproxy -f /etc/haproxy/haproxy.cfg
檢查進程:
ps -ef|grep haproxy
檢查端口情況:
sudo netstat -nplt|grep haproxy
image.png
網(wǎng)頁登錄:
192.168.3.3:1080/haproxy?stats
賬號密碼:admin123:admin123
image.png
網(wǎng)頁打開:
192.168.7.85:8100或者192.168.7.86:8100或者192.168.7.87:8100都可以查看單臺rabbitmq的管理界面,賬號密碼是在rabbitmq設置的用戶名密碼寥殖,示例如下:
image.png