概述
搭建高可用RabbitMQ镜像模式集群
- rabbitmq+Keepalived+Haproxy 的集群架构
一、部署rabbitmq集群
1、环境准备
3台centos7操作系统,ip分别为:
192.168.122.46
192.168.122.221
192.168.122.140
2、修改 hosts 文件 (3 台)
[root@rabbitmq1 ~]# vim /etc/hosts
192.168.122.46 rabbitmq1
192.168.122.221 rabbitmq2
192.168.122.140 rabbitmq3
3、安装 rabbitmq(3 台)
[root@rabbitmq-1 ~] yum install erlang
[root@rabbitmq-1 ~] yum install -y socat
[root@rabbitmq-1 ~] yum -y install rabbitmq-server
[root@rabbitmq-1 ~] systemctl start rabbitmq-server.service
启动 rabbitmq 内置 web 插件, 管理 rabbitmq 账号等信息 (3 台)
[root@rabbitmq1 ~]# rabbitmq-plugins enable rabbitmq_management
4、搭建 rabbitmq 的一般模式集群
统一 erlang.cookie 文件中 cookie 值
#复制 rabbitmq1 中 /var/lib/rabbitmq/.erlang.cookie 的内容到 rabbitmq2 rabbitmq2 相同的内容
[root@rabbitmq2 ~]# chmod 600 /var/lib/rabbitmq/.erlang.cookie
Rabbitmq 集群添加节点
- 重启 rabbitmq1机器中 rabbitmq 的服务 在 rabbitmq2,rabbitmq3 分别执行
[root@rabbitmq2 ~]# rabbitmqctl stop_app
[root@rabbitmq2 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1
[root@rabbitmq2 ~]# rabbitmqctl start_app
[root@rabbitmq2 ~]# rabbitmq-plugins enable rabbitmq_management
[root@rabbitmq2 ~]# systemctl restart rabbitmq-server.service
5、搭建 rabbitmq 的镜像高可用模式集群
[root@rabbitmq1 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
二、部署haproxy
环境
-
两台虚拟机或者选择集群中的任意两个节点配置
-
keepalived1:192.168.122.83
-
keepalived2:192.168.122.47
1、安装 haproxy
2、创建 haproxy.conf
[root@localhost ~]# cp -rf /etc/haproxy/haproxy.cfg{,.bak}
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
[root@localhost ~]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option dontlognull
retries 3
maxconn 3000
contimeout 50000
clitimeout 50000
srvtimeout 50000
listen stats
bind *:1314
stats enable
stats hide-version
stats uri /haproxystats
stats realm Haproxy stats
stats auth admin:admin
stats admin if TRUE
listen rabbitmq
bind *:15672
mode tcp
balance roundrobin
server rabbitmq-1 192.168.122.46:15672 check maxconn 2000
server rabbitmq-2 192.168.122.221:15672 check maxconn 2000
server rabbitmq-3 192.168.122.140:15672 check maxconn 2000
3、haproxy 监控页面访问验证
-
端口1314/haproxystats 账号密码 admin:admin
-
有以下效果
三、部署Keepalived,实现主从热备、秒级切换
1、环境
- 两台虚拟机或者选择集群中的任意两个节点配置
- keepalived1:192.168.122.83
- keepalived2:192.168.122.47
- VIP地址:192.168.122.100
2、安装keepalived
3、修改配置文件
保存备份 cp /etc/keepalived/keepalived.conf{,.bak}
1、keepalived1 配置
[root@localhost keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id director1
}
vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy_status.sh"
interval 5
}
vrrp_instance VI_1 {
state MASTER
nopreempt
interface eth0
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.122.100
}
track_script {
check_haproxy
}
}
2、keepalived2 配置
[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id director2
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 80
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.122.100
}
}
3、健康检测脚本 haproxy_chk.sh
[root@localhost keepalived]# cat /etc/keepalived/check_haproxy_status.sh
#!/bin/bash
counter=$(ps -C haproxy --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
systemctl restart haproxy
sleep 5
counter=$(ps -C haproxy --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
systemctl stop keepalived
fi
fi
添加执行权限 chmod a+x /etc/keepalived/check_haproxy_status.sh
4、开启服务验证是VIP
最后
以上就是酷酷咖啡豆为你收集整理的Rabbitmq+Haproxy+Keepalived 实现搭建高可用RabbitMQ镜像模式集群搭建高可用RabbitMQ镜像模式集群的全部内容,希望文章能够帮你解决Rabbitmq+Haproxy+Keepalived 实现搭建高可用RabbitMQ镜像模式集群搭建高可用RabbitMQ镜像模式集群所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复