CentOS7 下安装 RabbitMQ

本文介绍了在 CentOS7 系统下安装 RabbitMQ 的详细步骤。首先需要安装 Erlang 和 RabbitMQ,然后进行 RabbitMQ 的配置,包括设置防火墙和 SELinux。接着介绍了如何启用 RabbitMQ Web 控制台以及如何配置 SSL 证书。最后,文章提供了设置 RabbitMQ 集群的脚本 /usr/local/sbin/rabbitmq-cluster.sh,供读者参考。

CentOS7 下安装 RabbitMQ

1
2
sudo yum -y install epel-release
sudo yum -y update

安装 Erlang

下载 erlang repository:

1
wget http://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm

添加 erlang repository:

1
sudo rpm -Uvh erlang-solutions-1.0-1.noarch.rpm

安装 erlang 及其依赖:

1
sudo yum -y install erlang socat logrotate

安装 RabbitMQ

下载 RabbitMQ rpm 包:

1
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.8/rabbitmq-server-3.8.8-1.el6.noarch.rpm

添加 signing key:

1
sudo rpm --import https://www.rabbitmq.com/rabbitmq-signing-key-public.asc

安装 rabbitmq-server:

1
sudo rpm -Uvh rabbitmq-server-3.8.8-1.el6.noarch.rpm

启动 RabbitMQ:

1
sudo systemctl start rabbitmq-server

设置 RabbitMQ 开机自启:

1
sudo systemctl enable rabbitmq-server

RabbitMQ 配置(可选)

创建 rabbitmq 配置文件 /etc/rabbitmq/rabbitmq.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
listeners.ssl.default = 5671

ssl_options.cacertfile = /path/to/cacertfile.pem
ssl_options.certfile = /path/to/certfile.pem
ssl_options.keyfile = /path/to/keyfile.pem
ssl_options.verify = verify_peer
ssl_options.versions.1 = tlsv1.2
ssl_options.versions.2 = tlsv1.1
ssl_options.fail_if_no_peer_cert = false

tcp_listen_options.backlog = 128
tcp_listen_options.nodelay = true
tcp_listen_options.exit_on_close = false
tcp_listen_options.keepalive = false

heartbeat = 580

设置防火墙

设置防火墙规则,放通相关端口:

1
2
3
4
5
6
7
sudo firewall-cmd --zone=public --permanent --add-port=4369/tcp
sudo firewall-cmd --zone=public --permanent --add-port=25672/tcp
sudo firewall-cmd --zone=public --permanent --add-port=5671-5672/tcp
sudo firewall-cmd --zone=public --permanent --add-port=15672/tcp
sudo firewall-cmd --zone=public --permanent --add-port=61613-61614/tcp
sudo firewall-cmd --zone=public --permanent --add-port=1883/tcp
sudo firewall-cmd --zone=public --permanent --add-port=8883/tcp

重载防火墙,使规则生效:

1
sudo firewall-cmd --reload

SELinux

如果 SELinux 是 enabled,则启用 NIS:

1
sudo setsebool -P nis_enabled 1

-P 参数表示将设置永久生效,nis_enabled 是一个 SELinux 布尔类型的变量,用于控制是否启用 NIS(Network Information Service)服务。将其设置为 1 表示启用 NIS 服务。

RabbitMQ Web 控制台

启用 RabbitMQ web 控制台:

1
sudo rabbitmq-plugins enable rabbitmq_management

修改文件权限:

1
sudo chown -R rabbitmq:rabbitmq /var/lib/rabbitmq/

创建一个 admin 用户(将 password 替换为一个强密码):

1
sudo rabbitmqctl add_user admin password

给 admin 用户设置 administrator 标签:

1
sudo rabbitmqctl set_user_tags admin administrator

设置 admin 用户权限:

1
sudo rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"

使用 admin 用户访问 RabbitMQ web 控制台:

http://Your_Server_IP:15672

RabbitMQ Web 控制台配置 SSL 证书(可选)

编辑 RabbitMQ 配置文件 /etc/rabbitmq/rabbitmq.conf

1
2
3
4
5
6
management.listener.port = 15672
management.listener.ssl = true

management.listener.ssl_opts.cacertfile = /path/to/cacertfile.pem
management.listener.ssl_opts.certfile = /path/to/certfile.pem
management.listener.ssl_opts.keyfile = /path/to/keyfile.pem

RabbitMQ 集群

设置 RabbitMQ 集群, 拷贝下面的脚本 /usr/local/sbin/rabbitmq-cluster.sh 并运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
set -e

