實驗環境: 操作系統為 Centos 7.2 IP hostName 192.168.190.132 node132 192.168.190.139 node139 192.168.190.140 n... ...
實驗環境:
操作系統為 Centos 7.2
IP hostName
192.168.190.132 node132
192.168.190.139 node139
192.168.190.140 node140
RabbitMQ相關埠
- 4369 (epmd)
- 5672, 5671 (AMQP 0-9-1 and 1.0 without and with TLS)
- 25672. This port used by Erlang distribution for inter-node and CLI tools communication and is allocated from a dynamic range (limited to a single port by default, computed as AMQP port + 20000). Seenetworking guide for details.
- 15672 (if management plugin is enabled)
- 61613, 61614 (if STOMP is enabled)
- 1883, 8883 (if MQTT is enabled)
相應埠開啟防火牆設置
添加EPEL源
[root@node139 ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
Retrieving https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
warning: /var/tmp/rpm-tmp.2hDPSr: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:epel-release-7-8 ################################# [100%]
添加Erlang源[root@node139 ~]# rpm -Uvh http://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm
Retrieving http://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm
Preparing... ################################# [100%]
Updating / installing...
1:erlang-solutions-1.0-1 ################################# [100%]
--2016-11-29 06:34:38-- http://packages.erlang-solutions.com/rpm/centos/erlang_solutions.repo
Resolving packages.erlang-solutions.com (packages.erlang-solutions.com)... 31.172.186.53
Connecting to packages.erlang-solutions.com (packages.erlang-solutions.com)|31.172.186.53|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 245
Saving to: 'erlang_solutions.repo'
100%[==========================================================================>] 245 --.-K/s in 0s
2016-11-29 06:34:39 (27.5 MB/s) - 'erlang_solutions.repo' saved [245/245]
安裝RabbitMQ軟體版本:http://www.rabbitmq.com/releases/rabbitmq-server
[root@node139 ~]# wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.6/rabbitmq-server-3.6.6-1.el7.noarch.rpm
[root@node139 ~]# rpm --import https://www.rabbitmq.com/rabbitmq-signing-key-public.asc
[root@node139 ~]# yum install rabbitmq-server-3.6.6-1.el7.noarch.rpm
啟動服務
[root@node139 ~]# systemctl enable rabbitmq-server.service
[root@node139 ~]# systemctl start rabbitmq-server.service
啟用RabbitMQ監控插件[root@node139 ~]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Applying plugin configuration to rabbit@node139... started 6 plugins.
[root@node139 ~]# netstat -apn | grep 15672
tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 4119/beam.smp
RabbitMQ用戶管理添加用戶(用戶名root,密碼admin)
[root@node139 ~]# rabbitmqctl add_user admin admin
設置用戶角色(設置admin用戶為管理員角色)
[root@node139 ~]# rabbitmqctl set_user_tags admin administrator
Setting tags for user "admin" to [administrator] ...
設置用戶許可權(設置admin用戶配置、寫、讀的許可權)
[root@node139 ~]# rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
Setting permissions for user "admin" in vhost "/" ...
刪除用戶(刪除guest用戶)
[root@node139 ~]# rabbitmqctl delete_user guest
RabbitMQ集群配置
集群配置在單機配置完成的基礎上進行
以下以node140為例,node132需要做同樣的操作
- 添加/etc/hosts條目
在node132、node139、node140里的/etc/hosts文件中分別添加:
192.168.190.132 node132
192.168.190.139 node139
192.168.190.140 node140- 設置每個節點Cookie
Rabbitmq的集群是依賴於erlang的集群來工作的,所以必須先構建起erlang的集群環境。Erlang的集群中各節點是通過一個magic cookie來實現的,這個cookie存放在 /var/lib/rabbitmq/.erlang.cookie 中,文件是400的許可權。所以必須保證各節點cookie保持一致,否則節點之間就無法通信
[root@node140 /]# echo -n "AZVOCZYZZBVFLBPTBXU" > /var/lib/rabbitmq/.erlang.cookie
[root@node140 ~]# chown rabbitmq:root /var/lib/rabbitmq/.erlang.cookie
[root@node140 /]# chmod 600 /var/lib/rabbitmq/.erlang.cookie
[root@node140 /]# cat /var/lib/rabbitmq/.erlang.cookie
AZVOCZYZZBVFLBPTBXU
啟動服務
[root@node140 ~]# systemctl start rabbitmq-server.service
[root@node140 ~]# rabbitmq-server -detached
開通防火牆
[root@node140 ~]# firewall-cmd --permanent --add-port={4369/tcp,25672/tcp}
success
[root@node140 ~]# firewall-cmd --reload
success
- 加入集群
預設是磁碟節點,如果是記憶體節點的話,需要加--ram參數
[root@node140 ~]# rabbitmqctl stop_app
Stopping node rabbit@node140 ...
[root@node140 ~]# rabbitmqctl join_cluster rabbit@node139
Clustering node rabbit@node140 with rabbit@node139 ...
[root@node140 ~]# rabbitmqctl start_app
Starting node rabbit@node140 ...
- 設置鏡像策略
[root@node140 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}'
Setting policy "ha-all" for pattern "^" to "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"automatic\"}" with priority "0" …
啟用RabbitMQ監控插件cluster搭建起來後若在web管理工具中rabbitmq_management的Overview的Nodes部分看到"Node statistics not available"的信息,說明在該節點上web管理插件還未啟用。
[root@node140 ~]# rabbitmq-plugins enable rabbitmq_management
集群配置好後,可以在 RabbitMQ 任意節點上執行下麵的命令來查看是否集群配置成功。
[root@node140 ~]# rabbitmqctl cluster_status
其它節點需要做同樣的操作
登錄站點http://192.168.190.139:15672/
參考網址:http://www.rabbitmq.com/clustering.html
安裝中遇到的問題:
這個/var/lib/rabbitmq/.erlang.cookie文件需要修改文件所有者和文件的許可權為rw,否則rabbitmq服務無法啟動;