一、集群的安装部署
我们还是利用docker来安装RabbitMQ集群。3分钟安装一个集群,开始。
前提条件,docker安装了docker-compose。如果没安装的话,参考这里
docker-compose文件参考bitnami官网:https://github.com/bitnami/containers/tree/main/bitnami/rabbitmq#setting-up-a-cluster
这是最简单的方式安装一个RabbitMQ集群
docker-compose.yml如下:
version: '2'services:stats:image: bitnami/rabbitmqenvironment:- RABBITMQ_NODE_TYPE=stats- RABBITMQ_NODE_NAME=rabbit@stats- RABBITMQ_ERL_COOKIE=s3cr3tc00ki3ports:- '15672:15672'volumes:- 'rabbitmqstats_data:/bitnami/rabbitmq/mnesia'queue-disc1:image: bitnami/rabbitmqenvironment:- RABBITMQ_NODE_TYPE=queue-disc- RABBITMQ_NODE_NAME=rabbit@queue-disc1- RABBITMQ_CLUSTER_NODE_NAME=rabbit@stats- RABBITMQ_ERL_COOKIE=s3cr3tc00ki3volumes:- 'rabbitmqdisc1_data:/bitnami/rabbitmq/mnesia'queue-ram1:image: bitnami/rabbitmqenvironment:- RABBITMQ_NODE_TYPE=queue-ram- RABBITMQ_NODE_NAME=rabbit@queue-ram1- RABBITMQ_CLUSTER_NODE_NAME=rabbit@stats- RABBITMQ_ERL_COOKIE=s3cr3tc00ki3volumes:- 'rabbitmqram1_data:/bitnami/rabbitmq/mnesia'volumes:rabbitmqstats_data:driver: localrabbitmqdisc1_data:driver: localrabbitmqram1_data:driver: local
新建docker-compose.yml
我新建了一个目录mycompose,进入到这个目录,然后用上面的yml新建一个文件
用cat命令,输出到docker-compose.yml
[root@localhost mycompose]# cat >> docker-compose.yml << EOF
> version: '2'
>
> services:
> stats:
> image: bitnami/rabbitmq
> environment:
> - RABBITMQ_NODE_TYPE=stats
> - RABBITMQ_NODE_NAME=rabbit@stats
> - RABBITMQ_ERL_COOKIE=s3cr3tc00ki3
> ports:
> - '15672:15672'
> volumes:
> - 'rabbitmqstats_data:/bitnami/rabbitmq/mnesia'
> queue-disc1:
> image: bitnami/rabbitmq
> environment:
> - RABBITMQ_NODE_TYPE=queue-disc
> - RABBITMQ_NODE_NAME=rabbit@queue-disc1
> - RABBITMQ_CLUSTER_NODE_NAME=rabbit@stats
> - RABBITMQ_ERL_COOKIE=s3cr3tc00ki3
> volumes:
> - 'rabbitmqdisc1_data:/bitnami/rabbitmq/mnesia'
> queue-ram1:
> image: bitnami/rabbitmq
> environment:
> - RABBITMQ_NODE_TYPE=queue-ram
> - RABBITMQ_NODE_NAME=rabbit@queue-ram1
> - RABBITMQ_CLUSTER_NODE_NAME=rabbit@stats
> - RABBITMQ_ERL_COOKIE=s3cr3tc00ki3
> volumes:
> - 'rabbitmqram1_data:/bitnami/rabbitmq/mnesia'
>
> volumes:
> rabbitmqstats_data:
> driver: local
> rabbitmqdisc1_data:
> driver: local
> rabbitmqram1_data:
> driver: local
> EOF
[root@localhost mycompose]# ls -al
总用量 4
drwxr-xr-x. 2 root root 32 9月 24 21:26 .
dr-xr-x---. 6 root root 254 9月 24 21:23 ..
-rw-r--r--. 1 root root 1017 9月 24 21:26 docker-compose.yml
查看文件,建好了
[root@localhost mycompose]# ls -al
总用量 4
drwxr-xr-x. 2 root root 32 9月 24 21:26 .
dr-xr-x---. 6 root root 254 9月 24 21:23 ..
-rw-r--r--. 1 root root 1017 9月 24 21:26 docker-compose.yml
在当前目录执行docker-compose up
[root@localhost mycompose]# docker-compose up
执行效果如下:
启动完成后,发现确实是启了三个RabbitMQ的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8abe421a79a bitnami/rabbitmq "/opt/bitnami/script…" 2 minutes ago Up 2 minutes 4369/tcp, 5551-5552/tcp, 5671-5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp, :::15672->15672/tcp mycompose-stats-1
d797db9bbbbb bitnami/rabbitmq "/opt/bitnami/script…" 2 minutes ago Up 2 minutes 4369/tcp, 5551-5552/tcp, 5671-5672/tcp, 15671-15672/tcp, 25672/tcp mycompose-queue-ram1-1
7ed4e7406553 bitnami/rabbitmq "/opt/bitnami/script…" 2 minutes ago Up 2 minutes 4369/tcp, 5551-5552/tcp, 5671-5672/tcp, 15671-15672/tcp, 25672/tcp mycompose-queue-disc1-1
进入其中一个容器
如上面所示,我们进入ID为a8abe421a79a的容器。
[root@localhost ~]# docker exec -it a8a /bin/bash
查看集群状态
I have no name!@a8abe421a79a:/$ rabbitmqctl cluster_status
Cluster status of node rabbit@stats ...
BasicsCluster name: rabbit@a8abe421a79aDisk Nodesrabbit@queue-disc1
rabbit@statsRAM Nodesrabbit@queue-ram1Running Nodesrabbit@queue-disc1
rabbit@queue-ram1
rabbit@statsVersionsrabbit@queue-disc1: RabbitMQ 3.9.11 on Erlang 24.2
rabbit@queue-ram1: RabbitMQ 3.9.11 on Erlang 24.2
rabbit@stats: RabbitMQ 3.9.11 on Erlang 24.2Maintenance statusNode: rabbit@queue-disc1, status: not under maintenance
Node: rabbit@queue-ram1, status: not under maintenance
Node: rabbit@stats, status: not under maintenanceAlarms(none)Network Partitions(none)ListenersNode: rabbit@queue-disc1, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@queue-disc1, interface: [::], port: 15692, protocol: http/prometheus, purpose: Prometheus exporter API over HTTP
Node: rabbit@queue-disc1, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@queue-ram1, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@queue-ram1, interface: [::], port: 15692, protocol: http/prometheus, purpose: Prometheus exporter API over HTTP
Node: rabbit@queue-ram1, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@stats, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@stats, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@stats, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0Feature flagsFlag: 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: stream_queue, state: enabled
Flag: user_limits, state: enabled
Flag: virtual_host_metadata, state: enabled
进入控制台:http://192.168.56.201:15672/#/
账号默认是:用户名:user,密码:bitnami
如果想停止当前集群,可以用docker-compose down命令。
常用命令如下
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
部分命令选项如下:
-f,–file:指定使用的 Compose 模板文件,默认为 docker-compose.yml,可以多次指定,指定多个 yml;
-p, --project-name:指定工程名称,默认使用 docker-compose.yml 文件所在目录的名称;
-v:打印版本并退出;
–log-level:定义日志等级(DEBUG, INFO, WARNING, ERROR, CRITICAL)。