RabbitMQ最基本的集群的通过 rabbitmqctl join_cluster 命令组建的,但是这种集群只会同步配置信息,不会同步队列里面的消息。要实现同步队列消息,需要在这个基础上,再设置 ha-mode 的 Policy,就可以了。
这里需要注意,因为RabbitMQ集群节点之间的通信是基于节点名的,所以在安装前,节点需要做以下准备工作:
- 每个节点设置好主机名,比如:node1、node2
- 每个节点设置好主机名的DNS解析
机器准备
两台腾讯云Ubuntu主机,10.0.16.12、10.0.16.16。
节点内网中,全部端口可相互访问。
节点的主机名设置为 node1、node2。
节点名可以通过DNS解析。
手动安装
【步骤1】在RabbitMQ官网,复制安装脚本,保存到文件 install-rabbitmq-cloudsmith.sh
代码语言:javascript复制#!/usr/bin/sh
sudo apt-get install curl gnupg apt-transport-https -y
## Team RabbitMQ's main signing key
curl -1sLf "https://keys.openpgp.org/vks/v1/by-fingerprint/0A9AF2115F4687BD29803A206B73A36E6026DFCA" | sudo gpg --dearmor | sudo tee /usr/share/keyrings/com.rabbitmq.team.gpg > /dev/null
## Cloudsmith: modern Erlang repository
curl -1sLf https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/gpg.E495BB49CC4BBE5B.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/io.cloudsmith.rabbitmq.E495BB49CC4BBE5B.gpg > /dev/null
## Cloudsmith: RabbitMQ repository
curl -1sLf https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/gpg.9F4587F226208342.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/io.cloudsmith.rabbitmq.9F4587F226208342.gpg > /dev/null
## Add apt repositories maintained by Team RabbitMQ
sudo tee /etc/apt/sources.list.d/rabbitmq.list <<EOF
## Provides modern Erlang/OTP releases
##
deb [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.E495BB49CC4BBE5B.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/deb/ubuntu bionic main
deb-src [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.E495BB49CC4BBE5B.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/deb/ubuntu bionic main
## Provides RabbitMQ
##
deb [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.9F4587F226208342.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/deb/ubuntu bionic main
deb-src [signed-by=/usr/share/keyrings/io.cloudsmith.rabbitmq.9F4587F226208342.gpg] https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/deb/ubuntu bionic main
EOF
## Update package indices
sudo apt-get update -y
## Install Erlang packages
sudo apt-get install -y erlang-base
erlang-asn1 erlang-crypto erlang-eldap erlang-ftp erlang-inets
erlang-mnesia erlang-os-mon erlang-parsetools erlang-public-key
erlang-runtime-tools erlang-snmp erlang-ssl
erlang-syntax-tools erlang-tftp erlang-tools erlang-xmerl
## Install rabbitmq-server and its dependencies
sudo apt-get install rabbitmq-server -y --fix-missing
【步骤2】在节点1,安装、配置RabbitMQ
代码语言:javascript复制sudo chmod x install-rabbitmq-cloudsmith.sh
./install-rabbitmq-cloudsmith.sh
# 设置管理员用户和密码
sudo rabbitmqctl add_user admin 123456
sudo rabbitmqctl set_user_tags admin administrator
sudo rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
# 设置镜像策略
sudo rabbitmqctl set_policy ha-all '' '{"ha-mode":"all"}'
# 开启管理页面
sudo rabbitmq-plugins enable rabbitmq_management
【步骤3】在节点2,安装、配置RabbitMQ
代码语言:javascript复制sudo chmod x install-rabbitmq-cloudsmith.sh
./install-rabbitmq-cloudsmith.sh
# 设置管理员用户和密码
sudo rabbitmqctl add_user admin 123456
sudo rabbitmqctl set_user_tags admin administrator
sudo rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
# 开启管理页面
sudo rabbitmq-plugins enable rabbitmq_management
【步骤4】将节点1的 /var/lib/rabbitmq/.erlang.cookie 拷贝到节点2的 /var/lib/rabbitmq/.erlang.cookie。这样节点2才能和节点1通信。
【步骤5】节点2加入节点1的集群
代码语言:javascript复制sudo systemctl restart rabbitmq-server
sudo rabbitmqctl stop_app
sudo rabbitmqctl join_cluster rabbit@node1
sudo rabbitmqctl start_app
查看集群状态
代码语言:javascript复制> sudo rabbitmqctl cluster_status
Cluster status of node rabbit@node2 ...
Basics
Cluster name: rabbit@localhost.localdomain
Disk Nodes
rabbit@node1
rabbit@node2
Running Nodes
rabbit@node1
rabbit@node2
Versions
rabbit@node1: RabbitMQ 3.9.15 on Erlang 24.3.3
rabbit@node2: RabbitMQ 3.9.15 on Erlang 24.3.3
Maintenance status
Node: rabbit@node1, status: not under maintenance
Node: rabbit@node2, status: not under maintenance
Alarms
(none)
Network Partitions
(none)
Listeners
Node: rabbit@node1, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@node1, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@node1, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@node2, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@node2, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@node2, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Feature flags
Flag: drop_unroutable_metric, state: disabled
Flag: empty_basic_get_metric, state: disabled
Flag: implicit_default_bindings, state: enabled
Flag: maintenance_mode_status, state: enabled
Flag: quorum_queue, state: enabled
Flag: stream_queue, state: enabled
Flag: user_limits, state: enabled
Flag: virtual_host_metadata, state: enabled
这样RabbitMQ镜像集群就安装好了!
流水线安装
通过 Y20持续部署系统,把以上步骤编排为流水线 部署RabbitMQ镜像集群,可以实现自动部署。
下面演示一下,更多细节请登录 y20.work