如何在centos7下部署RabbitMQ集群环境?
痛点:
整了3天,重装3台系统5次,整个过程一次成功,这一次可能是关闭了防火墙,后续所有的都连接不上但能ping通,所以,同志们,注意了,防火防盗防火墙!
必备工具安装:
yum -y install vim wget ntp lrzsz
一、准备3台机器,配置/etc/hosts内容都为如下
192.168.12.106 rabbit1
192.168.12.107 rabbit2
192.168.12.108 rabbit3
vim /etc/hosts 添加(注意中间空格为tab符号)
192.168.12.106 rabbit1
192.168.12.107 rabbit2
192.168.12.108 rabbit3
然后拷贝到其他机器:
scp /etc/hosts [email protected]:/etc/hosts
scp /etc/hosts [email protected]:/etc/hosts
设置本地时区(同步3台机器时间)
如果时间不与网络时间同步,可以通过ntp服务同步网络时间:
# yum install -y ntp
# ntpdate cn.pool.ntp.org
查看同步后的时间:date
时间查了12小时,由于默认的时区不是东八区,也就是不是上海时间,我们必须设置为上海时间
查看本地时间:date 发现时间不正确,时区不对
修改本地时间:cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime (注意localtime为文件)
查看本机时间:date 发现本机时间已经修正
二、相关软件安装
1、erlang安装
首先安装erlang语言环境,需要安装C++编译:
yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel unixODBC unixODBC-devel httpd python-simplejson
(1)下载erlang
下载地址: http://www.rabbitmq.com/releases/erlang/ 我这里下载的是esl-erlang_20.0-1-centos-7_amd64.rpm
(2)将rpm包放在/opt目录下
(3)执行安装命令
rpm -ivh esl-erlang_20.0-1-centos-7_amd64.rpm
安装错误提示:
error: Failed dependencies:
libGL.so.1()(64bit) is needed by esl-erlang-19.3-1.x86_64
libGLU.so.1()(64bit) is needed by esl-erlang-19.3-1.x86_64
libodbc.so.2()(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_baseu-2.8.so.0()(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_baseu-2.8.so.0(WXU_2.8)(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_baseu_xml-2.8.so.0()(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_adv-2.8.so.0()(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_adv-2.8.so.0(WXU_2.8)(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_aui-2.8.so.0()(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_aui-2.8.so.0(WXU_2.8)(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_aui-2.8.so.0(WXU_2.8.5)(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_core-2.8.so.0()(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_core-2.8.so.0(WXU_2.8)(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_core-2.8.so.0(WXU_2.8.10)(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_gl-2.8.so.0()(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_gl-2.8.so.0(WXU_2.8)(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_html-2.8.so.0()(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_html-2.8.so.0(WXU_2.8)(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_stc-2.8.so.0()(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_stc-2.8.so.0(WXU_2.8)(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_xrc-2.8.so.0()(64bit) is needed by esl-erlang-19.3-1.x86_64
libwx_gtk2u_xrc-2.8.so.0(WXU_2.8)(64bit) is needed by esl-erlang-19.3-1.x86_64
解决方法:手动安装依赖包:(必须按顺序安装如下依赖包)
yum install epel-release
yum install unixODBC unixODBC-devel wxBase wxGTK SDL wxGTK-gl
然后在执行:rpm -ivh esl-erlang_20.0-1-centos-7_amd64.rpm 这时候就可以顺利安装erlang包了
2、RabbitMQ安装
(1) 下载地址https://dl.bintray.com/rabbitmq/rabbitmq-server-rpm/ 我这里选择的版本:centos_7_rabbitmq-server-3.6.14-1.el7.noarch.rpm
可能需要到rabbit下载连接找
(2)安装rpm包:rpm -ivh centos_7_rabbitmq-server-3.6.14-1.el7.noarch.rpm
安装过程报错如下:
error: Failed dependencies:
erlang >= R16B-03 is needed by rabbitmq-server-3.6.14-1.el7.noarch
socat is needed by rabbitmq-server-3.6.14-1.el7.noarch
安装依赖包:socat : yum -y install socat
此时会报错没有socat包或是找不到socat包,解决方法安装centos的epel的扩展源:yum -y install epel-release
继续安装包:rpm -ivh centos_7_rabbitmq-server-3.6.14-1.el7.noarch.rpm 依旧报错依赖包:erlang >= R16B-03 is needed by
选择忽略依赖包:rpm -i --nodeps centos_7_rabbitmq-server-3.6.14-1.el7.noarch.rpm
(3)生成配置文件
cp /usr/share/doc/rabbitmq-server-3.6.14/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
(4)启动RabbitMQ
systemctl start rabbitmq-server 报错 line 80: erl: command not found错误,说明安装erlang不正确,重新安装,如果已经安装需要手动设置erlang环境变量。
(5)查看启动状态
systemctl status rabbitmq-server
3、管理插件安装
查看插件:rabbitmq-plugins list
web插件安装:rabbitmq-plugins enable rabbitmq_management
日志跟踪插件安装:
rabbitmq-plugins enable rabbitmq_tracing #rabbitmq启用trace插件
rabbitmqctl trace_on #打开trace的开关
rabbitmqctl trace_on -p test #打开trace的开关(test为需要日志追踪的vhost)
rabbitmqctl trace_off #关闭trace的开关
安装(延时队列): rabbitmq_delayed_message_exchange
(1) cd /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.14/plugins/
(2) wget https://dl.bintray.com/rabbitmq/community-plugins/rabbitmq_delayed_message_exchange-0.0.1.ez
(3) rabbitmq-plugins enable rabbitmq_delayed_message_exchange
此时可以通过Ip登录到(这时候只有本机可以登录,需要开发远程登录权限后才能在远程登录)配置的这台RabbitMQ上了:http://192.168.12.106:15672/ ,默认用户名和密码都为guest
常用启动命令:
service rabbitmq-server start
service rabbitmq-server stop
service rabbitmq-server status
service rabbitmq-server rotate-logs|
service rabbitmq-server restart
service rabbitmq-server condrestart
service rabbitmq-server try-restart
service rabbitmq-server reload
service rabbitmq-server force-reload
查看rabbitMq进程:ps -ef | grep rabbitmq
默认监听端口15672/5672:netstat -anplt | grep LISTEN rabbitmq
4、集群服务配置
(1)说明
Rabbitmq的集群是依附于erlang的集群来工作的,所以必须先构建起erlang的集群镜像。Erlang的集群中各节点是经由过程一个magic cookie来实现的,这个cookie存放在 $home/.erlang.cookie 中,我的是用rpm安装的,所以.erlang.cookie就放在/var/lib/rabbitmq中。如果我们使用解压缩方式安装部署的rabbitmq,那么这个文件会在${home}目录下.
(2)复制cookie内容
先停止所有3台机器服务器:service rabbitmq-server stop
erlang.cookie是erlang实现分布式的必要文件,erlang分布式的每个节点上要保持相同的.erlang.cookie文件,同时保证文件的权限是400,不然节点之间就无法通信。打开文件然后需要先把其中的一台服务器的.erlang.cookie中的内容复制到别的机器上,最好是复制内容,因为文件权限不对的话会出现问题,在最后退出保存的时候使用wq!用!来进行强制保存即可。
也可是直接使用scp传过去,记得文件权限和用户属主属组如scp .erlang.cookie [email protected]:/tmp
偷个懒:
cd /var/lib/rabbitmq
修改rabbit2、rabbit3的机器的文件权限:
chmod 777 /var/lib/rabbitmq/.erlang.cookie,
然后从rabbit1拷贝
scp .erlang.cookie [email protected]:/var/lib/rabbitmq/
scp .erlang.cookie [email protected]:/var/lib/rabbitmq/
拷贝完后修改rabbit2、rabbit3回原来的权限:
#chmod 400 /var/lib/rabbitmq/.erlang.cookie #启动前必须设置400,只有owner拥有权限即可,否则启动rabbitmq服务失败
设置好cookie后先将三个节点的rabbitmq重启
# rabbitmqctl stop (如果已经在某些组件启动情况下必须先停止然后在启动服务)
# systemctl start rabbitmq-server
# systemctl status rabbitmq-server
关闭防火墙(否则提示连接不上):
systemctl stop firewalld
systemctl disable firewalld
(3)重启各个节点服务
重启节点:
rabbit1# rabbitmqctl stop
rabbit2# rabbitmqctl stop
rabbit3# rabbitmqctl stop
然后使用detached参数独立运行,这步很关键,尤其增加节点停止节点后再次启动遇到无法启动都可以参照这个顺序:
rabbit1# rabbitmq-server -detached
rabbit2# rabbitmq-server -detached
rabbit3# rabbitmq-server -detached
查看每个节点集群状态:
rabbitmqctl cluster_status
rabbit1节点(可以看到节点名且为硬盘节点):
Cluster status of node rabbit@rabbit1
[{nodes,[{disc,[rabbit@rabbit1]}]},
{running_nodes,[rabbit@rabbit1]},
{cluster_name,<<"rabbit@rabbit1">>},
{partitions,[]},
{alarms,[{rabbit@rabbit1,[]}]}]
rabbit2节点(可以看到节点名且为硬盘节点):
Cluster status of node rabbit@rabbit2
[{nodes,[{disc,[rabbit@rabbit2]}]},
{running_nodes,[rabbit@rabbit2]},
{cluster_name,<<"rabbit@rabbit2">>},
{partitions,[]},
{alarms,[{rabbit@rabbit2,[]}]}]
rabbit3节点(可以看到节点名且为硬盘节点):
Cluster status of node rabbit@rabbit3
[{nodes,[{disc,[rabbit@rabbit3]}]},
{running_nodes,[rabbit@rabbit3]},
{cluster_name,<<"rabbit@rabbit3">>},
{partitions,[]},
{alarms,[{rabbit@rabbit3,[]}]}]
查看系统运行状态:rabbitmqctl status
(4)添加集群节点
说明:
rabbitmq-server启动时,会一起启动节点和应用,它预先设置RabbitMQ应用为standalone模式。要将一个节点加入到现有的集群中,你需要停止这个应用,并将节点设置为原始状态。如果使用rabbitmqctl stop,应用和节点都将被关闭。所以使用rabbitmqctl stop_app仅仅关闭应用。
初始化rabbit执行队列
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
将rabbit1作为集群主节点,rabbit2和rabbit3加入集群,所以在rabbit2和rabbit3节点上执行如下命令,加入集群节点:
rabbit2节点执行命令:
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbit1
rabbitmqctl start_app
其中–ram指的是作为内存节点,要是想做为磁盘节点的话,就不用加–ram这个参数了,把第3行写成这样就好了
PS:只要在节点列表里包含了本身,它就成为一个磁盘节点。在RabbitMQ集群里,必须至少有一个磁盘节点存在。
rabbit3节点执行命令:
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@rabbit1
rabbitmqctl start_app
查看集群节点状态:
rabbitmqctl cluster_status
rabbit1集群状态:
Cluster status of node rabbit@rabbit1
[{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit3]},{ram,[rabbit@rabbit2]}]},
{running_nodes,[rabbit@rabbit2,rabbit@rabbit3,rabbit@rabbit1]},
{cluster_name,<<"rabbit@rabbit1">>},
{partitions,[]},
{alarms,[{rabbit@rabbit2,[nodedown]},
{rabbit@rabbit3,[]},
{rabbit@rabbit1,[]}]}]
rabbit2集群状态:
Cluster status of node rabbit@rabbit2
[{nodes,[{disc,[rabbit@rabbit3,rabbit@rabbit1]},{ram,[rabbit@rabbit2]}]},
{running_nodes,[rabbit@rabbit1,rabbit@rabbit3,rabbit@rabbit2]},
{cluster_name,<<"rabbit@rabbit1">>},
{partitions,[]},
{alarms,[{rabbit@rabbit1,[]},{rabbit@rabbit3,[]},{rabbit@rabbit2,[]}]}]
rabbit3集群状态:
Cluster status of node rabbit@rabbit3
[{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit3]},{ram,[rabbit@rabbit2]}]},
{running_nodes,[rabbit@rabbit2,rabbit@rabbit1,rabbit@rabbit3]},
{cluster_name,<<"rabbit@rabbit1">>},
{partitions,[]},
{alarms,[{rabbit@rabbit2,[nodedown]},
{rabbit@rabbit1,[]},
{rabbit@rabbit3,[]}]}]
注意:
(1)如果配置多次,可能导致如下错误
Error: {inconsistent_cluster,"Node rabbit@rabbit1 thinks it's clustered with node rabbit@rabbit3, but rabbit@rabbit3 disagrees"}
原因:mnesia的信息残留,故会认证失败,其目录为/var/lib/rabbitmq/mnesia
解决方法:
在需要加入到某集群环境的节点下执行:mv /var/lib/rabbitmq/mnesia /tmp
查看状态:
systemctl status rabbitmq-server
(2)启动错误:Error: Failed to initialize erlang distribution: {{shutdown
原因:频繁修改配置文件或名字导致的
解决:
删除重装:find / name "*rabbitmq*" -exec rm -rf {}
卸载: systemctl stop rabbitmq-server #停止服务
rpm -qa | grep -i rabbit #查看先关的安装包
rpm -e rabbitmq-server-3.6.14-1.el7.noarch --nodeps #卸载安装包
rpm -qa | grep -i rabbit #查看确认是否卸载
5、添加账号
由于guest这个用户,只能在本地访问,所以我们要新增一个用户并赋予权限
添加管理员账号(用户名密码都为admin):
rabbitmqctl add_user admin admin
添加权限(使admin用户对虚拟主机“/” 具有所有权限):
rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
分配用户标签(管理员组):
rabbitmqctl set_user_tags admin administrator
查看列举用户列表:
rabbitmqctl list_users
配置之后,然后就可以远程访问了,然后可直接配置用户权限等信息,到此,就可以通过http://ip:15672 使用admin 123456 进行登陆了。
到这里的话,每个节点是作为单独的一台RabbitMQ存在的,也可以正常提供服务了。
如果有需要修改密码操作如下(后两个参数为用户名和密码):
rabbitmqctl change_password Username Newpassword
此时就可以使用账号远程登录了:http://192.168.12.106:15672/#/
注意:这里添加用户后,登录任意一台集群环境web端都可以进去,也就是用户是针对所有集群机器的
6、查看启动日志
日志:/var/log/rabbitmq/[email protected]
/var/log/rabbitmq/[email protected]
启动目录详情(了解后可以通过日志目录查看日志,数目目录了解数据存储位置):
=INFO REPORT==== 26-Jul-2018::14:21:46 ===
node : rabbit@rabbit2
home dir : /var/lib/rabbitmq
config file(s) : /etc/rabbitmq/rabbitmq.config
cookie hash : tTjaA/Ix81C40esIy0pcwg==
log : /var/log/rabbitmq/[email protected]
sasl log : /var/log/rabbitmq/[email protected]
database dir : /var/lib/rabbitmq/mnesia/rabbit@rabbit2
7、其他运维
退出集群节点:
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
移除节点:
rabbitmqctl stop_app
rabbitmqctl forget_cluster_node rabbit@rabbit2
加入集群时指定节点类型:
rabbitmqctl stop_app
rabbitmqctl join_cluster --ram rabbit@rabbit2
rabbitmqctl start_app
其中,--ram 指定内存节点类型,--disc指定磁盘节点类型
修改节点类型:
rabbitmqctl stop_app
rabbitmqctl change_cluster_node_type disc
rabbitmqctl start_app
卸载rabbitmq:
rpm -qa|grep rabbitmq
rabbitmq-server-3.6.14-1.el7.noarch
rpm -e --nodeps rabbitmq-server-3.6.14-1.el7.noarch
rpm -qa|grep erlang
esl-erlang-19.3-1.x86_64
rpm -e --esl-erlang_20.0-1-centos-7_amd64.rpm
rabbitmqctl stop_app 仅关闭应用,不关闭节点
rabbitmqctl start_app 开启应用
rabbitmq--server -detached 启动节点和应用
rabbitmqctl stop 关闭节点和应用
插件下载页面:http://www.rabbitmq.com/community-plugins.html
插件安装:进入插件安装目录{rabbitmq-server}/plugins/
cd /usr/lib/rabbitmq/lib/rabbitmq-server-3.6.14-1/plugins
下载插件:
wget https://bintray.com/rabbitmq/community-plugins/download_file?file_path=rabbitmq_delayed_message_exchange-0.0.1.ez
(如果下载的文件名称不规则就手动重命名一下如:rabbitmq_delayed_message_exchange-0.0.1.ez)
启用插件:rabbitmq-plugins enable rabbitmq_delayed_message_exchange
快来成为我的朋友或合作伙伴,一起交流,一起进步!
QQ群:961179337
微信:lixiang6153
邮箱:[email protected]
公众号:IT技术快餐
更多资料等你来拿!