1、准备环境
准备3台服务器
关闭所有服务器防火墙和selinux
修改3台服务器主机名
[root@rabbitmq-1 ~]# hostnamectl set-hostname rabbitmq-1
[root@rabbitmq-2 ~]# hostnamectl set-hostname rabbitmq-2
[root@rabbitmq-3 ~]# hostnamectl set-hostname rabbitmq-3
三台服务器都做域名解析
[root@rabbitmq-1 ~]# vim /etc/hosts
10.36.192.139 rabbitmq-1
10.36.192.99 rabbitmq-2
10.36.192.39 rabbitmq-3
[root@rabbitmq-1 ~]# scp /etc/hosts 10.36.192.99:/etc
[root@rabbitmq-1 ~]# scp /etc/hosts 10.36.192.39:/etc
2、三台服务器都安装rabbitmq
安装依赖环境
[root@rabbitmq-1 ~]# yum install -y epel-release gcc-c++ unixODBC unixODBC-devel openssl-devel ncurses-devel
[root@rabbitmq-1 ~]# curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bash
[root@rabbitmq-1 ~]# yum install erlang-21.3.8.21-1.el7.x86_64
安装rabbitmq
https://github.com/rabbitmq/rabbitmq-server/releases/tag/v3.7.10
[root@rabbitmq-1 ~]# wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.10/rabbitmq-server-3.7.10-1.el7.noarch.rpm
[root@rabbitmq-1 ~]# yum install rabbitmq-server-3.7.10-1.el7.noarch.rpm
开启远程登录
[root@rabbitmq-1 ~]# cp /usr/share/doc/rabbitmq-server-3.7.10/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
[root@rabbitmq-1 ~]# vim /etc/rabbitmq/rabbitmq.config
打开loopback_users的注释(61行)并删除结尾逗号
%% Uncomment the following line if you want to allow access to the
%% guest user from anywhere on the network.
{loopback_users, []}
[root@rabbitmq-1 ~]# systemctl restart rabbitmq-server
开启rabbitmq的web访问界面:
[root@rabbitmq-1 ~]# rabbitmq-plugins enable rabbitmq_management
3、配置集群
1、首先创建好数据存放目录和日志存放目录:(三台服务器都操作)
[root@rabbitmq-1 ~]# mkdir -p /data/rabbitmq/data
[root@rabbitmq-1 ~]# mkdir -p /data/rabbitmq/logs
[root@rabbitmq-1 ~]# chmod 777 -R /data/rabbitmq
[root@rabbitmq-1 ~]# chown rabbitmq.rabbitmq /data/ -R
创建配置文件:
[root@rabbitmq-1 ~]# vim /etc/rabbitmq/rabbitmq-env.conf
[root@rabbitmq-1 ~]# cat /etc/rabbitmq/rabbitmq-env.conf
RABBITMQ_MNESIA_BASE=/data/rabbitmq/data
RABBITMQ_LOG_BASE=/data/rabbitmq/logs
重启服务
[root@rabbitmq-1 ~]# systemctl restart rabbitmq-server
2、Rabbitmq的集群是依附于erlang的集群来⼯作的,所以必须先构建起erlang的集群景象。Erlang的集群中各节点是经由过程⼀个magic cookie来实现的,这个cookie存放在/var/lib/rabbitmq/.erlang.cookie中,⽂件是400
的权限。所以必须保证各节点cookie⼀致,不然节点之间就⽆法通信.
如果执行# rabbitmqctl stop_app 这条命令报错:需要执行
#如果执行# rabbitmqctl stop_app 这条命令报错:需要执行
#chmod 400 .erlang.cookie
#chown rabbitmq.rabbitmq .erlang.cookie
官方在介绍集群的文档中提到过.erlang.cookie 一般会存在这两个地址:第一个是home/.erlang.cookie
;第二个地方就是/var/lib/rabbitmq/.erlang.cookie
。如果我们使用解压缩方式安装部署的rabbitmq,那么这个文件会在{home}目录下,也就是$home/.erlang.cookie
。如果我们使用rpm等安装包方式进行安装的,那么这个文件会在/var/lib/rabbitmq目录下。)
⽤scp的⽅式将rabbitmq-1节点的.erlang.cookie的值复制到其他两个节点中。
[root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@10.36.192.99:/var/lib/rabbitmq/
[root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@10.36.192.39:/var/lib/rabbitmq/
3、将mq-2、mq-3作为内存节点加⼊mq-1节点集群中
在mq-2、mq-3执⾏如下命令:
[root@rabbitmq-2 ~]# systemctl restart rabbitmq-server
[root@rabbitmq-2 ~]# rabbitmqctl stop_app
[root@rabbitmq-2 ~]# rabbitmqctl reset
[root@rabbitmq-2 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1
Clustering node 'rabbit@rabbitmq-2' with 'rabbit@rabbitmq-1' ...
[root@rabbitmq-2 ~]# rabbitmqctl start_app
Starting node 'rabbit@rabbitmq-2' ...
======================================================================
[root@rabbitmq-3 ~]# systemctl restart rabbitmq-server
[root@rabbitmq-3 ~]# rabbitmqctl stop_app
Stopping node 'rabbit@rabbitmq-3' ...
[root@rabbitmq-3 ~]# rabbitmqctl reset
Resetting node 'rabbit@rabbitmq-3' ...
[root@rabbitmq-3 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1
Clustering node 'rabbit@rabbitmq-3' with 'rabbit@rabbitmq-1' ...
[root@rabbitmq-3 ~]# rabbitmqctl start_app
Starting node 'rabbit@rabbitmq-3' ...
(1)默认rabbitmq启动后是磁盘节点,在这个cluster命令下,mq-2和mq-3是内存节点,
mq-1是磁盘节点。
(2)如果要使mq-2、mq-3都是磁盘节点,去掉--ram参数即可。
(3)如果想要更改节点类型,可以使⽤命令rabbitmqctl change_cluster_node_type
disc(ram),前提是必须停掉rabbit应⽤
注:
#如果有需要使用磁盘节点加入集群
[root@rabbitmq-2 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-1
[root@rabbitmq-3 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-1
4、查看集群状态
在 RabbitMQ 集群任意节点上执行 rabbitmqctl cluster_status来查看是否集群配置成功。
在mq-1磁盘节点上面查看
[root@rabbitmq-1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq-1 ...
[{nodes,[{disc,['rabbit@rabbitmq-1','rabbit@rabbitmq-3']},
{ram,['rabbit@rabbitmq-2']}]},
{running_nodes,['rabbit@rabbitmq-3','rabbit@rabbitmq-2','rabbit@rabbitmq-1']},
{cluster_name,<<"rabbit@rabbitmq-1">>},
{partitions,[]},
{alarms,[{'rabbit@rabbitmq-3',[]},
{'rabbit@rabbitmq-2',[]},
{'rabbit@rabbitmq-1',[]}]}]
5、登录ip访问网站
6、镜像集群配置
[root@rabbitmq-1 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
[root@rabbitmq-2 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
[root@rabbitmq-3 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
4、安装并配置负载均衡器HA
注意:如果使用阿里云,可以使用阿里云的内网slb来实现负载均衡,不用自己搭建HA。
安装并配置负载均衡器HA
1、在10.36.192.139安装HAProxy
[root@rabbitmq-1 ~]# yum -y install haproxy
2、修改 /etc/haproxy/haproxy.cfg
[root@rabbitmq-1 ~]# cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
[root@rabbitmq-1 ~]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
nbproc 4
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
defaults
mode http
log global
retries 3
timeout connect 10s
timeout client 1m
timeout server 1m
timeout check 10s
maxconn 2048
#---------------------------------------------------------------------
##监控查看本地状态#####
listen admin_stats
bind *:80
mode http
option httplog
option httpclose
log 127.0.0.1 local0 err
stats uri /haproxy
stats auth newrain:123456
stats refresh 30s
####################################
###反代监控
frontend server
bind *:5670
log global
mode tcp
#option forwardfor
default_backend rabbitmq
maxconn 3
backend rabbitmq
mode tcp
log global
balance roundrobin
server rabbitmq1 10.36.192.139:5672 check inter 2000s rise 2 fall 3
server rabbitmq2 10.36.192.99:5672 check inter 2000s rise 2 fall 3
server rabbitmq3 10.36.192.39:5672 check inter 2000s rise 2 fall 3
[root@rabbitmq-1 ~]# systemctl start haproxy
浏览器输入http://10.36.192.139/haproxy查看rabbitmq的状态。