网络结构以下图:
web
共有10四、10五、106三台RabbitMQ Server,互为集群
其中104和105安装了Haproxy,每一个Haproxy承担三台RabbitMQ server的负载均衡
两台Harpoxy采用Keepalived互为主备,VIP是172.16.0.108
操做系统为Ubuntu
如下介绍操做步骤:redis
一、三台主机安装RabbitMQbash
apt-get install rabbitmq-server cookie
开启RabbitMQ management,激活控制台以方便MQ的管理与监控
sudo rabbitmq-plugins enable rabbitmq_management
开启监控后能够输入http://ip:15672能够登陆管理界面,默认帐户guest/guest
二、配置MQ集群
2.1 cookie文件
由于RabbitMQ的集群是经过Erlang的集群来实现的,因此,要求三台机器的
/var/lib/rabbitmq/.erlang.cookie 文件内容一致,用VI等工具将它的内容修改成 zHDCGETPYWOWREASJUAB
因为RabbitMQ在启动Booker时会检查该文件的权限,必须为400,不然会报错,因此要修改文件的权限
chmod 400 .erlang.cookie
2.2 修改各机器hosts
172.16.0.104 pzs-test-1
172.16.0.105 pzs-test-2
172.16.0.106 pzs-test-3
保证三台机器能够互相访问对方
2.3 加入集群
对主节点(104):
#启动Broker
rabbitmq-server –detached > nohup.out&
#启动集群
rabbitmqctl start_app
#查看集群状态
rabbitmqctl cluster_status
对备节点(10五、106):
rabbitmq-server –detached > nohup.out&
rabbitmqctl start_app
rabbitmqctl stop_app
#加入集群
rabbitmqctl join_cluster --ram rabbit@pzs-test-1
rabbitmqctl start_app
#查看集群状态
rabbitmqctl cluster_status
在三台机器运行如下命令:
设置成镜像队列
# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}' //["^"匹配全部]
三、安装配置HaProxy
安装过程省略
修改/etc/haproxy/haproxy.cfg,在文件后面添加如下内容
listen rabbitmq_local_cluster 0.0.0.0:25672
#配置TCP模式
mode tcp
option tcplog
#简单的轮询
balance roundrobin
#rabbitmq集群节点配置
server rabbit1 172.16.0.104:5672 check inter 5000 rise 2 fall 2
server rabbit2 172.16.0.105:5672 check inter 5000 rise 2 fall 2
server rabbit3 172.16.0.106:5672 check inter 5000 rise 2 fall 2
#配置haproxy web监控,查看统计信息
listen private_monitoring :8100
mode http
option httplog
stats enable
#设置haproxy监控地址为http://localhost:8100/stats
stats uri /stats
stats refresh 30s
#添加用户名密码认证
stats auth admin:1234
启动haproxy: haproxy -f haproxy.cfg
访问http://172.16.0.104:8100/stats和http://172.16.0.105:8100/stats
能够查看haproxy的运行状态及一些统计信息
四、安装配置Keepalived
安装过程省略
【haproxy_check.sh文件内容】
#!/bin/bash
LOGFILE="/var/log/keepalived-haproxy-state.log"
date >> $LOGFILE
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
echo "fail: check_haproxy status" >> $LOGFILE
exit 1
else
echo "success: check_haproxy status" >> $LOGFILE
exit 0
fi
【haproxy_master.sh文件内容】
LOGFILE="/var/log/keepalived-haproxy-state.log"
echo "Being Master ..." >> $LOGFILE
而后修改/etc/keepalived/keepalived.conf文件
主机(104)
global_defs {
router_id test
}
vrrp_script chk_haproxy
{
script "/etc/keepalived/scripts/haproxy_check.sh"
interval 2
timeout 2
fall 3
}
vrrp_instance haproxy {
state MASTER # 主也配置为SLAVE
interface eth0
virtual_router_id 61
priority 150
virtual_ipaddress {
172.16.0.108
}
track_script {
chk_haproxy
}
notify_master "/etc/keepalived/scripts/haproxy_master.sh"
}
备机(105)
global_defs {
router_id redis
}
vrrp_script chk_haproxy
{
script "/etc/keepalived/scripts/haproxy_check.sh"
interval 2
timeout 2
fall 3
}
vrrp_instance haproxy {
state BACKUP # 主也配置为SLAVE
interface eth0
virtual_router_id 61
priority 100
virtual_ipaddress {
172.16.0.108
}
track_script {
chk_haproxy
}
notify_master "/etc/keepalived/scripts/haproxy_master.sh"
}
注意:keepalived可能运行多个实例,好比redis和haproxy共存,在这种状况下,必须注意几点:
1、VIP必须各实例不一样
2、virtual_router_id必须各实例不一样
3、脚本文件中不容许出现kill keepalived进程的操做
运行keepalived -D 启动keepalived