温馨提示×

Debian RabbitMQ如何进行集群搭建

小樊
45
2025-11-09 17:16:50
栏目: 智能运维

Prerequisites
Before starting, ensure all Debian nodes have static IP addresses and hostname resolution (via /etc/hosts or DNS). For example, add entries like 192.168.1.101 node1 and 192.168.1.102 node2 to /etc/hosts on all nodes and run sudo systemctl restart networking to apply changes.

Step 1: Install Erlang (Required Runtime)
RabbitMQ is built with Erlang, so all nodes need a compatible Erlang/OTP version. On Debian, run:

sudo apt-get update
sudo apt-get install -y erlang-base erlang-asn1 erlang-crypto erlang-eldap erlang-ftp erlang-inets erlang-mnesia erlang-os-mon erlang-parsetools erlang-public-key erlang-runtime-tools erlang-snmp erlang-ssl erlang-syntax-tools erlang-tftp erlang-tools erlang-xmerl

Verify installation with erl -version (should show OTP version ≥ 23).

Step 2: Add RabbitMQ Official Repository & Install
For the latest stable version, add RabbitMQ’s official GPG key and repository:

curl -sLf "https://keys.openpgp.org/vks/v1/by-fingerprint/0A9AF2115F4687BD29803A206B73A36E6026DFCA" | sudo gpg --dearmor | sudo tee /usr/share/keyrings/com.rabbitmq.team.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/com.rabbitmq.team.gpg] http://ppa.launchpad.net/rabbitmq/rabbitmq-erlang/ubuntu $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/rabbitmq.list
echo "deb [signed-by=/usr/share/keyrings/com.rabbitmq.team.gpg] http://ppa.launchpad.net/rabbitmq/rabbitmq-server/ubuntu $(lsb_release -cs) main" | sudo tee -a /etc/apt/sources.list.d/rabbitmq.list
sudo apt-get update
sudo apt-get install -y rabbitmq-server

Start the service and enable auto-start:

sudo systemctl start rabbitmq-server
sudo systemctl enable rabbitmq-server

Step 3: Configure Erlang Cookie (Critical for Node Trust)
RabbitMQ uses the .erlang.cookie file (in /var/lib/rabbitmq/) for node authentication. All nodes must have identical cookie files with 400 permissions.
On the primary node (e.g., node1), copy the cookie to other nodes (e.g., node2):

sudo scp /var/lib/rabbitmq/.erlang.cookie user@node2:/var/lib/rabbitmq/

On each secondary node, set correct permissions:

sudo chmod 400 /var/lib/rabbitmq/.erlang.cookie
sudo chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie

Restart RabbitMQ on all nodes to apply changes:

sudo systemctl restart rabbitmq-server

Step 4: Start Cluster Formation
Choose one node as the primary (e.g., node1). On all other nodes, execute these commands to join the cluster:

sudo rabbitmqctl stop_app
sudo rabbitmqctl reset
sudo rabbitmqctl join_cluster rabbit@node1  # Replace 'node1' with primary node's hostname
sudo rabbitmqctl start_app
  • stop_app: Stops the RabbitMQ application.
  • reset: Clears local node data (only needed for initial cluster setup).
  • join_cluster: Connects to the primary node (specified by rabbit@hostname).
  • start_app: Restarts the application to join the cluster.

Step 5: Verify Cluster Status
On any node, run:

sudo rabbitmqctl cluster_status

Expected output shows all joined nodes (e.g., disc for disk-based nodes, ram for in-memory nodes) and their status (running). Example:

Cluster status of node 'rabbit@node1' ...
[{nodes,[{disc,['rabbit@node1','rabbit@node2']}]},
 {running_nodes,['rabbit@node2','rabbit@node1']},
 {cluster_name,<<"rabbit@node1">>},
 {partitions,[]}]

Optional: Enable Management Plugin (Web UI)
For easy monitoring, enable the management plugin on all nodes:

sudo rabbitmq-plugins enable rabbitmq_management

Access the web interface at http://<node-ip>:15672 (default credentials: guest/guest; change these for production).

Optional: Configure Queue Mirroring (High Availability)
To ensure messages survive node failures, set up queue mirroring. On any node, create a policy to mirror all queues to all nodes:

sudo rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}'
  • ha-mode":"all": Mirrors queues to all nodes.
  • ha-sync-mode":"automatic": Automatically syncs messages across mirrors.
    Adjust the policy (e.g., ha-mode":"exactly","ha-params":2) for specific durability needs.

Troubleshooting Tips

  • Cookie Mismatch: Ensure all nodes have identical .erlang.cookie files (permissions: 400, owner: rabbitmq).
  • Hostname Resolution: Verify nodes can ping each other by hostname (check /etc/hosts).
  • Port Blocking: Open ports 5672 (AMQP), 15672 (management) in your firewall (e.g., sudo ufw allow 5672/tcp; sudo ufw allow 15672/tcp).

0