因为 guest 用户只能在本机访问,添加一个 admin 用户,密码也是 admin
./rabbitmqctl add_user admin admin
./rabbitmqctl set_user_tags admin administrator
./rabbitmqctl set_permissions -p / admin “." ".” “.*”
./rabbitmq-plugins enable rabbitmq_management
访问:
http://虚拟机 IP:15672
Erlang 是当前最新版本 23.1
RabbitMQ 是当前最新版本 3.8.4
准备三台虚拟机,可以用克隆功能。
192.168.111.133 RabbitMQ 内存节点 HAPRoxy Keepalived(master)
192.168.111.134 RabbitMQ 内存节点 HAPRoxy Keepalived(backup)
192.168.111.135 RabbitMQ 磁盘节点
VIP 192.168.111.159

先按这个步骤在三台机器上分别安装好 Erlang 和 RabbitMQ。
.erlang.cookie 默认在当前用户根目录下,是隐藏文件,只读文件。
权限要先改成 777,改完以后还原成 400
将三台机器内容改为一致,或者把第一台机器.erlang.cookie 上传到其他两台机器。
cd ~
chmod 777 .erlang.cookie
vim .erlang.cookie
XTNENHLEVWHMRLMIYAUD
chmod 400 .erlang.cookie
vim /etc/hosts
添加内容:
192.168.111.133 rmq156
192.168.111.134 rmq157
192.168.111.135 rmq158
重启网络
service network restart
相互验证一下,看看能不能 ping 通:
ping rmq157
在 158(磁盘节点)上执行:
cd /usr/local/soft/rabbitmq_server-3.8.9/sbin
./rabbitmq-server -detached >nohup.out&
./rabbitmqctl start_app
在 156、157(内存节点)上执行:
cd /usr/local/soft/rabbitmq_server-3.8.9/sbin
./rabbitmq-server -detached >nohup.out&
./rabbitmqctl start_app
./rabbitmqctl stop_app
./rabbitmqctl join_cluster --ram rabbit@rmq158
./rabbitmqctl start_app
查看集群状态
./rabbitmqctl cluster_status
也可以在任何一台机器的管理界面查看:

设置镜像队列,在三台机器上执行:
./rabbitmqctl set_policy ha-all “^” ‘{“ha-mode”:“all”}’
yum install -y haproxy
cd /etc/haproxy/
cp haproxy.cfg haproxy.cfg.bak
配置文件内容全部覆盖
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
log 127.0.0.1 local2
chroot /var/lib/haproxy # 改变当前工作目录
pidfile /var/run/haproxy.pid # haproxy 的 pid 存放路径,启动进程的用户必须有权限访问此文件
maxconn 4000 # 最大连接数,默认 4000
user root # 默认用户
group root # 默认组
daemon # 创建 1 个进程进入 deamon 模式运行。此参数要求将运行模式设置为 daemon
stats socket /var/lib/haproxy/stats # 创建监控所用的套接字目录
#---------------------------------------------------------------------
# defaults settings
#---------------------------------------------------------------------
# 注意:因为要使用 tcp 的负载,屏蔽掉与 http 相关的默认配置
defaults
mode http # 默认的模式 mode { tcp|http|health },tcp 是 4 层,http 是 7 层,health 只会返回 OK
log global
# option httplog # 采用 http 日志格式
option dontlognull # 启用该项,日志中将不会记录空连接。所谓空连接就是在上游的负载均衡器
# option http-server-close # 每次请求完毕后主动关闭 http 通道
# option forwardfor except 127.0.0.0/8 # 如果后端服务器需要获得客户端真实 ip 需要配置的参数,可以从 Http Header 中获得客户端 ip
option redispatch # serverId 对应的服务器挂掉后,强制定向到其他健康的服务器
retries 3 # 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 # 最大连接数
###################### 打开 haproxy 的监测界面###############################
listen status
bind 0.0.0.0:9188
mode http
stats enable
stats refresh 30s
stats uri /stats #设置 haproxy 监控地址为 http://localhost:9188/stats
stats auth admin:123456 #添加用户名密码认证
stats realm (Haproxy\ statistic)
stats admin if TRUE
######################监听 rabbitmq 的 web 操作页面############################
listen rabbitmq_admin
bind 0.0.0.0:15670
server rmq156 192.168.111.133:15672
server rmq157 192.168.111.134:15672
#######################监听 rabbimq_cluster #################################
listen rabbitmq_cluster
bind 0.0.0.0:5670
mode tcp
#负载均衡算法(#banlance roundrobin 轮询,balance source 保存 session 值,支持 static-rr,leastconn,first,uri 等参数)
balance roundrobin
#check inter 2000 是检测心跳频率
#rise 2 是 2 次正确认为服务器可用
#fall 2 是 2 次失败认为服务器不可用
server rabbit1 192.168.111.133:5672 check inter 5000 rise 2 fall 2
server rabbit2 192.168.111.134:5672 check inter 5000 rise 2 fall 2
haproxy -f /etc/haproxy/haproxy.cfg
其他命令:
service haproxy restart
service haproxy stop
192.168.44.156:15670
192.168.44.157:15670
这两个都能正常访问 RabbitMQ 管理界面,用 admin/admin 登录
192.168.44.156:9188/stats
192.168.44.157:9188/stats
这两个能正常访问 HaProxy 监控界面,用 amdin/123456 登录

yum install -y keepalived
cd /etc/keepalived
cp keepalived.conf keepalived.conf.bak
156 为 master,配置内容
global_defs {
router_id NodeA # 起一个唯一的名字,不能与备机相同
}
vrrp_script chk_haproxy {
script "/etc/keepalived/check_haproxy.sh"
interval 5
weight 2
}
vrrp_instance VI_1 {
state MASTER # 这里标记为主
interface eno16777736 # 这里的必须和你的网卡一致,ipconfig 获得
virtual_router_id 1 # 这里表示路由的唯一标识与备机一致
priority 100 # 配置优先级
advert_int 1
authentication {
auth_type PASS
auth_pass root # 配置认证密码
}
virtual_ipaddress { # 配置虚拟 ip 地址,就是开放的地址,与本机 ip 不同
192.168.111.159
}
track_script {
chk_haproxy
}
}
157 为 backup,配置内容
router_id、vrrp_instance state、priority 要修改
global_defs {
router_id NodeB
}
vrrp_script chk_haproxy {
script "/etc/keepalived/check_haproxy.sh"
interval 5
weight 2
}
vrrp_instance VI_1 {
state BACKUP # 通过 BACKUP 表示该机器为备机
interface eno16777736 # 与网卡一致,ipconfig 获得
virtual_router_id 1 # 与主机一致
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass root
}
virtual_ipaddress { # 开放的虚拟地址
192.168.111.159
}
track_script {
chk_haproxy
}
}
#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ];then
haproxy -f /etc/haproxy/haproxy.cfg
fi
sleep 2
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ];then
service keepalived stop
fi
作用:
如果 HAProxy 服务挂了,先重启。
如果重启不成功,关闭 Keepalived 服务,切换倒 backup
先启动 156 master,再启动 157 slave
service keepalived start
查看状态
service keepalived status
查看启动日志:
journalctl -xe
查看 keepalived 日志
tail -f /var/log/messages
访问:
192.168.111.159:15670
admin/admin 登录

应用连接到
192.168.111.159:5670 收发消息