RabbitMQ是常用的一款消息中间件,那么如何在我们虚拟机中创建其集群呢?跟着博主这篇文章让你一步到位
本篇搭建的是三台机器为一个集群!假设大家虚拟机都为初始化状态,从0开始(注意集群搭建需要CentOS8以上环境!可以点击官网下载)
1. 配置网络
先对三台机器配各自的网络,具体实例如下图所示(ip根据自己动态调整),最好先操作node01,在步骤3完成后再克隆
1.1 以node01为例子,创建setup_network.sh脚本文件
注意在bin目录下创建一个可执行文件
vim /bin/setup_network.sh
记得根据自己的网卡调整,我这里用的是第二张网卡ens36,一般是第一个ens33所以根据自己虚拟机情况调整
#!/bin/bash
# 检查是否提供了IP地址参数
if [ $# -ne 1 ]; then
echo "Usage: $0 <IP-ADDRESS>"
exit 1
fi
# 保存传入的IP地址
IPADDR=$1
# 删除ens36网络连接
nmcli c del ens36
# 编辑网络配置文件
cat > /etc/sysconfig/network-scripts/ifcfg-ens36 << EOF
TYPE=Ethernet
NAME=ens36
DEVICE=ens36
ONBOOT=yes
IPADDR=$IPADDR # ip传进来
NETMASK=255.255.255.0
GATEWAY=192.168.187.2
DNS1=114.114.114.114
DNS2=8.8.8.8
EOF
# 重启NetworkManager服务
systemctl restart NetworkManager
# 启用ens36网络连接
nmcli c up ens36
1.2 运行脚本
#首先修改权限
chmod 777 /bin/setup_network.sh
#运行 以第一台机器为例子(第二台.101 第三台.102)
/bin/setup_network.sh 192.168.187.100
2. 安装Erlang环境
RabbitMQ必要的运行环境,在docker中就集成好了依赖,单独安装集群就得重新安装
2.1 创建yum库配置文件
vim /etc/yum.repos.d/rabbitmq.repo
一下配置内容出自官方文档:
https://www.rabbitmq.com/docs/install-rpm
# In /etc/yum.repos.d/rabbitmq.repo
##
## Zero dependency Erlang RPM
##
[modern-erlang]
name=modern-erlang-el8
# uses a Cloudsmith mirror @ yum.novemberain.com in addition to its Cloudsmith upstream.
# Unlike Cloudsmith, the mirror does not have any traffic quotas
baseurl=https://yum1.novemberain.com/erlang/el/8/$basearch
https://yum2.novemberain.com/erlang/el/8/$basearch
https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/rpm/el/8/$basearch
repo_gpgcheck=1
enabled=1
gpgkey=https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-erlang.E495BB49CC4BBE5B.key
gpgcheck=1
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md
[modern-erlang-noarch]
name=modern-erlang-el8-noarch
# uses a Cloudsmith mirror @ yum.novemberain.com.
# Unlike Cloudsmith, it does not have any traffic quotas
baseurl=https://yum1.novemberain.com/erlang/el/8/noarch
https://yum2.novemberain.com/erlang/el/8/noarch
https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/rpm/el/8/noarch
repo_gpgcheck=1
enabled=1
gpgkey=https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-erlang.E495BB49CC4BBE5B.key
https://github.com/rabbitmq/signing-keys/releases/download/3.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
[modern-erlang-source]
name=modern-erlang-el8-source
# uses a Cloudsmith mirror @ yum.novemberain.com.
# Unlike Cloudsmith, it does not have any traffic quotas
baseurl=https://yum1.novemberain.com/erlang/el/8/SRPMS
https://yum2.novemberain.com/erlang/el/8/SRPMS
https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/rpm/el/8/SRPMS
repo_gpgcheck=1
enabled=1
gpgkey=https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-erlang.E495BB49CC4BBE5B.key
https://github.com/rabbitmq/signing-keys/releases/download/3.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
##
## RabbitMQ Server
##
[rabbitmq-el8]
name=rabbitmq-el8
baseurl=https://yum2.novemberain.com/rabbitmq/el/8/$basearch
https://yum1.novemberain.com/rabbitmq/el/8/$basearch
https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/rpm/el/8/$basearch
repo_gpgcheck=1
enabled=1
# Cloudsmith's repository key and RabbitMQ package signing key
gpgkey=https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-server.9F4587F226208342.key
https://github.com/rabbitmq/signing-keys/releases/download/3.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-el8-noarch]
name=rabbitmq-el8-noarch
baseurl=https://yum2.novemberain.com/rabbitmq/el/8/noarch
https://yum1.novemberain.com/rabbitmq/el/8/noarch
https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/rpm/el/8/noarch
repo_gpgcheck=1
enabled=1
# Cloudsmith's repository key and RabbitMQ package signing key
gpgkey=https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-server.9F4587F226208342.key
https://github.com/rabbitmq/signing-keys/releases/download/3.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-el8-source]
name=rabbitmq-el8-source
baseurl=https://yum2.novemberain.com/rabbitmq/el/8/SRPMS
https://yum1.novemberain.com/rabbitmq/el/8/SRPMS
https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/rpm/el/8/SRPMS
repo_gpgcheck=1
enabled=1
gpgkey=https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-server.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
2.2 更新yum库
# --nobest表示所需安装包即使不是最佳选择也接受
yum update -y --nobest
2.3 正式安装Erlang
yum install -y erlang
3. 安装RabbitMQ
由于wget下载github老连接不上🙃,这里就采用宿主机下载然后ftp传到虚拟机上
RPM包下载地址:https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.13.0/rabbitmq-server-3.13.0-1.el8.noarch.rpm
这里我放到/opt目录下
3.1 解压
rpm -ivh rabbitmq-server-3.13.0-1.el8.noarch.rpm
3.2 RabbitMQ基础配置
# 启用管理界面插件
rabbitmq-plugins enable rabbitmq_management
# 启动 RabbitMQ 服务:
systemctl start rabbitmq-server
# 将 RabbitMQ 服务设置为开机自动启动
systemctl enable rabbitmq-server
# 新增登录账号密码
rabbitmqctl add_user charles 123456
# 设置登录账号权限
rabbitmqctl set_user_tags charles administrator
rabbitmqctl set_permissions -p / charles ".*" ".*" ".*"
# 配置所有稳定功能 flag 启用
rabbitmqctl enable_feature_flag all
# 重启RabbitMQ服务生效
systemctl restart rabbitmq-server
注意最后要删掉一开始的rpm配置文件,因为需要的东西已经下载完了
rm -rf /etc/yum.repos.d/rabbitmq.repo
其实到这步后直接克隆就可以啦,省去繁琐的下载和配置时间,记得根据操作1换网络ip就好啦
4. 修改主机名字
# node01 、node02 、node03
vim /etc/hostname
5. 集群节点彼此发现
5.1 node01设置
①设置 IP 地址到主机名称的映射
修改文件/etc/hosts,追加如下内容:
192.168.187.100 node01
192.168.187.101 node02
192.168.187.102 node03
②查看当前RabbitMQ节点的Cookie值并记录
[root@node01 ~]# cat /var/lib/rabbitmq/.erlang.cookie
AEZXBETHCPXIACMJTZCB
③重置节点应用
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
5.2 node02设置
①设置 IP 地址到主机名称的映射
修改文件/etc/hosts,追加如下内容:
192.168.187.100 node01
192.168.187.101 node02
192.168.187.102 node03
②修改当前RabbitMQ节点的Cookie值
node02和node03都改成和node01一样:
vim /var/lib/rabbitmq/.erlang.cookie
③重置节点应用并加入集群
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@node01
rabbitmqctl start_app
5.3 node03设置
①设置 IP 地址到主机名称的映射
修改文件/etc/hosts,追加如下内容:
192.168.187.100 node01
192.168.187.101 node02
192.168.187.102 node03
②修改当前RabbitMQ节点的Cookie值
node02和node03都改成和node01一样:
vim /var/lib/rabbitmq/.erlang.cookie
③重置节点应用并加入集群
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@node01
rabbitmqctl start_app
④查看集群状态
rabbitmqctl cluster_status
到这里集群的搭建就完成啦🎉
5.4 常用操作
如有需要踢出某个节点,则按下面操作执行:
# 被踢出的节点:
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
# 节点1
rabbitmqctl forget_cluster_node rabbit@node02