CentOS7 使用docker安装rabbitMQ并搭建集群

Posted Peter-OK

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7 使用docker安装rabbitMQ并搭建集群相关的知识,希望对你有一定的参考价值。

注:为保证操作成功,请确认系统相关配置

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)

1. 系统是在vmware上 采用 桥接模式 接入网络,可参考:https://blog.csdn.net/xingkongtianma01/article/details/109593787

2. docker安装设置开机启动,可参考:https://www.cnblogs.com/adolfmc/p/12115686.html

(如果docker启动报错 WARNING: IPv4 forwarding is disabled. Networking will not work. 可参考:https://blog.csdn.net/w719566673/article/details/83182759

3. 使用docker安装rabbitMQ

#拉取镜像,指定带有“mangement”的版本(包含web管理页面),该版本包含了web控制页面
docker pull rabbitmq:management

查看拉取的镜像

docker images

#运行镜像 - 方式一:默认guest 用户,密码也是 guest
docker run -d --hostname my-rabbit --name rabbit -p 15672:15672 -p 5672:5672 rabbitmq:management

#运行镜像 - 方式二:设置用户名和密码
docker run -d --hostname my-rabbit --name rabbit -e RABBITMQ_DEFAULT_USER=user -e RABBITMQ_DEFAULT_PASS=password -p 15672:15672 -p 5672:5672 rabbitmq:management

说明:

  • -d 后台进程运行
  • --hostname RabbitMQ主机名称
  • --name 容器名称
  • -p port:port 本地端口:容器端口
  • -p 15672:15672 http访问端口
  • -p 5672:5672 amqp访问端口
  • -e 指定环境变量;(RABBITMQ_DEFAULT_VHOST:默认虚拟机名;RABBITMQ_DEFAULT_USER:默认的用户名;RABBITMQ_DEFAULT_PASS:默认用户名的密码)

查看正在运行容器

docker ps 

开启防火墙15672端口。(测试可直接关闭防火墙,生产不建议)

 firewall-cmd --zone=public --add-port=15672/tcp --permanent        

 firewall-cmd --reload 

浏览器打开web管理端:http://ip:15672。如未设置用户名和密码则默认(guest/guest)

到此,RabbitMQ已经安装并运行起来了。

后续 即可根据镜像name或containerID 停止、删除 或 启动镜像

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                                                                                         NAMES
ac3e15d01b34        rabbitmq:management   "docker-entrypoint.s…"   19 hours ago        Up 3 minutes        4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp   rabbit
[root@localhost ~]# docker stop rabbit
rabbit
[root@localhost ~]# docker start rabbit
rabbit
[root@localhost ~]# docker stop rabbit 
rabbit
[root@localhost ~]# docker rm rabbit  
rabbit
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                     PORTS               NAMES
[root@localhost ~]# 

 4.构建RabbitMQ集群

1. 启动多个RabbitMQ节点

使用Docker启动3个RabbitMQ节点,目标如下表所示:

服务器ip端口hostname管理界面地址
172.30.12.225672myRabbit1172.30.12.22:15672
172.30.12.225673myRabbit2172.30.12.22:15673
172.30.12.225674myRabbit3172.30.12.22:15674

命令:

docker run -d --hostname myRabbit1 --name rabbit1 -p 15672:15672 -p 5672:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitcookie' rabbitmq:management
docker run -d --hostname myRabbit2 --name rabbit2 -p 15672:15672 -p 5672:5672 --link rabbit1:myRabbit1 -e RABBITMQ_ERLANG_COOKIE='rabbitcookie' rabbitmq:management
docker run -d --hostname myRabbit3 --name rabbit3 -p 15672:15672 -p 5672:5672 --link rabbit1:myRabbit1 --link rabbit2:myRabbit2 -e RABBITMQ_ERLANG_COOKIE='rabbitcookie' rabbitmq:management

注意:

  • -e RABBITMQ_ERLANG_COOKIE='rabbitcookie' 必须设置为相同,因为 Erlang节点间是通过认证Erlang cookie的方式来允许互相通信的。
  • --link rabbit1:myRabbit1 --link rabbit2:myRabbit2 不要漏掉,否则会 一直处在 Cluster status of node rabbit@myRabbit3 ... 没有反应

启动完成之后,使用docker ps命令查看运行情况,确保RabbitMQ都已经启动。

CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS              PORTS                                                                                        NAMES
a2b5e54a39c9        rabbitmq:management   "docker-entrypoint.s…"   About an hour ago   Up About an hour    4369/tcp, 5671/tcp, 15671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:5674->5672/tcp, 0.0.0.0:15674->15672/tcp   rabbit3
feb22b5ca25f        rabbitmq:management   "docker-entrypoint.s…"   16 hours ago        Up 16 hours         4369/tcp, 5671/tcp, 15671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:5673->5672/tcp, 0.0.0.0:15673->15672/tcp   rabbit2
ada162f8ee63        rabbitmq:management   "docker-entrypoint.s…"   16 hours ago        Up 16 hours         4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp   rabbit1

2. 加入集群

内存节点和磁盘节点的选择:

每个RabbitMQ节点,要么是内存节点,要么是磁盘节点。内存节点将所有的队列、交换器、绑定、用户等元数据定义都存储在内存中;而磁盘节点将元数据存储在磁盘中。单节点系统只允许磁盘类型的节点,否则当节点重启以后,所有的配置信息都会丢失。如果采用集群的方式,可以选择至少配置一个节点为磁盘节点,其余部分配置为内存节点,,这样可以获得更快的响应。所以本集群中配置节点1位磁盘节点,节点2和节点3位内存节点。

集群中的第一个节点将初始元数据代入集群中,并且无须被告知加入。而第2个和之后加入的节点将加入它并获取它的元数据。要加入节点,需要进入Docker容器,重启RabbitMQ。

设置节点1:

[root@localhost ~]# docker exec -it rabbit1 bash
root@myRabbit1:/# rabbitmqctl stop_app
RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
Stopping rabbit application on node rabbit@myRabbit1 ...
root@myRabbit1:/# rabbitmqctl reset
RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
Resetting node rabbit@myRabbit1 ...
root@myRabbit1:/# rabbitmqctl start_app
RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
Starting node rabbit@myRabbit1 ...
root@myRabbit1:/# exit
exit

设置节点2:

[root@localhost ~]# docker exec -it rabbit2 bash
root@myRabbit2:/# rabbitmqctl stop_app
RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
Stopping rabbit application on node rabbit@myRabbit2 ...
root@myRabbit2:/# rabbitmqctl reset
RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
Resetting node rabbit@myRabbit2 ...
root@myRabbit2:/# rabbitmqctl join_cluster --ram rabbit@myRabbit1 
RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
Clustering node rabbit@myRabbit2 with rabbit@myRabbit1
root@myRabbit2:/# rabbitmqctl start_app
RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
Starting node rabbit@myRabbit2 ...
root@myRabbit2:/# exit
exit

设置节点3:

[root@localhost ~]# docker exec -it rabbit3 bash
root@myRabbit3:/# rabbitmqctl stop_app
RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
Stopping rabbit application on node rabbit@myRabbit3 ...
root@myRabbit3:/# rabbitmqctl reset
RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
Resetting node rabbit@myRabbit3 ...
root@myRabbit3:/# rabbitmqctl join_cluster --ram rabbit@myRabbit1 
RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
Clustering node rabbit@myRabbit3 with rabbit@myRabbit1
root@myRabbit3:/# rabbitmqctl start_app
RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
Starting node rabbit@myRabbit3 ...
root@myRabbit3:/# exit
exit

节点设置完成之后,在浏览器访问172.30.12.22:15672、172.30.12.22:15673 和 172.30.12.22:15674中任意一个,都会看到RabbitMQ集群已经创建成功。

3. 配置镜像队列

镜像队列工作原理:在非镜像队列的集群中,消息会路由到指定的队列。当配置为镜像队列之后,消息除了按照路由规则投递到相应的队列外,还会投递到镜像队列的拷贝。也可以想象在镜像队列中隐藏着一个fanout交换器,将消息发送到镜像的队列的拷贝。

进入任意一个RabbitMQ节点,执行如下命令:

rabbitmqctl set_policy ha-all "^" '"ha-mode":"all"'

可以设置镜像队列,"^"表示匹配所有队列,即所有队列在各个节点上都会有备份。在集群中,只需要在一个节点上设置镜像队列,设置操作会同步到其他节点。

查看集群的状态:

rabbitmqctl cluster_status
[root@localhost ~]# docker exec -it rabbit3 bash
root@myRabbit3:/# rabbitmqctl cluster_status
RABBITMQ_ERLANG_COOKIE env variable support is deprecated and will be REMOVED in a future version. Use the $HOME/.erlang.cookie file or the --erlang-cookie switch instead.
Cluster status of node rabbit@myRabbit3 ...
Basics

Cluster name: rabbit@myRabbit1

Disk Nodes

rabbit@myRabbit1

RAM Nodes

rabbit@myRabbit2
rabbit@myRabbit3

Running Nodes

rabbit@myRabbit1
rabbit@myRabbit2
rabbit@myRabbit3

Versions

rabbit@myRabbit1: RabbitMQ 3.8.9 on Erlang 23.1.2
rabbit@myRabbit2: RabbitMQ 3.8.9 on Erlang 23.1.2
rabbit@myRabbit3: RabbitMQ 3.8.9 on Erlang 23.1.2

Maintenance status

Node: rabbit@myRabbit1, status: not under maintenance
Node: rabbit@myRabbit2, status: not under maintenance
Node: rabbit@myRabbit3, status: not under maintenance

Alarms

(none)

Network Partitions

(none)

Listeners

Node: rabbit@myRabbit1, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@myRabbit1, interface: [::], port: 15692, protocol: http/prometheus, purpose: Prometheus exporter API over HTTP
Node: rabbit@myRabbit1, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@myRabbit1, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@myRabbit2, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@myRabbit2, interface: [::], port: 15692, protocol: http/prometheus, purpose: Prometheus exporter API over HTTP
Node: rabbit@myRabbit2, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@myRabbit2, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@myRabbit3, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@myRabbit3, interface: [::], port: 15692, protocol: http/prometheus, purpose: Prometheus exporter API over HTTP
Node: rabbit@myRabbit3, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@myRabbit3, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 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: virtual_host_metadata, state: enabled

 

以上是关于CentOS7 使用docker安装rabbitMQ并搭建集群的主要内容,如果未能解决你的问题,请参考以下文章

centos7下安装使用docker

CentOS7安装Docker

centos7下使用docker安装gitlab转

Centos7上docker的安装和基本使用

centos7安装docker1.31

Docker 安装与容器使用并安装centos7