function getHostname()
{
local HOST=''

while test -z "$HOST"
do
read -p "$1 : " HOST
done

echo $HOST;
}

SETUP_MASTER_SCRIPT='
rabbitmqctl stop_app;
rabbitmqctl reset;
rabbitmqctl start_app;
';

# Step 1 : Setup the Master. Get the erlang cookie

echo "Setup RabbitMQ Master";
echo "=====================";

OUT=/tmp/master.out
MASTER_HOSTNAME=$(getHostname "Enter the master server's hostname");
echo "[$MASTER_HOSTNAME] Setting up master";
ssh -t $MASTER_HOSTNAME "bash -c '$SETUP_MASTER_SCRIPT cat /var/lib/rabbitmq/.erlang.cookie;'" | tee $OUT;
COOKIE=$(cat $OUT | tail -n1)
rm $OUT;
echo "Master's Erlang Cookie : '$COOKIE'"

MASTER_IP=$(getHostname "Enter the master server's IP as seen from the slaves (Use a local IP if available)");


# Step 2 : Setup the slaves

SETUP_SLAVE_SCRIPT="
sed -i \"s/^$/$MASTER_IP $MASTER_HOSTNAME\n/\" /etc/hosts
bash -c \"echo -n '$COOKIE' > /var/lib/rabbitmq/.erlang.cookie\";
rabbitmqctl stop_app;
rabbitmqctl reset;
rabbitmqctl join_cluster --ram rabbit@$MASTER_HOSTNAME;
rabbitmqctl start_app;
rabbitmqctl cluster_status;
";

echo "Setup RabbitMQ Slaves";
echo "=====================";

SERVER=$(getHostname "Enter slave's hostname or 'q' to quit");
while test "$SERVER" != "q"
do
echo "Setting up slave";
echo "ssh '$SERVER'";
ssh -t $SERVER "bash -c '$SETUP_SLAVE_SCRIPT'";
SERVER=$(getHostname "Enter another slave's hostname or 'q' to quit");
done

# Step 3 : Create admin user
echo "[$MASTER_HOSTNAME] Setting up admin user";
ssh -t $MASTER_HOSTNAME "bash -c 'rabbitmqctl add_user admin password'";
ssh -t $MASTER_HOSTNAME "bash -c 'rabbitmqctl set_user_tags admin administrator'";
ssh -t $MASTER_HOSTNAME "bash -c 'rabbitmqctl set_permissions -p / admin \".*\" \".*\" \".*\"'";

# Step 3 : Delete guest user
echo "[$MASTER_HOSTNAME] Removing user";
ssh -t $MASTER_HOSTNAME "bash -c 'rabbitmqctl delete_user guest'";

# Step 5 : Create sync policy
echo "[$MASTER_HOSTNAME] Synchronizing cluster";
ssh -t $MASTER_HOSTNAME $"bash -c 'rabbitmqctl set_policy -p / ha-all \"\" '\''{\"ha-mode\":\"all\",\"ha-sync-mode\":\"automatic\"}'\'''";

echo "Done";

常见问题

1. RabbitMQ 异常退出无法重启

问题描述:

机房突然停电,rabbitmq 的主机异常断电,集群服务全部需要重启。但是在执行 systemctl start rabbitmq-server 启动主节点服务的时候,没有反应,服务没有启动,命令执行卡住。只能 Ctrl+C 结束进程。

查看 /var/log/rabbitmq/rabbit@hostname.log 发现有如下报错信息:

1
2
3
4
5
2021-04-13 20:15:00.011 [info] <0.317.0> Waiting for Mnesia tables for 30000 ms, 9 retries left
2021-04-13 20:15:00.011 [warning] <0.317.0> Error while waiting for Mnesia tables: {failed_waiting_for_tables,{node_not_running,rabbit@node233}}
...
2021-04-13 20:15:00.012 [info] <0.317.0> Waiting for Mnesia tables for 30000 ms, 0 retries left
2021-04-13 20:15:00.013 [error] <0.316.0> CRASH REPORT Process <0.316.0> with 0 neighbours exited with reason: {{failed_waiting_for_tables,{node_not_running,rabbit@node233}},{rabbit,start,[normal,[]]}} in application_master:init/4 line 138

解决方法:

  1. 检查端口是否被占用;
  2. 检查 /var/log/rabbitmq 目录权限;
  3. 分布式数据库 mnesia 异常,将 /var/lib/rabbitmq/mnesia/rabbit@hostname/ 下的数据库文件清空即可,重新启动服务;或直接清除 /var/lib/rabbitmq/mnesia 目录下所有文件;

References

Install RabbitMQ on CentOS 7