centos 安装rabbitmq集群

当然,我会提供一个更加详细的步骤指南,帮助你在三台 CentOS 服务器上搭建 RabbitMQ 集群。这些服务器的 IP 地址分别为 10.206.0.4、10.206.0.8 和 10.206.0.13。

步骤 1: 安装 Erlang

RabbitMQ 需要 Erlang 运行环境。在每台服务器上执行以下步骤:

  1. 添加 Erlang 仓库:

    sudo yum install -y epel-release
    sudo yum install -y https://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm
    
  2. 安装 Erlang:

    sudo yum install -y erlang
    
  3. 验证 Erlang 安装:

    erl -version
    

步骤 2: 安装 RabbitMQ

接下来,在每台服务器上安装 RabbitMQ:

  1. 添加 RabbitMQ 仓库:

    sudo rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc
    sudo yum install -y https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm
    
  2. 安装 RabbitMQ:

    sudo yum install -y rabbitmq-server
    
  3. 启动 RabbitMQ 服务并设置开机自启:

    sudo systemctl start rabbitmq-server
    sudo systemctl enable rabbitmq-server
    
  4. 启用 RabbitMQ 管理控制台(可选):

    sudo rabbitmq-plugins enable rabbitmq_management
    
  5. 重启 RabbitMQ 服务:

    sudo systemctl restart rabbitmq-server
    

步骤 3: 设置 Hostname 和 /etc/hosts

在每台服务器上执行以下步骤,以确保节点之间能够相互识别:

  1. 设置每台服务器的 Hostname:

    • 在 10.206.0.4 上:
      sudo hostnamectl set-hostname node1
      
    • 在 10.206.0.8 上:
      sudo hostnamectl set-hostname node2
      
    • 在 10.206.0.13 上:
      sudo hostnamectl set-hostname node3
      
  2. 更新每台服务器的 /etc/hosts 文件:

    • 在每台服务器上,添加以下行:
      10.206.0.4 node1
      10.206.0.8 node2
      10.206.0.13 node3
      
    • 你可以使用 sudo vi /etc/hosts 来编辑文件。

