docker-compose部署rabbitmq集群
单机上部署rabbitmq集群:
下载rabbitmq镜像,使用的是3.7-management版本
docker pull rabbitmq:3.7-management
rabbitmq-compose.yml文件如下:
version: '3'
services:
rabbitmq1:
image: rabbitmq:3.7-management
deploy:
resources:
limits:
cpus: '2'
memory: 8G
restart_policy:
condition: any
ports:
- "15672:15672"
- "5672:5672"
hostname: rabbitmq1
container_name: rabbitmq1
environment:
- RABBITMQ_ERLANG_COOKIE=rabbitcookie
volumes:
- /data/docker/data/rabbitmq/rabbitmq1:/var/lib/rabbitmq
- /etc/localtime:/etc/localtime
rabbitmq2:
image: rabbitmq:3.7-management
deploy:
resources:
limits:
cpus: '2'
memory: 8G
restart_policy:
condition: any
ports:
- "5673:5672"
hostname: rabbitmq2
container_name: rabbitmq2
environment:
- RABBITMQ_ERLANG_COOKIE=rabbitcookie
volumes:
- /data/docker/data/rabbitmq/rabbitmq2:/var/lib/rabbitmq
- /etc/localtime:/etc/localtime
rabbitmq3:
image: rabbitmq:3.7-management
deploy:
resources:
limits:
cpus: '2'
memory: 8G
restart_policy:
condition: any
ports:
- "5674:5672"
hostname: rabbitmq3
container_name: rabbitmq3
environment:
- RABBITMQ_ERLANG_COOKIE=rabbitcookie
volumes:
- /data/docker/data/rabbitmq/rabbitmq3:/var/lib/rabbitmq
- /etc/localtime:/etc/localtime
启动rabbitmq容器:
docker-compose --compatibility -f rabbitmq-compose.yml up -d
容器启动成功后,进行集群搭建,此次使用的1个disc节点,2个ram节点:
初始化init_rabbitmq.sh脚本如下,脚本中进行普通集群构建,创建用户,设置用户tag以及用户权限设置。
#!/bin/bash #reset first node echo "Reset first rabbitmq node." docker exec rabbitmq1 /bin/bash -c 'rabbitmqctl stop_app' docker exec rabbitmq1 /bin/bash -c 'rabbitmqctl reset' docker exec rabbitmq1 /bin/bash -c 'rabbitmqctl start_app' #build cluster echo "Starting to build rabbitmq cluster with two ram nodes." docker exec rabbitmq2 /bin/bash -c 'rabbitmqctl stop_app' docker exec rabbitmq2 /bin/bash -c 'rabbitmqctl reset' docker exec rabbitmq2 /bin/bash -c 'rabbitmqctl join_cluster --ram rabbit@rabbitmq1' docker exec rabbitmq2 /bin/bash -c 'rabbitmqctl start_app' docker exec rabbitmq3 /bin/bash -c 'rabbitmqctl stop_app' docker exec rabbitmq3 /bin/bash -c 'rabbitmqctl reset' docker exec rabbitmq3 /bin/bash -c 'rabbitmqctl join_cluster --ram rabbit@rabbitmq1' docker exec rabbitmq3 /bin/bash -c 'rabbitmqctl start_app' #check cluster status echo "Check cluster status:" docker exec rabbitmq1 /bin/bash -c 'rabbitmqctl cluster_status' docker exec rabbitmq2 /bin/bash -c 'rabbitmqctl cluster_status' docker exec rabbitmq3 /bin/bash -c 'rabbitmqctl cluster_status' echo "Starting to create user." docker exec rabbitmq1 /bin/bash -c 'rabbitmqctl add_user admin admin@123' echo "Set tags for new user." docker exec rabbitmq1 /bin/bash -c 'rabbitmqctl set_user_tags admin administrator' echo "Grant permissions to new user." docker exec rabbitmq1 /bin/bash -c "rabbitmqctl set_permissions -p '/' admin '.*' '.*' '.*'"
执行初始化脚本:
sh init_rabbitmq.sh
登录管理界面查看集群状态:
http://xxx.xxx.xxx.xxx:15672/#/
登录账号:admin/admin@123,或者默认账号guest/guest
设置镜像队列构建高可用集群:
管理界面操作:
查看队列特性:
后台命令操作:
docker exec -it rabbitmq1 /bin/bash root@rabbitmq1:/# rabbitmqctl set_policy test "[a-zA-Z]" '{"ha-mode":"all","ha-sync-mode":"automatic"}'