Docker多主机搭建RabbitMQ集群
本文通过docker swarm建立多主机的Rabbitmq集群,自带配置文件,无需再次配置,直接启动即可
3台主机,分别为:
test01:192.168.1.10,角色:manager
test02:192.168.1.20,角色:worker
test03:192.168.1.30,角色:worker
配置好各自的hostname
docker swarm
创建docker swarm集群
docker swarm init --advertise-addr 192.168.1.10Swarm initialized: current node (z2n633mty5py7u9wyl423qnq0) is now a manager. To add a worker to this swarm, run the following command: # 这就是添加节点的方式(要保存初始化后token,因为在节点加入时要使用token作为通讯的密钥) docker swarm join --token SWMTKN-1-2lefzq18zohy9yr1vskutf1sfb2a590xz9d0mjj2m15zu9eprw-2938j5f50t35ycut0vbj2sx0s 192.168.1.10:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
上面命令执行后,该机器自动加入到swarm集群。这个会创建一个集群token,获取全球唯一的 token,作为集群唯一标识。后续将其他节点加入集群都会用到这个token值。其中,--advertise-addr参数表示其它swarm中的worker节点使用此ip地址与manager联系。命令的输出包含了其它节点如何加入集群的命令。
因此,其它两个节点加入集群的话,只需要按照提示,输入:
docker swarm join --token SWMTKN-1-2lefzq18zohy9yr1vskutf1sfb2a590xz9d0mjj2m15zu9eprw-2938j5f50t35ycut0vbj2sx0s 192.168.1.10:2377
即可加入docker swarm集群
在manager服务器中确认新的两个workers已经加入
[root@test01 docker]# docker node lsID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSIONsdhq8qsmvptdge76nxe3shrug * test01 Ready Active Leader 19.03.1394gn5oj2so67wi63smjhdss69 test02 Ready Active 19.03.13lkl3pcqmuh43iwi4fjccmgg9r test03 Ready Active 19.03.13
创建网络
创建一个overlay的网络
docker network create -d overlay rabbitmq-cluster
如果已经有这个网络了先删除:bashdocker network rm rabbitmq-cluster
查看已创建好的网络
docker network ls
NETWORK ID NAME DRIVER SCOPE4fb77484d00e bridge bridge localfe2fe4e230f3 docker_gwbridge bridge local392f51dce275 host host localxyiiys695y3q ingress overlay swarme7e48d381800 none null localun13usga59lw rabbitmq-cluster overlay swarm
配置文件
我将项目目录规划在/docker/rabbitmq中,先建立持久化存储目录/docker/rabbitmq/data
在manager上创建rabbitmq的配置文件路径:/docker/rabbitmq/rabbitmq.conf,内容如下:
loopback_users.guest = falselisteners.tcp.default = 5672hipe_compile = falsedefault_user = admindefault_pass = admincluster_formation.peer_discovery_backend = rabbit_peer_discovery_classic_configcluster_formation.classic_config.nodes.1 = rabbit@rabbit1cluster_formation.classic_config.nodes.2 = rabbit@rabbit2cluster_formation.classic_config.nodes.3 = rabbit@rabbit3cluster_formation.node_type = ram
后面的cluster_formation.node_type = ram表示创建的是ram集群,如果不写,就是默认的disc集群。
创建docker config
docker config create rabbitmq-config /docker/rabbitmq/rabbitmq.conf
创建部署文件,我将它放在/docker/swarm/rabbitmq_swarm.yml
version: "3.7"services: rabbit1: image: rabbitmq:3.8.2-management hostname: rabbit1 environment: RABBITMQ_ERLANG_COOKIE: "liaoxingchen_test" volumes: - "/docker/rabbitmq/data:/var/lib/rabbitmq" configs: - source: rabbitmq-config target: /etc/rabbitmq/rabbitmq.conf ports: - "4369:4369" - "5671:5671" - "5672:5672" - "15671:15671" - "15672:15672" - "25672:25672" deploy: replicas: 1 placement: constraints: - node.hostname==test01 rabbit2: image: rabbitmq:3.8.2-management hostname: rabbit2 depends_on: - rabbit1 environment: RABBITMQ_ERLANG_COOKIE: "liaoxingchen_test" volumes: - "/docker/rabbitmq/data:/var/lib/rabbitmq" configs: - source: rabbitmq-config target: /etc/rabbitmq/rabbitmq.conf deploy: replicas: 1 placement: constraints: - node.hostname==test02 rabbit3: image: rabbitmq:3.8.2-management hostname: rabbit3 depends_on: - rabbit1 environment: RABBITMQ_ERLANG_COOKIE: "liaoxingchen_test" volumes: - "/docker/rabbitmq/data:/var/lib/rabbitmq" configs: - source: rabbitmq-config target: /etc/rabbitmq/rabbitmq.conf deploy: replicas: 1 placement: constraints: - node.hostname==test03networks: default: external: name: rabbitmq-clusterconfigs: rabbitmq-config: external: true
其中,RABBITMQERLANGCOOKIE,可以自定义,只需要保证3个service一样,即可。
启动集群
在test01上使用如下命令启动集群:
docker stack deploy -c /docker/swarm/rabbitmq_swarm.yml rabbitmq
此时,你可以使用任何一个节点的15672端口,来访问Rabbitmq集群
无配置文件
如果不带配置文件,也可启动集群后,手动配置。
将yaml文件中configs:相关内容去掉,再启动集群,然后进入test02和test03,进入容器,输入如下命令:
rabbitmqctl stop_apprabbitmqctl resetrabbitmqctl join_cluster --ram rabbit@rabbit1rabbitmqctl start_app
效果是一样的,而且也会持久化到映射的data目录中。
补充
查看集群状态
docker service ls
ID NAME MODE REPLICAS IMAGE PORTS8rlyppvidvjy rabbit_rabbit1 replicated 1/1 rabbitmq:3.8.2-management :4369->4369/tcp,:5671-5672->5671-5672/tcp, :15671-15672->15671-15672/tcp, :25672->25672/tcpjicd2n9dh0rd rabbit_rabbit2 replicated 1/1 rabbitmq:3.8.2-managementms4h7ljutm5n rabbit_rabbit3 replicated 1/1 rabbitmq:3.8.2-management
查看某一个service的状态,如,rabbit1
docker service ps rabbit_rabbit1
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTSsdih8hbhadfc rabbit_rabbit1.1 rabbitmq:3.8.2-management test01 Running Running 2 hours ago
删除服务,你可以挨个删除service,因为你同时启动了3个service,
docker service rm rabbit_rabbit1docker service rm rabbit_rabbit2docker service rm rabbit_rabbit3
也可以利用stack同时删除
docker stack rm rabbit
番外补充
Spring Boot 应用连接 RabbitMQ 集群配置
spring.rabbitmq.addresses=node1:5672,node2:5672,node3:5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
相关文章
- 05-10docker搭建clickhouse集群【笔记】
- 05-10Docker搭建跨主机PXC集群
- 05-10SpringCloud微服务实战——搭建企业级开发框架(三十四):SpringCloud + Docker + k8s实现微服务集群打包部署-打包配置
- 05-10基于docker搭建hadoop集群
- 05-10第 7 章 多主机管理 - 045 - 安装 Docker Machine
- 05-10基于Docker搭建大数据集群(六)Hive搭建
- 05-10RabbitMQ镜像集群搭建
- 05-10rabbitmq集群的搭建
- 05-10RabbitMQ系列(五)使用Docker部署RabbitMQ集群
- 05-10win10下用Docker Desktop搭建zookeeper集群