RabbitMQ 3.8.11 集群搭建

·  阅读 315

1 环境准备

1.1 机器要求

操作系统:Centos7

节点IP工作模式操作系统
rabbitmq1192.168.8.171DISKCentOS 7.0 - 64位
rabbitmq2192.168.8.172DISKCentOS 7.0 - 64位(克隆rabbitmq1的方式)
rabbitmq3192.168.8.173DISKCentOS 7.0 - 64位(克隆rabbitmq1的方式)

1.2 hostname

[root@base ~]# hostnamectl set-hostname rabbitmq1 --static
[root@base ~]# echo "192.168.8.171 rabbitmq1" >> /etc/hosts
[root@base ~]# echo "192.168.8.172 rabbitmq2" >> /etc/hosts
[root@base ~]# echo "192.168.8.173 rabbitmq3" >> /etc/hosts
复制代码

1.3 网络设置

[root@base1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO="static" #dhcp改为static 
ONBOOT="yes" #开机启用本配置
IPADDR=192.168.8.171 #静态IP 172;173
GATEWAY=192.168.8.2 #默认网关
NETMASK=255.255.255.0 #子网掩码
DNS1=114.114.114.114 #DNS 配置
DNS2=8.8.8.8 #DNS 配置

[root@base1 ~]# reboot
复制代码

2 单机版

2.1 更新软件包和存储库

[root@rabbitmq1 ~]# yum -y install wget make gcc gcc-c++ kernel-devel openssl-devel ncurses-deve && yum -y update
复制代码

2.2 安装Erlang(RabbitMQ 运行需要 Erlang环境):

[root@rabbitmq1 ~]# vi /etc/yum.repos.d/rabbitmq-erlang.repo
[rabbitmq-erlang]
name=rabbitmq-erlang
baseurl=https://dl.bintray.com/rabbitmq/rpm/erlang/23/el/7
gpgcheck=1
gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
repo_gpgcheck=0
enabled=1
[root@rabbitmq1 ~]# yum -y install erlang socat
复制代码

2.3 安装 RabbitMQ Server

[root@rabbitmq1 ~]# mkdir -p ~/download && cd ~/download
[root@rabbitmq1 download]# wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.11/rabbitmq-server-3.8.11-1.el7.noarch.rpm
[root@rabbitmq1 download]# rpm --import https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
[root@rabbitmq1 download]# yum -y install rabbitmq-server-3.8.11-1.el7.noarch.rpm

复制代码

注意--卸载RabbitMQ命令如下:

$ rpm -e rabbitmq-server-3.8.11-1.el7.noarch && rm -rf /var/lib/rabbitmq/
复制代码

2.4 安装好之后,就可以启动 RabbitMQ Server 了

[root@rabbitmq1 ~]#systemctl enable rabbitmq-server  && systemctl start rabbitmq-server
Job for rabbitmq-server.service failed because the control process exited with error code. See "systemctl status rabbitmq-server.service" and "journalctl -xe" for details.
复制代码

2.5 默认情况下未将服务器作为守护程序启动

[root@rabbitmq1 ~]# chkconfig rabbitmq-server on
Note: Forwarding request to 'systemctl enable rabbitmq-server.service'.
复制代码

2.5 查看rabbitmq服务状态

[root@rabbitmq1 ~]# systemctl status rabbitmq-server
● rabbitmq-server.service - RabbitMQ broker
   Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-02-12 07:08:17 JST; 49s ago
 Main PID: 6511 (beam.smp)
   Status: "Initialized"
   CGroup: /system.slice/rabbitmq-server.service
           ├─6511 /usr/lib64/erlang/erts-11.1.7/bin/beam.smp -W w -MBas ageffcbf -MHas ageffc...
           ├─6526 erl_child_setup 32768
           ├─6551 /usr/lib64/erlang/erts-11.1.7/bin/epmd -daemon
           ├─6574 inet_gethost 4
           └─6575 inet_gethost 4

Feb 12 07:08:15 rabbitmq1 rabbitmq-server[6511]: ##########  Licensed under the MPL 2.0. We...om
Feb 12 07:08:15 rabbitmq1 rabbitmq-server[6511]: Doc guides: https://rabbitmq.com/documenta...ml
Feb 12 07:08:15 rabbitmq1 rabbitmq-server[6511]: Support:    https://rabbitmq.com/contact.html
Feb 12 07:08:15 rabbitmq1 rabbitmq-server[6511]: Tutorials:  https://rabbitmq.com/getstarte...ml
Feb 12 07:08:15 rabbitmq1 rabbitmq-server[6511]: Monitoring: https://rabbitmq.com/monitorin...ml
Feb 12 07:08:15 rabbitmq1 rabbitmq-server[6511]: Logs: /var/log/rabbitmq/rabbit@rabbitmq1.log
Feb 12 07:08:15 rabbitmq1 rabbitmq-server[6511]: /var/log/rabbitmq/rabbit@rabbitmq1_upgrade.log
Feb 12 07:08:15 rabbitmq1 rabbitmq-server[6511]: Config file(s): (none)
Feb 12 07:08:17 rabbitmq1 rabbitmq-server[6511]: Starting broker... completed with 0 plugins.
Feb 12 07:08:17 rabbitmq1 systemd[1]: Started RabbitMQ broker.
Hint: Some lines were ellipsized, use -l to show in full.
复制代码

2.6 启动 RabbitMQ Web 管理控制台

[root@rabbitmq1 ~]# rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@rabbitmq1:
rabbitmq_management
The following plugins have been configured:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch
Applying plugin configuration to rabbit@rabbitmq1...
The following plugins have been enabled:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch

started 3 plugins.
复制代码

2.7 创建管理用户

RabbitMQ Server 默认guest用户,只能localhost地址访问

[root@rabbitmq1 ~]# rabbitmqctl add_user admin admin123 &&
> rabbitmqctl set_user_tags admin administrator &&
> rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
Adding user "admin" ...
Done. Don't forget to grant the user permissions to some virtual hosts! See 'rabbitmqctl help set_permissions' to learn more.
Setting tags for user "admin" to [administrator] ...
Setting permissions for user "admin" in vhost "/" ...
复制代码

2.8 添加防火墙运行访问端口

[root@rabbitmq1 ~]# firewall-cmd --zone=public --permanent --add-port=4369/tcp && 
firewall-cmd --zone=public --permanent --add-port=25672/tcp && 
firewall-cmd --zone=public --permanent --add-port=5671-5672/tcp && 
firewall-cmd --zone=public --permanent --add-port=15672/tcp && 
firewall-cmd --zone=public --permanent --add-port=61613-61614/tcp && 
firewall-cmd --zone=public --permanent --add-port=1883/tcp && 
firewall-cmd --zone=public --permanent --add-port=8883/tcp
复制代码

2.9 重新启动防火墙

[root@rabbitmq1 ~]# firewall-cmd --reload
success
复制代码

2.10 浏览器访问

3 搭建 RabbitMQ Server 高可用集群

从rabbitmq1克隆成rabbitmq2和rabbitmq3,重复1.2,1.3操作

3.1 查看.erlang.cookie文件

[root@rabbitmq1 ~]# find / -name ".erlang.cookie"
/var/lib/rabbitmq/.erlang.cookie
[root@rabbitmq1 ~]# cat /var/lib/rabbitmq/.erlang.cookie
VFHQROHPWGMZFFUQPTWO
复制代码

注意:这里克隆后的.erlang.cookie肯定相同

3.2 后台方式启动 RabbitMQ Server

  • rabbitmq1节点
[root@rabbitmq1 ~]# rabbitmqctl stop
[root@rabbitmq1 ~]# rabbitmq-server -detached
复制代码
  • rabbitmq2节点
[root@rabbitmq2 ~]# rabbitmqctl stop
[root@rabbitmq2 ~]# rabbitmq-server -detached
复制代码
  • rabbitmq3节点
[root@rabbitmq3 ~]# rabbitmqctl stop
[root@rabbitmq3 ~]# rabbitmq-server -detached
复制代码

3.3 然后以rabbitmq1作为集群中心

默认是磁盘节点,如果是内存节点的话,需要加--ram参数

  • rabbitmq1节点
[root@rabbitmq1 ~]# rabbitmqctl stop_app
[root@rabbitmq1 ~]# rabbitmqctl reset
[root@rabbitmq1 ~]# rabbitmqctl start_app
复制代码
  • rabbitmq2节点
[root@rabbitmq2 ~]# rabbitmqctl stop_app
[root@rabbitmq2 ~]# rabbitmqctl reset
[root@rabbitmq2 ~]# rabbitmqctl join_cluster rabbit@rabbitmq1
[root@rabbitmq2 ~]# rabbitmqctl start_app
复制代码
  • rabbitmq3节点
[root@rabbitmq3 ~]# rabbitmqctl stop_app
[root@rabbitmq3 ~]# rabbitmqctl reset
[root@rabbitmq3 ~]# rabbitmqctl join_cluster rabbit@rabbitmq1
[root@rabbitmq3 ~]# rabbitmqctl start_app
复制代码

3.4 查看集群的状态

[root@rabbitmq1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq1 ...
Basics

Cluster name: rabbit@rabbitmq1

Disk Nodes

rabbit@rabbitmq1
rabbit@rabbitmq2
rabbit@rabbitmq3

Running Nodes

rabbit@rabbitmq1
rabbit@rabbitmq2
rabbit@rabbitmq3

Versions

rabbit@rabbitmq1: RabbitMQ 3.8.11 on Erlang 23.2.4
rabbit@rabbitmq2: RabbitMQ 3.8.11 on Erlang 23.2.4
rabbit@rabbitmq3: RabbitMQ 3.8.11 on Erlang 23.2.4

Maintenance status

Node: rabbit@rabbitmq1, status: not under maintenance
Node: rabbit@rabbitmq2, status: not under maintenance
Node: rabbit@rabbitmq3, status: not under maintenance

Alarms

(none)

Network Partitions

(none)

Listeners

Node: rabbit@rabbitmq1, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbitmq1, interface: [::], port: 25672, protocol: clustering, purpose: inter-node                                                                                                   and CLI tool communication
Node: rabbit@rabbitmq1, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQ                                                                                                  P 1.0
Node: rabbit@rabbitmq2, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbitmq2, interface: [::], port: 25672, protocol: clustering, purpose: inter-node                                                                                                   and CLI tool communication
Node: rabbit@rabbitmq2, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQ                                                                                                  P 1.0
Node: rabbit@rabbitmq3, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbitmq3, interface: [::], port: 25672, protocol: clustering, purpose: inter-node                                                                                                   and CLI tool communication
Node: rabbit@rabbitmq3, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQ                                                                                                  P 1.0

Feature flags

Flag: drop_unroutable_metric, state: enabled
Flag: empty_basic_get_metric, state: enabled
Flag: implicit_default_bindings, state: enabled
Flag: maintenance_mode_status, state: enabled
Flag: quorum_queue, state: enabled
Flag: user_limits, state: enabled
Flag: virtual_host_metadata, state: enabled
复制代码

3.5 创建管理用户

[root@rabbitmq1 ~]# rabbitmqctl add_user admin admin123 &&
> rabbitmqctl set_user_tags admin administrator &&
> rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
Adding user "admin" ...
Done. Don't forget to grant the user permissions to some virtual hosts! See 'rabbitmqctl help set_permissions' to learn more.
Setting tags for user "admin" to [administrator] ...
Setting permissions for user "admin" in vhost "/" ...

复制代码

3.6 浏览器访问

4 搭建 HAProxy 负载均衡

4.1 在rabbitmq1安装HAProxy

[root@rabbitmq1 ~]# rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@rabbitmq1 ~]# yum -y install haproxy
复制代码

4.2 配置 HAProxy

[root@rabbitmq1 ~]# cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
[root@rabbitmq1 ~]# vi /etc/haproxy/haproxy.cfg
global
    log     127.0.0.1  local0 info
    log     127.0.0.1  local1 notice
    daemon
    maxconn 4096

defaults
    log     global
    mode    tcp
    option  tcplog
    option  dontlognull
    retries 3
    option  abortonclose
    maxconn 4096
    timeout connect  5000ms
    timeout client  3000ms
    timeout server  3000ms
    balance roundrobin

listen private_monitoring
    bind    0.0.0.0:8100
    mode    http
    option  httplog
    stats   refresh  5s
    stats   uri  /stats
    stats   realm   Haproxy
    stats   auth  admin:admin123

listen rabbitmq_admin
    bind    0.0.0.0:8102
    server  rabbitmq1 rabbitmq1:15672
    server  rabbitmq2 rabbitmq2:15672
    server  rabbitmq3 rabbitmq3:15672

listen rabbitmq_cluster
    bind    0.0.0.0:8101
    mode    tcp
    option  tcplog
    balance roundrobin
    timeout client  3h
    timeout server  3h
    server  rabbitmq1  rabbitmq1:5672  check  inter  5000  rise  2  fall  3
    server  rabbitmq2  rabbitmq2:5672  check  inter  5000  rise  2  fall  3
    server  rabbitmq3  rabbitmq3:5672  check  inter  5000  rise  2  fall  3
复制代码

4.3 启动 HAProxy

[root@rabbitmq1 ~]# haproxy -f /etc/haproxy/haproxy.cfg
复制代码

4.4 防火墙设置

firewall-cmd --zone=public --permanent --add-port=8100/tcp && 
firewall-cmd --zone=public --permanent --add-port=8101/tcp && 
firewall-cmd --zone=public --permanent --add-port=8102/tcp
复制代码

4.5 重启防火墙

[root@rabbitmq1 ~]# firewall-cmd --reload
success
复制代码

4.6 浏览器查看 HAProxy 负载均衡信息

http://192.168.8.171:8100/stats: HAProxy 负载均衡信息地址 ,账号密码:admin/admin123

http://192.168.8.171:8102/#/ : RabbitMQ Server Web 管理界面(基于负载均衡)。

http://192.168.8.171:8101 : RabbitMQ Server 服务地址(基于负载均衡)
参考:www.cnblogs.com/xishuai/p/c…

分类:
后端
标签: