部署RabbitMQ集群-disk模式

部署RabbitMQ集群-disk模式

Deng YongJie's blog 364 2022-09-10

debian操作系统部署rabbitmq集群

配置三台主机的hosts文件,因为rabbitmq是通过主机名称识别的,命令如下

cat >> /etc/hosts <<'EOF'
10.6.0.45 crm-ma01-mq-s1
10.6.0.46 crm-ma01-mq-s2
10.6.0.47 crm-ma01-mq-s3
EOF

ping ma05-mq-d1

ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.250

开始分别在三台机器上安装rabbitmq,整体步骤比较简单,按步骤执行如下命令即可

apt autoremove
apt --fix-broken install
mv /var/lib/dpkg/info/ /var/lib/dpkg/info_old/
mkdir /var/lib/dpkg/info/
apt-get update
apt-get -f install
mv /var/lib/dpkg/info/* /var/lib/dpkg/info_old/
rm -rf /var/lib/dpkg/info
mv /var/lib/dpkg/info_old/ /var/lib/dpkg/info/
apt-get update
apt-get upgrade
apt-get --reinstall install `dpkg --get-selections | grep '[[:space:]]install' | cut -f1`   #重装dpkg

wget https://packages.erlang-solutions.com/erlang/debian/pool/esl-erlang_23.1.5-1~debian~stretch_amd64.deb
apt --fix-broken install -y
dpkg -i esl-erlang_23.1.5-1_debian_stretch_amd64.deb
erl -version
erl

wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.15/rabbitmq-server_3.8.15-1_all.deb
dpkg -i rabbitmq-server_3.8.15-1_all.deb
apt --fix-broken install -y

添加配置文件
cat > /etc/rabbitmq/rabbitmq-env.conf <<'EOF'
RABBITMQ_MNESIA_BASE=/usr/local/rabbitmq/data/
RABBITMQ_LOG_BASE=/usr/local/rabbitmq/log
vm_memory_high_watermark=0.8
EOF

[root@ma05-mq-d2 ~]# cat > /etc/rabbitmq/rabbitmq.conf <<EOF
vm_memory_high_watermark.relative = 0.8
vm_memory_high_watermark_paging_ratio = 0.5
cluster_partition_handling = pause_minority

log.file.level = error
log.default.level = error

default_user = xxxxx
default_pass = xxxxx2022

tcp_listen_options.backlog = 4096
tcp_listen_options.nodelay = true
tcp_listen_options.linger.on = true
tcp_listen_options.linger.timeout = 0
tcp_listen_options.exit_on_close = false
tcp_listen_options.keepalive = true
tcp_listen_options.send_timeout = 15000
EOF


mkdir /usr/local/rabbitmq/{log,data} -p
chown -R rabbitmq.rabbitmq /usr/local/rabbitmq

#三台都安装远程访问插件,否则集群后看不到状态情况
rabbitmq-plugins enable rabbitmq_management
netstat -lntup

#三台设置开机启动
systemctl enable rabbitmq-server.service

#启动s1服务器的,其它两台暂不启动
systemctl start rabbitmq-server

#如下命令在s1执行,其它节点不需要执行
rabbitmqctl add_user admin xxxxxPBUN3
rabbitmqctl set_user_tags admin administrator
Setting tags for user "admin" to [administrator] ...

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

#创建用户,授权,允许远程登录
rabbitmqctl add_user xxxxx xxxxx2022
rabbitmqctl  set_permissions -p /  xxxxx '.*' '.*' '.*'
rabbitmqctl list_permissions -p /
rabbitmqctl set_user_tags xxxxx administrator

至此,已经在三台服务器成功安装了rabbitmq,并且可以通过远端web进行访问管理了

开始构建集群环境

#从s1服务器上复制.erlang.cookie文件到s2,s3
scp /var/lib/rabbitmq/.erlang.cookie crm-ma01-mq-s2:/var/lib/rabbitmq/
scp /var/lib/rabbitmq/.erlang.cookie ma01-mq-d3:/var/lib/rabbitmq/


#在三台服务器上,分别设置.erlang.cookie的权限
chown rabbitmq:root /var/lib/rabbitmq/.erlang.cookie
chmod 600 /var/lib/rabbitmq/.erlang.cookie

#这时再分别启动s2,s3服务
systemctl start rabbitmq-server

开始编组,分别登陆s2,s3服务器,执行如下命令,将s2,s3编组到s1中

#必须是运行状态下执行
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@crm-ma01-mq-s1    #s1,默认是disk节点,加参数 --ram代表是内存节点,建议使用磁盘模式,确保不丢数据,使用SSD硬盘并且扩大容量。

rabbitmqctl start_app
rabbitmqctl join_cluster --ram rabbit@ma05-mq-d1  

#2台节点加入主节点后,在主节点查看集群状态
root@ma01-mq-d1:~# rabbitmqctl cluster_status
Cluster status of node rabbit@ma01-mq-d1 ...
[{nodes,[{disc,['rabbit@ma01-mq-d1','rabbit@ma01-mq-d2',
                'rabbit@ma01-mq-d3']}]},
 {running_nodes,['rabbit@ma01-mq-d1']},
 {cluster_name,<<"rabbit@ma01-mq-d1">>},
 {partitions,[]},
 {alarms,[{'rabbit@ma01-mq-d1',[]}]}]


成功后,启动从节点
rabbitmqctl start_app

登录web页面端口号是:15672