步骤 4: 配置和启动 RabbitMQ 集群

  1. 在 node1 上创建一个集群:
    RabbitMQ 服务已经在前面的步骤中启动。

  2. 复制 Erlang Cookie:

    • Erlang Cookie 用于节点之间的身份验证。从 node1 复制 .erlang.cookie 文件到 node2 和 node3:
      scp /var/lib/rabbitmq/.erlang.cookie root@node2:/var/lib/rabbitmq/
      scp /var/lib/rabbitmq/.erlang.cookie root@node3:/var/lib/rabbitmq/
      
    • 确保在 node2 和 node3 上的 /var/lib/rabbitmq/.erlang.cookie 文件权限为 400:
      sudo chmod 400 /var/lib/rabbitmq/.erlang.cookie
      
  3. 在 node2 和 node3 上加入集群:

    • 在 node2 和 node3 上,停止 RabbitMQ 服务:
      sudo systemctl stop rabbitmq-server
      
    • 清除 mnesia 数据库:
      sudo rm -rf /var/lib/rabbitmq/mnesia/*
      
    • 重新启动 RabbitMQ 服务:
      sudo systemctl start rabbitmq-server
      
    • 将 node2 和

node3 加入到集群:
- 在 node2 上:
bash sudo rabbitmqctl stop_app sudo rabbitmqctl join_cluster rabbit@node1 sudo rabbitmqctl start_app
- 在 node3 上,重复上述步骤,但将 join_cluster 命令中的 rabbit@node1 改为 rabbit@node2

  1. 检查集群状态:
    • 在任何节点上执行以下命令来检查集群状态:
      sudo rabbitmqctl cluster_status
      

步骤 5: 设置镜像队列(可选)

如果你想要增强数据的持久性和高可用性,你可以设置镜像队列:

sudo rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}'

这将应用于所有队列,使它们在集群的所有节点上都有镜像。

注意事项

  • 确保在所有服务器上正确设置了 Erlang Cookie,并且文件权限正确。
  • 在配置集群之前,确保所有 RabbitMQ 节点可以相互网络通信。
  • 如果在集群配置过程中遇到问题,请检查 RabbitMQ 日志文件以获取更多信息。日志文件通常位于 /var/log/rabbitmq/
  • 保持集群中所有 RabbitMQ 节点的版本一致。

安装机器日志

[root@master rabbitmq]# erl -version
Erlang (SMP,ASYNC_THREADS,HIPE) (BEAM) emulator version 10.4.4
[root@master rabbitmq]# sudo rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc
[root@master rabbitmq]# sudo yum install -y https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm
Repository docker-ce-stable is listed more than once in the configuration
Repository docker-ce-stable-source is listed more than once in the configuration
Repository docker-ce-test is listed more than once in the configuration
Repository docker-ce-test-source is listed more than once in the configuration
Last metadata expiration check: 0:06:16 ago on Sat 20 Jan 2024 04:43:58 PM CST.
[MIRROR] rabbitmq-server-3.8.9-1.el7.noarch.rpm: Curl error (28): Timeout was reached for https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm [Connection timed out after 30000 milliseconds]
[MIRROR] rabbitmq-server-3.8.9-1.el7.noarch.rpm: Curl error (28): Timeout was reached for https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm [Connection timed out after 30000 milliseconds]
[MIRROR] rabbitmq-server-3.8.9-1.el7.noarch.rpm: Curl error (28): Timeout was reached for https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm [Connection timed out after 30000 milliseconds]
[MIRROR] rabbitmq-server-3.8.9-1.el7.noarch.rpm: Curl error (28): Timeout was reached for https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm [Connection timed out after 30001 milliseconds]
[FAILED] rabbitmq-server-3.8.9-1.el7.noarch.rpm: Curl error (28): Timeout was reached for https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm [Connection timed out after 30001 milliseconds]
Curl error (28): Timeout was reached for https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm [Connection timed out after 30001 milliseconds]
[root@master rabbitmq]# sudo yum clean all
Repository docker-ce-stable is listed more than once in the configuration
Repository docker-ce-stable-source is listed more than once in the configuration
Repository docker-ce-test is listed more than once in the configuration
Repository docker-ce-test-source is listed more than once in the configuration
69 files removed
[root@master rabbitmq]# sudo yum install -y https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm
Repository docker-ce-stable is listed more than once in the configuration
Repository docker-ce-stable-source is listed more than once in the configuration
Repository docker-ce-test is listed more than once in the configuration
Repository docker-ce-test-source is listed more than once in the configuration
TencentOS Server 3.1 - TencentOS                                                                                         4.9 MB/s | 3.1 MB     00:00    
TencentOS Server 3.1 - Updates                                                                                            10 MB/s |  30 MB     00:02    
TencentOS Server 3.1 - TencentOS-AppStream                                                                                17 MB/s |  20 MB     00:01    
TencentOS Server 3.1 - Base                                                                                              1.5 kB/s | 257  B     00:00    
TencentOS Server 3.1 - AppStream                                                                                         1.6 kB/s | 257  B     00:00    
TencentOS Server 3.1 - Extras                                                                                            138 kB/s |  25 kB     00:00    
TencentOS Server 3.1 - PowerTools                                                                                        7.7 kB/s | 1.2 kB     00:00    
Docker CE Stable - x86_64                                                                                                273 kB/s |  50 kB     00:00    
Extra Packages for TencentOS Server 3.1 - x86_64                                                                          24 MB/s |  16 MB     00:00    
Extra Packages for TencentOS Server 3.1 Modular - x86_64                                                                 4.8 MB/s | 733 kB     00:00    
Kubernetes                                                                                                               1.4 MB/s | 182 kB     00:00    
[MIRROR] rabbitmq-server-3.8.9-1.el7.noarch.rpm: Curl error (28): Timeout was reached for https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm [Connection timed out after 30000 milliseconds]
[MIRROR] rabbitmq-server-3.8.9-1.el7.noarch.rpm: Curl error (28): Timeout was reached for https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm [Connection timed out after 30000 milliseconds]
[MIRROR] rabbitmq-server-3.8.9-1.el7.noarch.rpm: Curl error (28): Timeout was reached for https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm [Connection timed out after 30000 milliseconds]
[MIRROR] rabbitmq-server-3.8.9-1.el7.noarch.rpm: Curl error (28): Timeout was reached for https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm [Connection timed out after 30000 milliseconds]
[FAILED] rabbitmq-server-3.8.9-1.el7.noarch.rpm: Curl error (28): Timeout was reached for https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm [Connection timed out after 30000 milliseconds]
Curl error (28): Timeout was reached for https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.9/rabbitmq-server-3.8.9-1.el7.noarch.rpm [Connection timed out after 30000 milliseconds]
[root@master rabbitmq]# ls
rabbitmq-server-3.8.9-1.el7.noarch.rpm
[root@master rabbitmq]# 
[root@master rabbitmq]#  yum install -y rabbitmq-server-3.8.9-1.el7.noarch.rpm
Repository docker-ce-stable is listed more than once in the configuration
Repository docker-ce-stable-source is listed more than once in the configuration
Repository docker-ce-test is listed more than once in the configuration
Repository docker-ce-test-source is listed more than once in the configuration
Last metadata expiration check: 0:09:26 ago on Sat 20 Jan 2024 04:54:31 PM CST.
Dependencies resolved.
=========================================================================================================================================================
 Package                                 Architecture                   Version                               Repository                            Size
=========================================================================================================================================================
Installing:
 rabbitmq-server                         noarch                         3.8.9-1.el7                           @commandline                          15 M

Transaction Summary
=========================================================================================================================================================
Install  1 Package

Total size: 15 M
Installed size: 15 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                 1/1 
  Running scriptlet: rabbitmq-server-3.8.9-1.el7.noarch                                                                                              1/1 
  Installing       : rabbitmq-server-3.8.9-1.el7.noarch                                                                                              1/1 
  Running scriptlet: rabbitmq-server-3.8.9-1.el7.noarch                                                                                              1/1 
[/usr/lib/tmpfiles.d/rabbitmq-server.conf:1] Line references path below legacy directory /var/run/, updating /var/run/rabbitmq → /run/rabbitmq; please update the tmpfiles.d/ drop-in file accordingly.

  Verifying        : rabbitmq-server-3.8.9-1.el7.noarch                                                                                              1/1 

Installed:
  rabbitmq-server-3.8.9-1.el7.noarch                                                                                                                     

Complete!
[root@master rabbitmq]# sudo yum install -y rabbitmq-server
Repository docker-ce-stable is listed more than once in the configuration
Repository docker-ce-stable-source is listed more than once in the configuration
Repository docker-ce-test is listed more than once in the configuration
Repository docker-ce-test-source is listed more than once in the configuration
Last metadata expiration check: 0:09:42 ago on Sat 20 Jan 2024 04:54:31 PM CST.
Package rabbitmq-server-3.8.9-1.el7.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@master rabbitmq]# sudo systemctl start rabbitmq-server
[root@master rabbitmq]# sudo systemctl enable rabbitmq-server
Created symlink /etc/systemd/system/multi-user.target.wants/rabbitmq-server.service → /usr/lib/systemd/system/rabbitmq-server.service.
[root@master rabbitmq]# sudo rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@master:
rabbitmq_management
The following plugins have been configured:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch
Applying plugin configuration to rabbit@master...
The following plugins have been enabled:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch

started 3 plugins.
[root@master rabbitmq]# Welcome to TencentOS 3 64bit
Version 3.1 20230621
tlinux3.1-64bit-5.4.119-19.0009.28-20230621
Last failed login: Sat Jan 20 17:04:15 CST 2024 from 141.98.11.11 on ssh:notty
There were 66 failed login attempts since the last successful login.
Last login: Sat Jan 20 15:14:10 2024 from 114.84.30.36
[root@master ~]# 
[root@master ~]# 
[root@master ~]# sudo systemctl restart rabbitmq-server
[root@master ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 52:54:00:b2:d9:8f brd ff:ff:ff:ff:ff:ff
    inet 10.206.0.4/20 brd 10.206.15.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:feb2:d98f/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:75:e7:8a:55 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
4: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default 
    link/ether fe:76:ea:e2:7d:ac brd ff:ff:ff:ff:ff:ff
    inet 10.244.0.0/32 scope global flannel.1
       valid_lft forever preferred_lft forever
    inet6 fe80::fc76:eaff:fee2:7dac/64 scope link 
       valid_lft forever preferred_lft forever
5: cni0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default qlen 1000
    link/ether 5a:f9:63:ee:40:66 brd ff:ff:ff:ff:ff:ff
    inet 10.244.0.1/24 brd 10.244.0.255 scope global cni0
       valid_lft forever preferred_lft forever
    inet6 fe80::58f9:63ff:feee:4066/64 scope link 
       valid_lft forever preferred_lft forever
6: veth493a584d@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue master cni0 state UP group default 
    link/ether 5e:b3:de:c6:1b:24 brd ff:ff:ff:ff:ff:ff link-netns cni-b91e1415-a9ef-8c20-aa2a-dde1763330ab
    inet6 fe80::5cb3:deff:fec6:1b24/64 scope link 
       valid_lft forever preferred_lft forever
7: vethdf6c578d@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue master cni0 state UP group default 
    link/ether a6:0d:69:a6:2b:23 brd ff:ff:ff:ff:ff:ff link-netns cni-7c1ab72c-05cf-fc96-2973-4eeea0dd0ccb
    inet6 fe80::a40d:69ff:fea6:2b23/64 scope link 
       valid_lft forever preferred_lft forever
[root@master ~]# sudo hostnamectl set-hostname node1
[root@master ~]# sudo hostnamectl set-hostname master
[root@master ~]# scp /var/lib/rabbitmq/.erlang.cookie root@node2:/var/lib/rabbitmq/
The authenticity of host 'node2 (10.206.0.8)' can't be established.
ECDSA key fingerprint is SHA256:jE6hSkM1aT+1q0vmNHqa0JHMMSRj3V8NeJdnyPCI5pQ.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'node2,10.206.0.8' (ECDSA) to the list of known hosts.
root@node2's password: 
.erlang.cookie                                                                                                         100%   20    61.5KB/s   00:00    
[root@master ~]# scp /var/lib/rabbitmq/.erlang.cookie root@node3:/var/lib/rabbitmq/
ssh: Could not resolve hostname node3: Name or service not known
lost connection
[root@master ~]# scp /var/lib/rabbitmq/.erlang.cookie root@node1:/var/lib/rabbitmq/
The authenticity of host 'node1 (10.206.0.13)' can't be established.
ECDSA key fingerprint is SHA256:kCk++fRif8cOYBpWe98waBTWL/L2Wob0SWezO5e5TgE.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'node1' (ECDSA) to the list of known hosts.
root@node1's password: 
.erlang.cookie                                                                                                         100%   20    53.6KB/s   00:00    
[root@master ~]# sudo rabbitmqctl cluster_status
Cluster status of node rabbit@master ...
Basics

Cluster name: rabbit@node1

Disk Nodes

rabbit@master
rabbit@node1
rabbit@node2

Running Nodes

rabbit@master
rabbit@node1
rabbit@node2

Versions

rabbit@master: RabbitMQ 3.8.9 on Erlang 22.0.7
rabbit@node1: RabbitMQ 3.8.9 on Erlang 22.0.7
rabbit@node2: RabbitMQ 3.8.9 on Erlang 22.0.7

Maintenance status

Node: rabbit@master, status: not under maintenance
Node: rabbit@node1, status: not under maintenance
Node: rabbit@node2, status: not under maintenance

Alarms

(none)

Network Partitions

(none)

Listeners

Node: rabbit@master, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@master, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@master, 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@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@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: virtual_host_metadata, state: enabled
[root@master ~]#