RabbitMQ集群及镜像配置部署详解
一、注意事项
1、准备三台服务器,RabbitMQ集群节点必须在同一网段,如有跨区域效果不好
2、关闭防火墙和selinux
二、部署步骤
1、修改主机名称,添加解析,配置hosts文件更改三台MQ节点的计算机名分别为rabbitmq-1、rabbitmq-2 和rabbitmq-3,然后修改hosts配置件(三台机器都要做)
#systemctl stop firewalld
#systemctl disable firewalld
#setenforce 0
[root@rabbitmq-1 ~]# hostnamectl set-hostname rabbitmq-1
[root@rabbitmq-1 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.50.138 rabbitmq-1
192.168.50.139 rabbitmq-2
192.168.50.140 rabbitmq-3
2、三台机器配置rabbitmw软件(三台机器都要做)
[root@rabbitmq-1 ~]# yum install -y *epel* gcc-c++ unixODBC unixODBC-devel openssl-devel ncurses-devel //安装依赖
[root@rabbitmq-1 ~]# vim /etc/yum.repo.d/rabbitmq.repo//去官网下载yum源码包安装erlang
[rabbitmq_erlang]
name=rabbitmq_erlang
baseurl=https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/rpm/el/7/$basearch
repo_gpgcheck=1
enabled=1
# Cloudsmith's repository key and RabbitMQ package signing key
gpgkey=https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/gpg.E495BB49CC4BBE5B.key
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
gpgcheck=1
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md
[rabbitmq_erlang-noarch]
name=rabbitmq_erlang-noarch
baseurl=https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/rpm/el/7/noarch
repo_gpgcheck=1
enabled=1
# Cloudsmith's repository key and RabbitMQ package signing key
gpgkey=https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/gpg.E495BB49CC4BBE5B.key
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
gpgcheck=1
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md
[rabbitmq_erlang-source]
name=rabbitmq_erlang-source
baseurl=https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/rpm/el/7/SRPMS
repo_gpgcheck=1
enabled=1
gpgkey=https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/gpg.E495BB49CC4BBE5B.key
gpgcheck=0
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md
##
## RabbitMQ server
##
[rabbitmq-server]
name=rabbitmq_server
baseurl=https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/rpm/el/7/$basearch
repo_gpgcheck=1
enabled=1
# Cloudsmith's repository key and RabbitMQ package signing key
gpgkey=https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/gpg.9F4587F226208342.key
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
gpgcheck=1
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md
[rabbitmq_server-noarch]
name=rabbitmq_server-noarch
baseurl=https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/rpm/el/7/noarch
repo_gpgcheck=1
enabled=1
# Cloudsmith's repository key and RabbitMQ package signing key
gpgkey=https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/gpg.9F4587F226208342.key
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
gpgcheck=1
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md
[rabbitmq_server-source]
name=rabbitmq_server-source
baseurl=https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/rpm/el/7/SRPMS
repo_gpgcheck=1
enabled=1
gpgkey=https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/gpg.9F4587F226208342.key
gpgcheck=0
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md
[root@rabbitmq-1 ~]#yum -y install erlang//按装erlang,如有报错修改配置文件gpgcheck=0或者重装一遍
[root@rabbitmq-1 ~]# erl //测试安装成功
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)
1> //按两次Ctrl+c 退出登录
[root@rabbitmq-1 ~]# wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.8.16/rabbitmq-server-3.8.16-1.el7.noarch.rpm //下载rabbitmq包
[root@rabbitmq-1 ~]# yum install rabbitmq-server-3.8.16-1.el7.noarch.rpm -y
3、启动(三台都做)
[root@rabbitmq-1 ~]# systemctl daemon-reload
[root@rabbitmq-1 ~]# systemctl start rabbitmq-server
[root@rabbitmq-1 ~]# systemctl enable rabbitmq-server
[root@rabbitmq-1 ~]# systemctl status rabbitmq-server
老版本用此方法启动(Centos 7以前的)
[root@rabbitmq-1 ~]# /sbin/service rabbitmq-server status ---查看状态
[root@rabbitmq-1 ~]# /sbin/service rabbitmq-server start ---启动
[root@rabbitmq-1 ~]# rabbitmq-plugins enable rabbitmq_management
看到此页面说明启动成功
4、创建用户
注意:在一台机器操作
添加用户和密码
[root@rabbitmq-1 ~]# rabbitmqctl add_user yy 123 //"yy"用户 "123"密码
Adding user "yy" ...
设置为管理员
[root@rabbitmq-1 ~]# rabbitmqctl set_user_tags soho administrator
Setting tags for user "yy" to [administrator] ...
查看用户
[root@rabbitmq-1 ~]# rabbitmqctl list_users
Listing users ...
guest [administrator]
soho [administrator]
...done.
[root@rabbitmq-1 ~]# rabbitmqctl set_permissions -p "/" yy ".*" ".*" ".*" //授权
Setting permissions for user "yy" in vhost "/" ...
此处设置权限时注意'.*'之间需要有空格 三个'.*'分别代表了conf权限,read权限与write权限,如yy设置这三个权限前是没有权限查询队列,在ui界面也看不见
5、开启远程登录(三台机器都做)
root@rabbitmq-1 ~]# cd /etc/rabbitmq/
[root@rabbitmq-1 rabbitmq]# cp /usr/share/doc/rabbitmq-server-3.8.16/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
[root@rabbitmq-1 rabbitmq]# ls
enabled_plugins rabbitmq.config
[root@rabbitmq-1 rabbitmq]# vim rabbitmq.config
三台机器都操作重启服务服务:
[root@rabbitmq-1 ~]# systemctl restart rabbitmq-server
6、查看端口是否启动(!注意如果是云服务器,切记添加安全组端口放行)
[root@rabbitmq-1 rabbitmq]#netstat -lntp
如果查询失败下载yum -y install net-tols工具
4369 -- erlang端口
5672 --程序连接端口
15672 -- 管理界面ui端口
25672 -- server间内部通信端口
7、打开浏览器登录Web页面192.168.239.132:15672(三台机器都做)
rabbitmq默认管理员用户:guest 密码:guest
新添加的用户为:yy 密码:123
8、集群部署(三台都做)
首先创建好数据存放目录和日志存放目录:
[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 //修改配置文件(三台一样)
RABBITMQ_MNESIA_BASE=/data/rabbitmq/data //定义数据存放的目录
RABBITMQ_LOG_BASE=/data/rabbitmq/logs //定义日志存放的目录
[root@rabbitmq-1 ~]# systemctl restart rabbitmq-server
9、搭建erlang的集群
Rabbitmq的集群是依附于erlang的集群来工作的,所以必须先构建起erlang的集群。Erlang的集群中各节点是经由各个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目录下
[root@rabbitmq-1 ~]# cat /var/lib/rabbitmq/.erlang.cookie
HOUCUGJDZYTFZDSWXTHJ
scp的方式将rabbitmq-1节点的.erlang.cookie的值复制到其他两个节点中。
[root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@192.168.239.129:/var/lib/rabbitmq/
[root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@192.168.239.128:/var/lib/rabbitmq/
[root@rabbitmq-1 ~]#systemctl restart rabbitmq-server
10、将将mq-2、mq-3作为内存节点加到mq-1节点集群中
[root@rabbitmq-2 ~]# rabbitmqctl stop_app #停止节点,切记不是停止服务
[root@rabbitmq-2 ~]# rabbitmqctl reset #如果有数据需要重置,没有则不用
注意查看回显,是否报错,如有则重启rabbitmq服务
将两个节点加入集群:
[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 //加入节点到1
Clustering node 'rabbit@rabbitmq-3' with 'rabbit@rabbitmq-1' ...
[root@rabbitmq-3 ~]# rabbitmqctl start_app //开启服务
Starting node 'rabbit@rabbitmq-3' ...
completed with 3 plugins.
机器2同理
(1)默认rabbitmq启动后是磁盘节点,在这个cluster命令下,mq-2和mq-3是内存节点,
mq-1是磁盘节点。
(2)如果要使mq-2、mq-3都是磁盘节点,去掉–ram参数即可。
(3)如果想要更改节点类型,可以使用命令rabbitmqctl change_cluster_node_type
disc(ram),前提是必须停掉rabbitmq应用
#如果有需要使用磁盘节点加入集群
[root@rabbitmq-2 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-1
[root@rabbitmq-3 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-1
11、查看集群状态(三台都需要查看是否相同)
[root@rabbitmq-1 ~]# rabbitmqctl cluster_status
12、登录rabbitmq web管理控制台,创建新的队列
打开浏览器输入http://192.168.239.132:15672,
输入默认的Username:guest
输入默认的Password:guest
三台机器都要登录查看集群是否创建成功
13、RabbitMQ镜像集群配置
上面已经完成RabbitMQ默认集群模式,**但并不保证队列的高可用性,队列内容不会复制。如果队列节点宕机直接导致该队列无法应用,只能等待重启,所以要想在队列节点宕机或故障也能正常应用,就要复制队列内容到集群里的每个节点,必须要创建镜像队列
创建镜像集群:在任意一台机器操作
[root@rabbitmq-1 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
Setting policy "ha-all" for pattern "^" to "{"ha-mode":"all"}" with priority "0" for vhost "/" ...
"^"匹配所有的队列, ha-all 策略名称为ha-all, '{"ha-mode":"all"}' 策略模式为 all 即复制到所有节点,包含新增节点。
再次查看队列已经同步到其他两台节点:
"^"匹配所有的队列, ha-all 策略名称为ha-all, ‘{“ha-mode”:“all”}’ 策略模式为 all 即复制到所有节点,包含新增节点。
已经部署完成将所有队列设置为镜像队列,即队列会被复制到各个节点,各个节点状态保持一致