1 Server basic information
server ip | docker container name |
---|---|
172.21.16.4 | mq_document_1 |
172.21.16.6 | mq_document_2 |
172.21.16.9 | mq_document_3 |
Target:
- container
mq_document_1
and containermq_document_3
join to containermq_document_2
2 Arranging files
The directory structure is as follows:
mq
|-- data
|-- docker-compose.yaml
`-- rabbitmq.conf
The docker-compose.yaml configuration file is as follows:
the three configuration files are consistent, just use different names
version: "3"
services:
mq:
image: rabbitmq:management
container_name: mq_document_1
hostname: mq_document_1
environment:
RABBITMQ_DEFAULT_USER: xxx
RABBITMQ_DEFAULT_PASS: xxx
RABBITMQ_ERLANG_COOKIE: rabbitcookie
RABBITMQ_CONFIG_FILE: /etc/rabbitmq/rabbitmq.conf
extra_hosts:
- "mq_document_1:172.21.16.4"
- "mq_document_2:172.21.16.6"
- "mq_document_3:172.21.16.9"
restart: always
network_mode: host
volumes:
- ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
- ./data:/var/lib/rabbitmq
rabbitmq.conf
consumer_timeout = 21600000
listeners.tcp.default = 5673
management.tcp.port = 15673
3 Arrangement file explanation
In Docker Compose, use extra_hosts
the keyword to configure the host binding relationship, --add-host=mq_document_1:172.21.16.4
which has the same effect as the native command.
Use network_mode: host
the host network mode to share the network with the host.
The following are the default ports that mq needs to use:
4369/tcp, 5671-5672/tcp, 15671-15672/tcp, 15691-15692/tcp, 25672/tcp
You can also map ports by yourself. The disadvantage is that the configuration is complex and error-prone. RabbitMQ uses 5672 as the port of the AMQP protocol, 15672 as the management port of the web interface, 25672 as the port between Erlang distributed nodes, etc., and some other ports It needs to be modified through the configuration file.
4 Start and join the node
The three servers are executed separatelydocker-compose up -d
Then operate on the server where mq_document_1
and mq_document_3
are located, the steps are exactly the same:
-
into the container
docker-compose exec -it mq bash
-
Close the RabbitMQ service
rabbitmqctl stop_app
Prompt that the environment variable RABBITMQ_ERLANG_COOKIE is outdated, don’t worry about it, manual configuration is more troublesome, just use it
root@mq_document_1:/# 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@mq_document_1 ...
-
reset
rabbitmqctl reset
-
join node 2
rabbitmqctl join_cluster rabbit@mq_document_2
The following log has been added successfully. The error means that the current node has not been started. Follow the steps below to start it.
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@mq_document_1 with rabbit@mq_document_2 07:53:44.737 [warn] Feature flags: the previous instance of this node must have failed to write the `feature_flags` file at `/var/lib/rabbitmq/mnesia/rabbit@mq_document_1-feature_flags`: 07:53:44.737 [warn] Feature flags: - list of previously disabled feature flags now marked as such: [:maintenance_mode_status] 07:53:44.812 [warn] Feature flags: the previous instance of this node must have failed to write the `feature_flags` file at `/var/lib/rabbitmq/mnesia/rabbit@mq_document_1-feature_flags`: 07:53:44.812 [warn] Feature flags: - list of previously enabled feature flags now marked as such: [:maintenance_mode_status] 07:53:44.822 [error] Failed to create a tracked connection table for node :rabbit@mq_document_1: { :node_not_running, :rabbit@mq_document_1} 07:53:44.822 [error] Failed to create a per-vhost tracked connection table for node :rabbit@mq_document_1: { :node_not_running, :rabbit@mq_document_1} 07:53:44.822 [error] Failed to create a per-user tracked connection table for node :rabbit@mq_document_1: { :node_not_running, :rabbit@mq_document_1}
-
Start application service
rabbitmqctl start_app
5 Configure mirror queue rules
rabbitmq management interface Admin->Policies
Add the following rules:
Name: document-mirrors
Pattern: .*(?=-document-queue)
Definition: ha-mode=all
ha-sync-mode=automatic