Docker部署RabbitMQ集群并实现Haproxy代理
一、RabbitMQ简介
RabbitMQ是一个开源的消息代理和队列服务器,用来通过普通协议在完全不同的应用之间共享数据,RabbitMQ是使用Erlang(高并发语言)语言来编写的,并且RabbitMQ是基于AMQP协议的。即Advanced Message Queuing Protocol(高级消息队列协议)
1.1 AMQP专业术语
Server # 又称broker,接受客户端的链接,实现AMQP实体服务。
Connection # 连接,应用程序与broker的网络连接。
Channel # 网络信道,几乎所有的操作都在channel中进行,Channel是进行消息读写的通道。客户端可以建立多个channel,每个channel代表一个会话任务。
Message # 消息,服务器与应用程序之间传送的数据,由Properties和Body组成.Properties可以对消息进行修饰,必须消息的优先级、延迟等高级特性;Body则是消息体内容。
virtualhost # 虚拟地址,用于进行逻辑隔离,最上层的消息路由。一个virtual host里面可以有若干个Exchange和Queue,同一个Virtual Host 里面不能有相同名称的Exchange 或 Queue。
Exchange # 交换机,接收消息,根据路由键转单消息到绑定队列Binding: Exchange和Queue之间的虚拟链接,binding中可以包换routing key。
Routing key # 一个路由规则,虚拟机可用它来确定如何路由一个特定消息。(如负载均衡)
1.2 RabbitMQ整体架构
ClientA(生产者)发送消息到Exchange1(交换机),同时带上RouteKey(路由Key),Exchange1找到绑定交换机为它和绑定传入的RouteKey的队列,把消息转发到对应的队列,消费者Client1,Client2,Client3只需要指定对应的队列名即可以消费队列数据。交换机和队列多对多关系,实际开发中一般是一个交换机对多个队列,防止设计复杂化。
推荐文章:https://www.compose.com/articles/configuring-rabbitmq-exchanges-queues-and-bindings-part-2/
二、RabbitMQ 集群创建准备工作
2.1 拉取RabbitMQ镜像
[root@10-27-0-224 ~]# docker pull rabbitmq:3.8-rc-management
【注】若不使用RabbitMQ的management功能,可以拉取镜像:rabbitmq:3.8-rc
2.2 创建RabbitMQ共享网络
[root@10-27-0-224 ~]# docker network create rabbitmqnet
三、RabbitMQ 集群节点创建与配置
3.1 RabbitMQ节点创建
注意这里三个Node节点要使用相同的 RABBITMQ_ERLANG_COOKIE 值。
(1)RabibitMQ Node1
[root@10-27-0-224 ~]# docker run -d \ --name=rabbitmq1 \ -p 5672:5672 \ -p 15672:15672 \ -v /workspace/rabbitmq/data/rabbitmq1:/var/lib/rabbitmq:z \ -e RABBITMQ_DEFAULT_USER=stargao \ -e RABBITMQ_DEFAULT_PASS=ucloudcn \ -e RABBITMQ_NODENAME=rabbitmq1 \ -e RABBITMQ_ERLANG_COOKIE='Dustyone-Murex-cluster-cookie' \ -h rabbitmq1 \ --net=rabbitmqnet \ rabbitmq:3.8-rc-management
(2)RabibitMQ Node2
[root@10-27-0-224 ~]# docker run -d \ --name=rabbitmq2 \ -p 5673:5672 \ -p 15673:15672 \ -v /workspace/rabbitmq/data/rabbitmq2:/var/lib/rabbitmq:z \ -e RABBITMQ_DEFAULT_USER=stargao \ -e RABBITMQ_DEFAULT_PASS=ucloudcn \ -e RABBITMQ_NODENAME=rabbitmq2 \ -e RABBITMQ_ERLANG_COOKIE='Dustyone-Murex-cluster-cookie' \ -h rabbitmq2 \ --net=rabbitmqnet \ rabbitmq:3.8-rc-management
(3)RabibitMQ Node3
[root@10-27-0-224 ~]# docker run -d \ --name=rabbitmq3 \ -p 5674:5672 \ -p 15674:15672 \ -v /workspace/rabbitmq/data/rabbitmq3:/var/lib/rabbitmq:z \ -e RABBITMQ_DEFAULT_USER=stargao \ -e RABBITMQ_DEFAULT_PASS=ucloudcn \ -e RABBITMQ_NODENAME=rabbitmq3 \ -e RABBITMQ_ERLANG_COOKIE='Dustyone-Murex-cluster-cookie' \ -h rabbitmq3 \ --net=rabbitmqnet \ rabbitmq:3.8-rc-management
(4)查看运行中的容器
[root@10-27-0-224 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5a6013aff365 rabbitmq:3.8-rc-management "docker-entrypoint.s…" 29 seconds ago Up 28 seconds 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 rabbitmq3 9a3c3464af03 rabbitmq:3.8-rc-management "docker-entrypoint.s…" About a minute ago Up About a minute 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 rabbitmq2 cd578cf68acf rabbitmq:3.8-rc-management "docker-entrypoint.s…" 2 minutes ago Up 2 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 rabbitmq1
3.2 组建RabbitMQ集群
登陆RabbitMQ的后两个节点,执行命令加入第一个RabbitMQ节点集群。
(1)RabibitMQ Node2
[root@10-27-0-224 ~]# docker exec rabbitmq2 bash -c \ "rabbitmqctl stop_app && \ rabbitmqctl reset && \ rabbitmqctl join_cluster rabbitmq1@rabbitmq1 && \ rabbitmqctl start_app"
(2)RabibitMQ Node3
[root@10-27-0-224 ~]# docker exec rabbitmq3 bash -c \ "rabbitmqctl stop_app && \ rabbitmqctl reset && \ rabbitmqctl join_cluster --ram rabbitmq1@rabbitmq1 && \ rabbitmqctl start_app"
3.3 登录访问RabbitMQ集群
(1)访问集群
(2)节点退出集群
[root@10-27-0-224 ~]# docker exec rabbitmq3 bash -c \ "rabbitmqctl stop_app && \ rabbitmqctl reset && \ rabbitmqctl start_app"
四、配置Haproxy代理
4.1 准备工作
(1)拉取Haproxy镜像
[root@10-27-0-224 ~]# docker pull haproxy
(2)准备Haproxy配置
[root@10-27-0-224 ~]# mkdir /workspace/rabbitmq/haproxy-etc [root@10-27-0-224 ~]# vim /workspace/rabbitmq/haproxy-etc/haproxy.cfg #Simple configuration for an HTTP proxy listening on port 80 on all # interfaces and forwarding requests to a single backend "servers" with a # single server "server1" listening on 127.0.0.1:8000 global daemon maxconn 256 defaults mode http timeout connect 5000ms timeout client 5000ms timeout server 5000ms listen rabbitmq_cluster bind 0.0.0.0:5677 option tcplog mode tcp balance leastconn server rabbit1 rabbitmq1:5672 check inter 2s rise 2 fall 3 server rabbit2 rabbitmq2:5672 check inter 2s rise 2 fall 3 server rabbit3 rabbitmq3:5672 check inter 2s rise 2 fall 3 listen http_front bind 0.0.0.0:8002 stats uri /haproxy?stats listen rabbitmq_admin bind 0.0.0.0:8001 server rabbit1 rabbitmq1:15672 server rabbit2 rabbitmq2:15672 server rabbit3 rabbitmq3:15672
4.2 创建Haproxy容器
[root@10-27-0-224 ~]# docker run -d \ --name rabbitmq-haproxy \ -p 8002:8002 -p 5677:5677 -p 8001:8001 \ --net=rabbitmqnet \ -v /workspace/rabbitmq/haproxy-etc:/usr/local/etc/haproxy:ro \ haproxy:latest
4.3 访问Haproxy
(1)通过haproxy来访问rabbitMQ集群
(2)获取haproxy的状态
http://10.27.0.224:8002/haproxy?stats
五、Docker-compose部署RabbitMQ集群方案
5.1 Rabbitmq环境准备
(1)目录结构情况
[root@10-27-0-224 ~]# mkdir /workspace [root@10-27-0-224 workspace]# mkdir {config,data,haproxy-etc,rabbitmq1,rabbitmq2,rabbitmq3} [root@10-27-0-224 workspace]# touch config/docker-compose.yml [root@10-27-0-224 workspace]# touch haproxy-etc/haproxy.cfg [root@10-27-0-224 /]# tree /workspace workspace/ ├── config │ └── docker-compose.yml ├── data ├── haproxy-etc │ └── haproxy.cfg ├── rabbitmq1 ├── rabbitmq2 └── rabbitmq3
(2)创建RabbitMQ集群网络
[root@10-27-0-224 /]# docker network create rabbitmqnet
5.2 Docker-compose文件配置
[root@10-27-0-224 /]# vim /workspace/config/docker-compose.yml version: '3.7' services: rabbitmq1: image: rabbitmq:3.8-rc-management container_name: rabbitmq1 restart: always ports: - 5672:5672 - 15672:15672 volumes: - /workspace/rabbitmq/data/rabbitmq1:/var/lib/rabbitmq:z environment: RABBITMQ_DEFAULT_USER: 'stargao' RABBITMQ_DEFAULT_PASS: 'ucloudcn' RABBITMQ_NODENAME: 'rabbitmq1' RABBITMQ_ERLANG_COOKIE: 'Dustyone-Murex-cluster-cookie' hostname: rabbitmq1 rabbitmq2: image: rabbitmq:3.8-rc-management container_name: rabbitmq2 restart: always ports: - 5673:5672 - 15673:15672 volumes: - /workspace/rabbitmq/data/rabbitmq2:/var/lib/rabbitmq:z environment: RABBITMQ_DEFAULT_USER: 'stargao' RABBITMQ_DEFAULT_PASS: 'ucloudcn' RABBITMQ_NODENAME: 'rabbitmq2' RABBITMQ_ERLANG_COOKIE: 'Dustyone-Murex-cluster-cookie' hostname: rabbitmq2 links: - rabbitmq1 rabbitmq3: image: rabbitmq:3.8-rc-management container_name: rabbitmq3 restart: always ports: - 5674:5672 - 15674:15672 volumes: - /workspace/rabbitmq/data/rabbitmq3:/var/lib/rabbitmq:z environment: RABBITMQ_DEFAULT_USER: 'stargao' RABBITMQ_DEFAULT_PASS: 'ucloudcn' RABBITMQ_NODENAME: 'rabbitmq3' RABBITMQ_ERLANG_COOKIE: 'Dustyone-Murex-cluster-cookie' hostname: rabbitmq3 links: - rabbitmq1 - rabbitmq2 rabbitmq-haproxy: image: haproxy container_name: rabbitmq-haproxy restart: always ports: - 8002:8002 - 5677:5677 - 8001:8001 volumes: - /workspace/haproxy-etc:/usr/local/etc/haproxy:ro links: - rabbitmq1 - rabbitmq2 - rabbitmq3 networks: default: external: name: rabbitmqnet
5.3 执行docker-compose.yml文件
(1)启动容器
[root@ansible config]# docker-compose up -d
(2)删除容器
[root@ansible config]# docker-compose down Removing rabbitmq-haproxy ... done Removing rabbitmq3 ... done Removing rabbitmq2 ... done Removing rabbitmq1 ... done Network rabbitmqnet is external, skipping
(3)查看容器运行情况
[root@ansible config]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f1321cd3b5e haproxy "docker-entrypoint..." 24 seconds ago Up 23 seconds 0.0.0.0:5677->5677/tcp, 0.0.0.0:8001-8002->8001-8002/tcp rabbitmq-haproxy 54576aa634de rabbitmq:3.8-rc-management "docker-entrypoint..." 25 seconds ago Up 24 seconds 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 rabbitmq3 be24e8e3a4b7 rabbitmq:3.8-rc-management "docker-entrypoint..." 25 seconds ago Up 24 seconds 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 rabbitmq2 0110e78d3388 rabbitmq:3.8-rc-management "docker-entrypoint..." 26 seconds ago Up 25 seconds 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 rabbitmq1
作者:UStarGao
链接:https://www.starcto.com/Cloud-BigData-AI/234.html
来源:STARCTO
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
UCloud云平台推荐
随便看看
- 2022-05-17MySQL命令统计的库大小和物理文件大小差异
- 2021-08-07CI/CD Pipeline之Jenkins部署
- 2021-02-27VMware Workstation虚机NAT的实现
- 2021-01-31Linux安装包管理-Yum仓库配置
- 2022-07-13极简了解Ceph存储架构