此前,简单的叙述了rabbitmq(瞎扯)的单机使用,本篇记录了rabbitmq集群的简单使用。其他参考:activemq+zookeeper集群配置笔记准备环境:CentOS Linux release 7.5.1804 (Core) erlang-21.1.4-1.el7.centos.x86_64rabbitmq-server-3.7.9-1.el7HA-Proxy version 1.6.9 2(docker)
大致情况如下:
先决条件
-
编辑hosts文件
[root@LinuxEA-172_25_12_16 ~]# cat /etc/hosts 172.25.12.16 rabbitmq-1 172.25.12.17 rabbitmq-2 172.25.12.18 rabbitmq-3
而后重启网卡
systemctl restart network
如果有必要,修改主机名。否则每次登陆可能无法正常使用
hostnamectl set-hostname rabbitmq-1 hostnamectl set-hostname rabbitmq-2 hostnamectl set-hostname rabbitmq-3
- 安装
在页面https://packagecloud.io/rabbitmq/erlang/packages/el/7/erlang-21.1.4-1.el7.centos.x86_64.rpm
根据提示,使用如下安装:
curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bash
sudo yum install erlang-21.1.4-1.el7.centos.x86_64
在页面https://packagecloud.io/rabbitmq/rabbitmq-server/packages/el/7/rabbitmq-server-3.7.9-1.el7.noarch.rpm
根据提示,使用如下安装
curl -s https://packagecloud.io/install/repositories/rabbitmq/rabbitmq-server/script.rpm.sh | sudo bash
sudo yum install rabbitmq-server-3.7.9-1.el7.noarch
其他版本仓库:http://www.rabbitmq.com/releases/rabbitmq-server/
- 配置互通
配置互通主要用于传递文件,和集群没毛关系
[root@LinuxEA-172_25_12_16 ~]# ssh-keygen -t rsa
[root@LinuxEA-172_25_12_16 ~]# ssh-copy-id 172.25.12.17
[root@LinuxEA-172_25_12_16 ~]# ssh-copy-id 172.25.12.18
- 防火墙
-A INPUT -s 172.25.12.16/32 -p tcp -m tcp -m state --state NEW -m multiport --dports 4369,5671:5673,15672:15673,25672 -m comment --comment "RabbitMQ_cluster-1" -j ACCEPT
-A INPUT -s 172.25.12.17/32 -p tcp -m tcp -m state --state NEW -m multiport --dports 4369,5671:5673,15672:15673,25672 -m comment --comment "RabbitMQ_cluster-2" -j ACCEPT
-A INPUT -s 172.25.12.18/32 -p tcp -m tcp -m state --state NEW -m multiport --dports 4369,5671:5673,15672:15673,25672 -m comment --comment "RabbitMQ_cluster-3" -j ACCEPT
[root@LinuxEA--172_25_12_16 ~]# iptables -L -nv|grep RabbitMQ
2158 110K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp state NEW multiport dports 4369,5671:5673,15672:15673,25672 /* RabbitMQ_cluster-1 */
0 0 ACCEPT tcp -- * * 172.25.12.16 0.0.0.0/0 tcp state NEW multiport dports 4369,5671:5673,15672:15673,25672 /* RabbitMQ_cluster-1 */
0 0 ACCEPT tcp -- * * 172.25.12.17 0.0.0.0/0 tcp state NEW multiport dports 4369,5671:5673,15672:15673,25672 /* RabbitMQ_cluster-2 */
0 0 ACCEPT tcp -- * * 172.25.12.18 0.0.0.0/0 tcp state NEW multiport dports 4369,5671:5673,15672:15673,25672 /* RabbitMQ_cluster-3 */
我假设你关闭了selinux,如果没有,请关闭。
统一认证配置
将本地的/var/lib/rabbitmq/.erlang.cookie
复制到另外两台节点相同目录下
[root@LinuxEA-172_25_12_16 ~]# scp -P22992 /var/lib/rabbitmq/.erlang.cookie root@172.25.12.17:/var/lib/rabbitmq/
.erlang.cookie 100% 20 16.3KB/s 00:00
[root@LinuxEA-172_25_12_16 ~]# scp -P22992 /var/lib/rabbitmq/.erlang.cookie root@172.25.12.18:/var/lib/rabbitmq/
.erlang.cookie 100% 20 16.5KB/s 00:00
如果有必要,可以修改下权限
[root@LinuxEA-172_25_12_16 ~]# ssh -p22992 root@172.25.12.17 'chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie'
[root@LinuxEA-172_25_12_16 ~]# ssh -p22992 root@172.25.12.18 'chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie'
[root@LinuxEA-172_25_12_16 ~]# ssh -p22992 root@172.25.12.17 'chmod 400 /var/lib/rabbitmq/.erlang.cookie '
[root@LinuxEA-172_25_12_16 ~]# ssh -p22992 root@172.25.12.18 'chmod 400 /var/lib/rabbitmq/.erlang.cookie '
而后关闭掉所有节点,重新启动另外两台rabbitmq节点
[root@LinuxEA-172_25_12_16 ~]# rabbitmq-server -detached
Warning: PID file not written; -detached was passed.
[root@LinuxEA-172_25_12_17 ~]# rabbitmq-server -detached
Warning: PID file not written; -detached was passed.
[root@LinuxEA-172_25_12_18 ~]# rabbitmq-server -detached
Warning: PID file not written; -detached was passed.
加入集群
将172.25.12.17加入到172.25.12.16集群中
[root@LinuxEA-172_25_12_17 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq-2 ...
[root@LinuxEA-172_25_12_17 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1
Clustering node rabbit@rabbitmq-2 with rabbit@rabbitmq-1
[root@LinuxEA-172_25_12_17 ~]# rabbitmqctl start_app
Starting node rabbit@rabbitmq-2 ...
completed with 4 plugins.
[root@LinuxEA-172_25_12_17 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq-2 ...
[{nodes,[{disc,['rabbit@rabbitmq-1']},{ram,['rabbit@rabbitmq-2']}]},
{running_nodes,['rabbit@rabbitmq-1','rabbit@rabbitmq-2']},
{cluster_name,},
{partitions,[]},
{alarms,[{'rabbit@rabbitmq-1',[]},{'rabbit@rabbitmq-2',[]}]}]
将172.25.12.18加入到172.25.12.16集群中
[root@LinuxEA-172_25_12_18 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq-3 ...
[root@LinuxEA-172_25_12_18 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1
Clustering node rabbit@rabbitmq-3 with rabbit@rabbitmq-1
[root@LinuxEA-172_25_12_18 ~]# rabbitmqctl start_app
Starting node rabbit@rabbitmq-3 ...
completed with 4 plugins.
[root@LinuxEA-172_25_12_18 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq-3 ...
[{nodes,[{disc,['rabbit@rabbitmq-1']},
{ram,['rabbit@rabbitmq-3','rabbit@rabbitmq-2']}]},
{running_nodes,['rabbit@rabbitmq-2','rabbit@rabbitmq-1','rabbit@rabbitmq-3']},
{cluster_name,},
{partitions,[]},
{alarms,[{'rabbit@rabbitmq-2',[]},
{'rabbit@rabbitmq-1',[]},
{'rabbit@rabbitmq-3',[]}]}]
集群状态:
[root@LinuxEA-172_25_12_16 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq-1 ...
[{nodes,[{disc,['rabbit@rabbitmq-1']},
{ram,['rabbit@rabbitmq-3','rabbit@rabbitmq-2']}]},
{running_nodes,['rabbit@rabbitmq-3','rabbit@rabbitmq-2','rabbit@rabbitmq-1']},
{cluster_name,},
{partitions,[]},
{alarms,[{'rabbit@rabbitmq-3',[]},
{'rabbit@rabbitmq-2',[]},
{'rabbit@rabbitmq-1',[]}]}]
镜像
高可用集群大概分为2种,单一的不算。就剩下默认的集群模式和镜像模式。镜像模式可以完善普通集群的弊端,所有消息实体会主动在镜像节点同步,而不是单单的临时拉取。但这样的方式对性能有一定的损耗,但是胜在可靠性高。
[root@LinuxEA-172_25_12_16 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
Setting policy "ha-all" for pattern "^" to "{"ha-mode":"all"}" with priority "0" for vhost "/" ...
配置用户
添加用户
[root@LinuxEA-172_25_12_16 ~]# rabbitmqctl add_user admin admin
set管理员状态权限
[root@LinuxEA-172_25_12_16 ~]# rabbitmqctl set_user_tags admin administrator
授权读写所有权限
[root@LinuxEA-172_25_12_16 ~]# rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
Setting permissions for user "admin" in vhost "/" ...
WEB管理界面插件安装
[root@LinuxEA-172_25_12_16 ~]# rabbitmq-plugins enable rabbitmq_management
创建一个host
[root@LinuxEA-172_25_12_17 ~]# rabbitmqctl add_vhost test
Adding vhost "test" ...
将test所有队列标记为镜像队列,即队列会被复制到各个节点,各个节点状态保持一致
[root@LinuxEA-172_25_12_17 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}' -p test
Setting policy "ha-all" for pattern "^" to "{"ha-mode":"all"}" with priority "0" for vhost "test" ...
haproxy
global
log 127.0.0.1 local3
log 127.0.0.1 local1 notice
maxconn 5000
pidfile /run/haproxy.pid
defaults
log global
mode tcp
option tcplog
option dontlognull
retries 3
option redispatch #在使用基于cookie定向时,一旦后端某一server宕机时,会将会话重新定向至某一上游服务器,必须使用 的选项
maxconn 4000 #每个server最大的连接数
timeout http-request 10s #在客户端建立连接但不请求数据时,关闭客户端连接
timeout queue 1m #等待最大时长
timeout connect 10s #定义haproxy将客户端请求转发至后端服务器所等待的超时时长
timeout client 1m #客户端非活动状态的超时时长
timeout server 1m #客户端与服务器端建立连接后,等待服务器端的超时时长
timeout http-keep-alive 10s #定义保持连接的超时时长
timeout check 10s #健康状态监测时的超时时间,过短会误判,过长资源消耗
listen stats
bind :9000
mode http
stats enable
stats uri /haproxy_stats
stats realm HAProxy\ Statistics #统计页面密码框上提示文本,默认为Haproxy\ Statistics
stats auth admin:ZTVmNTlhYzczOGM0 #统计页面用户名和密码设置
stats admin if TRUE #如果认证通过就做管理功能,可以管理后端的服务器
stats hide-version #隐藏统计页面上HAProxy的版本信息
stats refresh 30s #统计页面自动刷新时间
frontend rabbitmq
bind *:5672
mode tcp
log global
default_backend rabbitmq
backend rabbitmq
balance roundrobin
server rabbitmq1-slave 172.25.12.18:5672 check port 5672 rise 1 fall 2 maxconn 4000 weight 1 # mem data
server rabbitmq2-slave 172.25.12.17:5672 check port 5672 rise 1 fall 2 maxconn 4000 weight 1 # mem data
#server rabbitmq3-master 172.25.12.16:5672 check port 5672 rise 1 fall 2 maxconn 4000 weight 1 #disk data
172.25.12.16在rabbitmqctl cluster_status查看时候是disc节点,就不做为读写了,之作为存储盘,而其他两个内存节点就作为读写
[root@DT_Node-172_25_12_16 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq-1 ...
[{nodes,[{disc,['rabbit@rabbitmq-1']},
{ram,['rabbit@rabbitmq-3','rabbit@rabbitmq-2']}]},
{running_nodes,['rabbit@rabbitmq-3','rabbit@rabbitmq-2','rabbit@rabbitmq-1']},
{cluster_name,},
{partitions,[]},
{alarms,[{'rabbit@rabbitmq-3',[]},
{'rabbit@rabbitmq-2',[]},
{'rabbit@rabbitmq-1',[]}]}]