RabbitMQ 是一个广泛使用的消息代理软件,它允许应用程序之间通过消息进行通信

RabbitMQ 集群指南

  1. 概述
    RabbitMQ 是一个广泛使用的消息代理软件,它允许应用程序之间通过消息进行通信。在分布式系统中,RabbitMQ 集群可以提供高可用性、可扩展性和容错性。通过 RabbitMQ 集群,多个节点可以协同工作,以分发和路由消息。

  2. RabbitMQ 节点和集群
    RabbitMQ 运行时由一个或多个节点组成,每个节点都维护着自己的消息队列集合。节点是通过节点名称唯一标识的。要形成 RabbitMQ 集群,多个节点需要相互连接并共享消息。

  3. 集群要求
    • 节点名称:每个节点都需要有一个唯一的名称。名称可以在节点启动时通过命令行参数指定,或者在运行时通过环境变量或配置文件进行设置。
    • 网络连接:集群中的节点需要能够相互通信。这通常意味着它们应该位于相同的局域网内,且网络配置应允许节点间的低延迟通信。
    • 共享存储:为了确保消息持久性,所有节点必须能够访问同一个消息存储。这通常通过共享的磁盘存储实现,如 NFS(网络文件系统)或本地磁盘。
    • 相同的 Erlang 版本:所有节点上的 Erlang 运行时环境版本必须相同,以确保节点间的兼容性。

  4. 集群优势
    • 高可用性:通过在多个节点上存储消息,RabbitMQ 集群可以在某个节点发生故障时自动将消息路由到其他可用节点。
    • 可扩展性:通过增加更多的节点,RabbitMQ 集群可以处理更多的并发连接和消息流量。
    • 容错性:由于消息在多个节点上存储,即使某些节点发生故障,其他节点仍然可以继续处理和路由消息。

  5. 集群配置
    配置 RabbitMQ 集群涉及到几个关键步骤:
    • 设置节点名称:为每个节点指定唯一的名称。
    • 配置 Erlang 网络:确保 Erlang 运行时环境正确配置以支持节点间的通信。
    • 共享存储配置:确保所有节点都可以访问相同的消息存储。
    • 启动节点并加入集群:按照 RabbitMQ 的文档启动每个节点,并将它们添加到集群中。

  6. 安全和认证
    在 RabbitMQ 集群中,为了安全地交换认证信息和其他管理任务,通常使用 Erlang cookie。这个 cookie 是一个共享的密钥,必须在所有节点上设置并保持一致。另外,可以使用用户名和密码进行更细粒度的访问控制和安全策略。

  7. 集群中的节点类型
    RabbitMQ 集群中有三种类型的节点:
    • 磁盘节点:这种节点类型在磁盘上存储消息,是生产者和消费者的默认节点类型。
    • RAM 节点:这种节点类型将所有队列和交换器存储在内存中,通常用于需要高吞吐量的场景。
    • 混合节点:同时包含磁盘和 RAM 节点的特性,用于需要平衡存储和性能的场景。

  8. 集群管理和监控
    为了确保 RabbitMQ 集群的正常运行,需要定期进行管理和监控。这包括:
    • 检查节点状态:通过 RabbitMQ 的管理插件,可以查看每个节点的状态、资源使用情况等。
    • 监控消息队列:监控队列的长度、消息的吞吐量等,以便及时发现性能问题或瓶颈。
    • 检查网络和存储健康状况:定期检查网络连接和共享存储的状态,确保它们正常运行。

  9. 故障转移和恢复
    在 RabbitMQ 集群中,为了应对节点故障,可以使用一些策略来自动转移工作负载。例如,使用代理的自动故障转移或使用负载均衡器来分发流量。在节点故障发生时,其他节点可以接管工作负载,确保服务的连续性。

  10. 总结
    RabbitMQ 集群通过增加可用性和可扩展性,提供了一种可靠的方式来处理大量消息。正确配置和监控 RabbitMQ 集群对于确保消息的可靠传递和系统的稳定性至关重要。了解集群的原理、配置和最佳实践是构建高效、可靠的分布式系统的基础。
    Clustering Guide
    RabbitMQ

    Features
    Get Started
    Support
    Community
    Docs
    Blog

Overview

This guide covers fundamental topics related to RabbitMQ clustering:

How RabbitMQ nodes are identified: node names
Requirements for clustering
What data is and isn't replicated between cluster nodes
What clustering means for clients
How clusters are formed
How nodes authenticate to each other (and with CLI tools)
Why two cluster nodes are highly recommended against
Node restarts and how nodes rejoin their cluster
How to remove a cluster node
How to reset a cluster node

and more. Cluster Formation and Peer Discovery is a closely related guide that focuses on peer discovery and cluster formation automation-related topics. For queue contents (message) replication, see the Mirrored Queues guide.

A RabbitMQ cluster is a logical grouping of one or several nodes, each sharing users, virtual hosts, queues, exchanges, bindings, runtime parameters and other distributed state.
Cluster Formation
Ways of Forming a Cluster

A RabbitMQ cluster can formed in a number of ways:

Declaratively by listing cluster nodes in config file
Declaratively using DNS-based discovery
Declaratively using AWS (EC2) instance discovery (via a plugin)
Declaratively using Kubernetes discovery (via a plugin)
Declaratively using Consul-based discovery (via a plugin)
Declaratively using etcd-based discovery (via a plugin)
Manually with rabbitmqctl

Please refer to the Cluster Formation guide for details.

The composition of a cluster can be altered dynamically. All RabbitMQ brokers start out as running on a single node. These nodes can be joined into clusters, and subsequently turned back into individual brokers again.
Node Names (Identifiers)

RabbitMQ nodes are identified by node names. A node name consists of two parts, a prefix (usually rabbit) and hostname. For example, rabbit@node1.messaging.svc.local is a node name with the prefix of rabbit and hostname of node1.messaging.svc.local.

Node names in a cluster must be unique. If more than one node is running on a given host (this is usually the case in development and QA environments), they must use different prefixes, e.g. rabbit1@hostname and rabbit2@hostname.

In a cluster, nodes identify and contact each other using node names. This means that the hostname part of every node name must resolve. CLI tools also identify and address nodes using node names.

When a node starts up, it checks whether it has been assigned a node name. This is done via the RABBITMQ_NODENAME environment variable. If no value was explicitly configured, the node resolves its hostname and prepends rabbit to it to compute its node name.

If a system uses fully qualified domain names (FQDNs) for hostnames, RabbitMQ nodes and CLI tools must be configured to use so called long node names. For server nodes this is done by setting the RABBITMQ_USE_LONGNAME environment variable to true.

For CLI tools, either RABBITMQ_USE_LONGNAME must be set or the --longnames option must be specified.
Cluster Formation Requirements
Hostname Resolution

RabbitMQ nodes address each other using domain names, either short or fully-qualified (FQDNs). Therefore hostnames of all cluster members must be resolvable from all cluster nodes, as well as machines on which command line tools such as rabbitmqctl might be used.

Hostname resolution can use any of the standard OS-provided methods:

DNS records
Local host files (e.g. /etc/hosts)

In more restrictive environments, where DNS record or hosts file modification is restricted, impossible or undesired, Erlang VM can be configured to use alternative hostname resolution methods, such as an alternative DNS server, a local file, a non-standard hosts file location, or a mix of methods. Those methods can work in concert with the standard OS hostname resolution methods.

To use FQDNs, see RABBITMQ_USE_LONGNAME in the Configuration guide. See Node Names above.
Port Access

RabbitMQ nodes bind to ports (open server TCP sockets) in order to accept client and CLI tool connections. Other processes and tools such as SELinux may prevent RabbitMQ from binding to a port. When that happens, the node will fail to start.

CLI tools, client libraries and RabbitMQ nodes also open connections (client TCP sockets). Firewalls can prevent nodes and CLI tools from communicating with each other. Make sure the following ports are accessible:

4369: epmd, a helper discovery daemon used by RabbitMQ nodes and CLI tools
5672, 5671: used by AMQP 0-9-1 and 1.0 clients without and with TLS
25672: used for inter-node and CLI tools communication (Erlang distribution server port) and is allocated from a dynamic range (limited to a single port by default, computed as AMQP port + 20000). Unless external connections on these ports are really necessary (e.g. the cluster uses federation or CLI tools are used on machines outside the subnet), these ports should not be publicly exposed. See networking guide for details.
35672-35682: used by CLI tools (Erlang distribution client ports) for communication with nodes and is allocated from a dynamic range (computed as server distribution port + 10000 through server distribution port + 10010). See networking guide for details.
15672: HTTP API clients, management UI and rabbitmqadmin (only if the management plugin is enabled)
61613, 61614: STOMP clients without and with TLS (only if the STOMP plugin is enabled)
1883, 8883: (MQTT clients without and with TLS, if the MQTT plugin is enabled
15674: STOMP-over-WebSockets clients (only if the Web STOMP plugin is enabled)
15675: MQTT-over-WebSockets clients (only if the Web MQTT plugin is enabled)
15692: Prometheus metrics (only if the Prometheus plugin is enabled)

It is possible to configure RabbitMQ to use different ports and specific network interfaces.
Nodes in a Cluster
What is Replicated?

All data/state required for the operation of a RabbitMQ broker is replicated across all nodes. An exception to this are message queues, which by default reside on one node, though they are visible and reachable from all nodes. To replicate queues across nodes in a cluster, see the documentation on high availability (note: this guide is a prerequisite for mirroring).
Nodes are Equal Peers

Some distributed systems have leader and follower nodes. This is generally not true for RabbitMQ. All nodes in a RabbitMQ cluster are equal peers: there are no special nodes in RabbitMQ core. This topic becomes more nuanced when queue mirroring and plugins are taken into consideration but for most intents and purposes, all cluster nodes should be considered equal

Many CLI tool operations can be executed against any node. An HTTP API client can target any cluster node.

Individual plugins can designate (elect) certain nodes to be “special” for a period of time. For example, federation links are colocated on a particular cluster node. Should that node fail, the links will be restarted on a different node.

In versions older than 3.6.7, RabbitMQ management plugin used a dedicated node for stats collection and aggregation.
How CLI Tools Authenticate to Nodes (and Nodes to Each Other): the Erlang Cookie

RabbitMQ nodes and CLI tools (e.g. rabbitmqctl) use a cookie to determine whether they are allowed to communicate with each other. For two nodes to be able to communicate they must have the same shared secret called the Erlang cookie. The cookie is just a string of alphanumeric characters up to 255 characters in size. It is usually stored in a local file. The file must be only accessible to the owner (e.g. have UNIX permissions of 600 or similar). Every cluster node must have the same cookie.

If the file does not exist, Erlang VM will try to create one with a randomly generated value when the RabbitMQ server starts up. Using such generated cookie files are appropriate in development environments only. Since each node will generate its own value independently, this strategy is not really viable in a clustered environment.

Erlang cookie generation should be done at cluster deployment stage, ideally using automation and orchestration tools such as Chef, Puppet, BOSH, Docker or similar.
Cookie File Locations
Linux, MacOS, *BSD

On UNIX systems, the cookie will be typically located in /var/lib/rabbitmq/.erlang.cookie (used by the server) and $HOME/.erlang.cookie (used by CLI tools). Note that since the value of $HOME varies from user to user, it’s necessary to place a copy of the cookie file for each user that will be using the CLI tools. This applies to both non-privileged users and root.
Windows

On Windows, the cookie location depends on a few factors:

Erlang version: prior to 20.2 or 20.2 and later
Whether the HOMEDRIVE and HOMEPATH environment variables are both set

Erlang 20.2 or later

With Erlang versions starting with 20.2, the cookie file locations are:

%HOMEDRIVE%%HOMEPATH%\.erlang.cookie (usually C:\Users\%USERNAME%\.erlang.cookie for user %USERNAME%) if both the HOMEDRIVE and HOMEPATH environment variables are set
%USERPROFILE%\.erlang.cookie (usually C:\Users\%USERNAME%\.erlang.cookie) if HOMEDRIVE and HOMEPATH are not both set
For the RabbitMQ Windows service - %USERPROFILE%\.erlang.cookie (usually C:\WINDOWS\system32\config\systemprofile)

If the Windows service is used, the cookie should be copied from C:\Windows\system32\config\systemprofile.erlang.cookie to the expected location for users running commands like rabbitmqctl.bat.
Erlang 19.3 through 20.2

With Erlang versions prior to 20.2, the cookie file locations are:

%HOMEDRIVE%%HOMEPATH%\.erlang.cookie (usually C:\Users\%USERNAME%\.erlang.cookie for user %USERNAME%) if both the HOMEDRIVE and HOMEPATH environment variables are set
%USERPROFILE%\.erlang.cookie (usually C:\Users\%USERNAME%\.erlang.cookie) if HOMEDRIVE and HOMEPATH are not both set
For the RabbitMQ Windows service - %WINDIR%\.erlang.cookie (usually C:\Windows\.erlang.cookie)

If the Windows service is used, the cookie should be copied from C:\Windows.erlang.cookie to the expected location for users running commands like rabbitmqctl.bat.
Troubleshooting

When a node starts, it will log its home (base) directory location. Unless any server directories were overridden, that’s the directory the cookie file will be created in by the RabbitMQ service.
Runtime Arguments

As an alternative, you can add the option “-setcookie value” in the RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS environment variable value:

RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS=“-setcookie cookie-value”

This is the least secure option and generally not recommended.
Authentication Failures

When the cookie is misconfigured (for example, not identical), RabbitMQ will log errors such as “Connection attempt from disallowed node” and “Could not auto-cluster”. When a CLI tool such as rabbitmqctl fails to authenticate with RabbitMQ, the message usually says

  • epmd reports node ‘rabbit’ running on port 25672
  • TCP connection succeeded but Erlang distribution failed
  • suggestion: hostname mismatch?
  • suggestion: is the cookie set correctly?
  • suggestion: is the Erlang distribution using TLS?

An incorrectly placed cookie file or cookie value mismatch are most common scenarios for such failures.

When a recent Erlang/OTP version is used, authentication failures contain more information and cookie mismatches can be identified better:

  • connected to epmd (port 4369) on warp10

  • epmd reports node ‘rabbit’ running on port 25672

  • TCP connection succeeded but Erlang distribution failed

  • Authentication failed (rejected by the remote node), please check the Erlang cookie

See the CLI Tools guide for more information.
Node Counts and Quorum

Because several features (e.g. quorum queues, client tracking in MQTT) require a consensus between cluster members, odd numbers of cluster nodes are highly recommended: 1, 3, 5, 7 and so on.

Two node clusters are highly recommended against since it’s impossible for cluster nodes to identify a majority and form a consensus in case of connectivity loss. For example, when the two nodes lose connectivity MQTT client connections won’t be accepted, quorum queues would lose their availability, and so on.

From the consensus point of view, Four or six node clusters would have the same availability characteristics as three and five node clusters.

The Quorum queue guide covers this topic in more detail.
Clustering and Clients

Assuming all cluster members are available, a client can connect to any node and perform any operation. Nodes will route operations to the queue master node transparently to clients.

With all supported messaging protocols a client is only connected to one node at a time.

In case of a node failure, clients should be able to reconnect to a different node, recover their topology and continue operation. For this reason, most client libraries accept a list of endpoints (hostnames or IP addresses) as a connection option. The list of hosts will be used during initial connection as well as connection recovery, if the client supports it. See documentation guides for individual clients to learn more.

There are scenarios where it may not be possible for a client to transparently continue operations after connecting to a different node. They usually involve non-mirrored queues hosted on a failed node.
Clustering and Observability

Client connections, channels and queues will be distributed across cluster nodes. Operators need to be able to inspect and monitor such resources across all cluster nodes.

RabbitMQ CLI tools such as rabbitmq-diagnostics and rabbitmqctl provide commands that inspect resources and cluster-wide state. Some commands focus on the state of a single node (e.g. rabbitmq-diagnostics environment and rabbitmq-diagnostics status), others inspect cluster-wide state. Some examples of the latter include rabbitmqctl list_connections, rabbitmqctl list_mqtt_connections, rabbitmqctl list_stomp_connections, rabbitmqctl list_users, rabbitmqctl list_vhosts and so on.

Such “cluster-wide” commands will often contact one node first, discover cluster members and contact them all to retrieve and combine their respective state. For example, rabbitmqctl list_connections will contact all nodes, retrieve their AMQP 0-9-1 and AMQP 1.0 connections, and display them all to the user. The user doesn’t have to manually contact all nodes. Assuming a non-changing state of the cluster (e.g. no connections are closed or opened), two CLI commands executed against two different nodes one after another will produce identical or semantically identical results. “Node-local” commands, however, will not produce identical results since two nodes rarely have identical state: at the very least their node names will be different!

Management UI works similarly: a node that has to respond to an HTTP API request will fan out to other cluster members and aggregate their responses. In a cluster with multiple nodes that have management plugin enabled, the operator can use any node to access management UI. The same goes for monitoring tools that use the HTTP API to collect data about the state of the cluster. There is no need to issue a request to every cluster node in turn.
Node Failure Handling

RabbitMQ brokers tolerate the failure of individual nodes. Nodes can be started and stopped at will, as long as they can contact a cluster member node known at the time of shutdown.

Queue mirroring allows queue contents to be replicated across multiple cluster nodes.

Non-mirrored queues can also be used in clusters. Non-mirrored queue behaviour in case of node failure depends on queue durability.

RabbitMQ clustering has several modes of dealing with network partitions, primarily consistency oriented. Clustering is meant to be used across LAN. It is not recommended to run clusters that span WAN. The Shovel or Federation plugins are better solutions for connecting brokers across a WAN. Note that Shovel and Federation are not equivalent to clustering.
Metrics and Statistics

Every node stores and aggregates its own metrics and stats, and provides an API for other nodes to access it. Some stats are cluster-wide, others are specific to individual nodes. Node that responds to an HTTP API request contacts its peers to retrieve their data and then produces an aggregated result.

In versions older than 3.6.7, RabbitMQ management plugin used a dedicated node for stats collection and aggregation.
Disk and RAM Nodes

A node can be a disk node or a RAM node. (Note: disk and disc are used interchangeably). RAM nodes store internal database tables in RAM only. This does not include messages, message store indices, queue indices and other node state.

In the vast majority of cases you want all your nodes to be disk nodes; RAM nodes are a special case that can be used to improve the performance clusters with high queue, exchange, or binding churn. RAM nodes do not provide higher message rates. When in doubt, use disk nodes only.

Since RAM nodes store internal database tables in RAM only, they must sync them from a peer node on startup. This means that a cluster must contain at least one disk node. It is therefore not possible to manually remove the last remaining disk node in a cluster.
Clustering Transcript with rabbitmqctl

The following is a transcript of manually setting up and manipulating a RabbitMQ cluster across three machines - rabbit1, rabbit2, rabbit3. It is recommended that the example is studied before more automation-friendly cluster formation options are used.

We assume that the user is logged into all three machines, that RabbitMQ has been installed on the machines, and that the rabbitmq-server and rabbitmqctl scripts are in the user’s PATH.

This transcript can be modified to run on a single host, as explained more details below.
Starting Independent Nodes

Clusters are set up by re-configuring existing RabbitMQ nodes into a cluster configuration. Hence the first step is to start RabbitMQ on all nodes in the normal way:

on rabbit1

rabbitmq-server -detached

on rabbit2

rabbitmq-server -detached

on rabbit3

rabbitmq-server -detached

This creates three independent RabbitMQ brokers, one on each node, as confirmed by the cluster_status command:

on rabbit1

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit1 …

=> [{nodes,[{disc,[rabbit@rabbit1]}]},{running_nodes,[rabbit@rabbit1]}]

=> …done.

on rabbit2

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit2 …

=> [{nodes,[{disc,[rabbit@rabbit2]}]},{running_nodes,[rabbit@rabbit2]}]

=> …done.

on rabbit3

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit3 …

=> [{nodes,[{disc,[rabbit@rabbit3]}]},{running_nodes,[rabbit@rabbit3]}]

=> …done.

The node name of a RabbitMQ broker started from the rabbitmq-server shell script is rabbit@shorthostname, where the short node name is lower-case (as in rabbit@rabbit1, above). On Windows, if rabbitmq-server.bat batch file is used, the short node name is upper-case (as in rabbit@RABBIT1). When you type node names, case matters, and these strings must match exactly.
Creating a Cluster

In order to link up our three nodes in a cluster, we tell two of the nodes, say rabbit@rabbit2 and rabbit@rabbit3, to join the cluster of the third, say rabbit@rabbit1. Prior to that both newly joining members must be reset.

We first join rabbit@rabbit2 in a cluster with rabbit@rabbit1. To do that, on rabbit@rabbit2 we stop the RabbitMQ application and join the rabbit@rabbit1 cluster, then restart the RabbitMQ application. Note that a node must be reset before it can join an existing cluster. Resetting the node removes all resources and data that were previously present on that node. This means that a node cannot be made a member of a cluster and keep its existing data at the same time. When that’s desired, using the Blue/Green deployment strategy or backup and restore are the available options.

on rabbit2

rabbitmqctl stop_app

=> Stopping node rabbit@rabbit2 …done.

rabbitmqctl reset

=> Resetting node rabbit@rabbit2 …

rabbitmqctl join_cluster rabbit@rabbit1

=> Clustering node rabbit@rabbit2 with [rabbit@rabbit1] …done.

rabbitmqctl start_app

=> Starting node rabbit@rabbit2 …done.

We can see that the two nodes are joined in a cluster by running the cluster_status command on either of the nodes:

on rabbit1

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit1 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2]}]},

=> {running_nodes,[rabbit@rabbit2,rabbit@rabbit1]}]

=> …done.

on rabbit2

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit2 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2]}]},

=> {running_nodes,[rabbit@rabbit1,rabbit@rabbit2]}]

=> …done.

Now we join rabbit@rabbit3 to the same cluster. The steps are identical to the ones above, except this time we’ll cluster to rabbit2 to demonstrate that the node chosen to cluster to does not matter - it is enough to provide one online node and the node will be clustered to the cluster that the specified node belongs to.

on rabbit3

rabbitmqctl stop_app

=> Stopping node rabbit@rabbit3 …done.

on rabbit3

rabbitmqctl reset

=> Resetting node rabbit@rabbit3 …

rabbitmqctl join_cluster rabbit@rabbit2

=> Clustering node rabbit@rabbit3 with rabbit@rabbit2 …done.

rabbitmqctl start_app

=> Starting node rabbit@rabbit3 …done.

We can see that the three nodes are joined in a cluster by running the cluster_status command on any of the nodes:

on rabbit1

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit1 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},

=> {running_nodes,[rabbit@rabbit3,rabbit@rabbit2,rabbit@rabbit1]}]

=> …done.

on rabbit2

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit2 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},

=> {running_nodes,[rabbit@rabbit3,rabbit@rabbit1,rabbit@rabbit2]}]

=> …done.

on rabbit3

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit3 …

=> [{nodes,[{disc,[rabbit@rabbit3,rabbit@rabbit2,rabbit@rabbit1]}]},

=> {running_nodes,[rabbit@rabbit2,rabbit@rabbit1,rabbit@rabbit3]}]

=> …done.

By following the above steps we can add new nodes to the cluster at any time, while the cluster is running.
Restarting Cluster Nodes

Nodes that have been joined to a cluster can be stopped at any time. They can also fail or be terminated by the OS. In all cases the rest of the cluster can continue operating, and the nodes automatically “catch up” with (sync from) the other cluster nodes when they start up again. Note that some partition handling strategies may work differently and affect other nodes.

We shut down the nodes rabbit@rabbit1 and rabbit@rabbit3 and check on the cluster status at each step:

on rabbit1

rabbitmqctl stop

=> Stopping and halting node rabbit@rabbit1 …done.

on rabbit2

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit2 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},

=> {running_nodes,[rabbit@rabbit3,rabbit@rabbit2]}]

=> …done.

on rabbit3

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit3 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},

=> {running_nodes,[rabbit@rabbit2,rabbit@rabbit3]}]

=> …done.

on rabbit3

rabbitmqctl stop

=> Stopping and halting node rabbit@rabbit3 …done.

on rabbit2

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit2 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},

=> {running_nodes,[rabbit@rabbit2]}]

=> …done.

Now we start the nodes again, checking on the cluster status as we go along:

on rabbit1

rabbitmq-server -detached
rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit1 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},

=> {running_nodes,[rabbit@rabbit2,rabbit@rabbit1]}]

=> …done.

on rabbit2

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit2 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},

=> {running_nodes,[rabbit@rabbit1,rabbit@rabbit2]}]

=> …done.

on rabbit3

rabbitmq-server -detached

on rabbit1

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit1 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},

=> {running_nodes,[rabbit@rabbit2,rabbit@rabbit1,rabbit@rabbit3]}]

=> …done.

on rabbit2

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit2 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},

=> {running_nodes,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]

=> …done.

on rabbit3

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit3 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2,rabbit@rabbit3]}]},

=> {running_nodes,[rabbit@rabbit2,rabbit@rabbit1,rabbit@rabbit3]}]

=> …done.

It is important to understand the process node go through when they are stopped and restarted.

A stopping node picks an online cluster member (only disc nodes will be considered) to sync with after restart. Upon restart the node will try to contact that peer 10 times by default, with 30 second response timeouts. In case the peer becomes available in that time interval, the node successfully starts, syncs what it needs from the peer and keeps going. If the peer does not become available, the restarted node will give up and voluntarily stop.

When a node has no online peers during shutdown, it will start without attempts to sync with any known peers. It does not start as a standalone node, however, and peers will be able to rejoin it.

A node rejoining after a node name or host name change can start as a blank node if its data directory path changes as a result. Such nodes will fail to rejoin the cluster. While the node is offline, its peers can be reset or started with a blank data directory. In that case the recovering node will fail to rejoin its peer as well since internal data store cluster identity would no longer match.

Consider the following scenario:

A cluster of 3 nodes, A, B and C is formed
Node A is shut down
Node B is reset
Node A is started
Node A tries to rejoin B but B's cluster identity has changed
Node B doesn't recognise A as a known cluster member because it's been reset

in this case node B will reject the clustering attempt from A with an appropriate error message in the log:

Node ‘rabbit@node1.local’ thinks it’s clustered with node ‘rabbit@node2.local’, but ‘rabbit@node2.local’ disagrees

In this case B can be reset again and then will be able to join A, or A can be reset and will successfully join B.

When the entire cluster is brought down therefore, the last node to go down is the only one that didn’t have any running peers at the time of shutdown. That node can start without contacting any peers first. Since nodes will try to contact a known peer for up to 5 minutes (by default), nodes can be restarted in any order in that period of time. In this case they will rejoin each other one by one successfully. This window of time can be adjusted using two configuration settings:

wait for 60 seconds instead of 30

mnesia_table_loading_retry_timeout = 60000

retry 15 times instead of 10

mnesia_table_loading_retry_limit = 15

By adjusting these settings and tweaking the time window in which known peer has to come back it is possible to account for cluster-wide redeployment scenarios that can be longer than 5 minutes to complete.

During upgrades, sometimes the last node to stop must be the first node to be started after the upgrade. That node will be designated to perform a cluster-wide schema migration that other nodes can sync from and apply when they rejoin.

In some cases the last node to go offline cannot be brought back up. It can be removed from the cluster using the forget_cluster_node rabbitmqctl command.

Alternatively force_boot rabbitmqctl command can be used on a node to make it boot without trying to sync with any peers (as if they were last to shut down). This is usually only necessary if the last node to shut down or a set of nodes will never be brought back online.
Breaking Up a Cluster

Sometimes it is necessary to remove a node from a cluster. The operator has to do this explicitly using a rabbitmqctl command.

Some peer discovery mechanisms support node health checks and forced removal of nodes not known to the discovery backend. That feature is opt-in (disabled by default).

We first remove rabbit@rabbit3 from the cluster, returning it to independent operation. To do that, on rabbit@rabbit3 we stop the RabbitMQ application, reset the node, and restart the RabbitMQ application.

on rabbit3

rabbitmqctl stop_app

=> Stopping node rabbit@rabbit3 …done.

rabbitmqctl reset

=> Resetting node rabbit@rabbit3 …done.

rabbitmqctl start_app

=> Starting node rabbit@rabbit3 …done.

Note that it would have been equally valid to list rabbit@rabbit3 as a node.

Running the cluster_status command on the nodes confirms that rabbit@rabbit3 now is no longer part of the cluster and operates independently:

on rabbit1

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit1 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2]}]},

=> {running_nodes,[rabbit@rabbit2,rabbit@rabbit1]}]

=> …done.

on rabbit2

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit2 …

=> [{nodes,[{disc,[rabbit@rabbit1,rabbit@rabbit2]}]},

=> {running_nodes,[rabbit@rabbit1,rabbit@rabbit2]}]

=> …done.

on rabbit3

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit3 …

=> [{nodes,[{disc,[rabbit@rabbit3]}]},{running_nodes,[rabbit@rabbit3]}]

=> …done.

We can also remove nodes remotely. This is useful, for example, when having to deal with an unresponsive node. We can for example remove rabbit@rabbi1 from rabbit@rabbit2.

on rabbit1

rabbitmqctl stop_app

=> Stopping node rabbit@rabbit1 …done.

on rabbit2

rabbitmqctl forget_cluster_node rabbit@rabbit1

=> Removing node rabbit@rabbit1 from cluster …

=> …done.

Note that rabbit1 still thinks its clustered with rabbit2, and trying to start it will result in an error. We will need to reset it to be able to start it again.

on rabbit1

rabbitmqctl start_app

=> Starting node rabbit@rabbit1 …

=> Error: inconsistent_cluster: Node rabbit@rabbit1 thinks it’s clustered with node rabbit@rabbit2, but rabbit@rabbit2 disagrees

rabbitmqctl reset

=> Resetting node rabbit@rabbit1 …done.

rabbitmqctl start_app

=> Starting node rabbit@rabbit1 …

=> …done.

The cluster_status command now shows all three nodes operating as independent RabbitMQ brokers:

on rabbit1

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit1 …

=> [{nodes,[{disc,[rabbit@rabbit1]}]},{running_nodes,[rabbit@rabbit1]}]

=> …done.

on rabbit2

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit2 …

=> [{nodes,[{disc,[rabbit@rabbit2]}]},{running_nodes,[rabbit@rabbit2]}]

=> …done.

on rabbit3

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit3 …

=> [{nodes,[{disc,[rabbit@rabbit3]}]},{running_nodes,[rabbit@rabbit3]}]

=> …done.

Note that rabbit@rabbit2 retains the residual state of the cluster, whereas rabbit@rabbit1 and rabbit@rabbit3 are freshly initialised RabbitMQ brokers. If we want to re-initialise rabbit@rabbit2 we follow the same steps as for the other nodes:

on rabbit2

rabbitmqctl stop_app

=> Stopping node rabbit@rabbit2 …done.

rabbitmqctl reset

=> Resetting node rabbit@rabbit2 …done.

rabbitmqctl start_app

=> Starting node rabbit@rabbit2 …done.

Besides rabbitmqctl forget_cluster_node and the automatic cleanup of unknown nodes by some peer discovery plugins, there are no scenarios in which a RabbitMQ node will permanently remove its peer node from a cluster.
How to Reset a Node

Sometimes it may be necessary to reset a node (wipe all of its data) and later make it rejoin the cluster. Generally speaking, there are two possible scenarios: when the node is running, and when the node cannot start or won’t respond to CLI tool commands e.g. due to an issue such as ERL-430.

Resetting a node will delete all of its data, cluster membership information, configured runtime parameters, users, virtual hosts and any other node data. It will also permanently remove the node from its cluster.

To reset a running and responsive node, first stop RabbitMQ on it using rabbitmqctl stop_app and then reset it using rabbitmqctl reset:

on rabbit1

rabbitmqctl stop_app

=> Stopping node rabbit@rabbit1 …done.

rabbitmqctl reset

=> Resetting node rabbit@rabbit1 …done.

In case of a non-responsive node, it must be stopped first using any means necessary. For nodes that fail to start this is already the case. Then override the node’s data directory location or [re]move the existing data store. This will make the node start as a blank one. It will have to be instructed to rejoin its original cluster, if any.

A node that’s been reset and rejoined its original cluster will sync all virtual hosts, users, permissions and topology (queues, exchanges, bindings), runtime parameters and policies. It may sync mirrored queue contents if elected to host a replica. Non-mirrored queue contents on a reset node will be lost.

Restoring queue data directories on a reset node that has synchronised its schema from a peer is not guaranteed to make that data available to clients because queue master location might have changed for the affected queues.
Upgrading clusters

You can find instructions for upgrading a cluster in the upgrade guide.
A Cluster on a Single Machine

Under some circumstances it can be useful to run a cluster of RabbitMQ nodes on a single machine. This would typically be useful for experimenting with clustering on a desktop or laptop without the overhead of starting several virtual machines for the cluster.

In order to run multiple RabbitMQ nodes on a single machine, it is necessary to make sure the nodes have distinct node names, data store locations, log file locations, and bind to different ports, including those used by plugins. See RABBITMQ_NODENAME, RABBITMQ_NODE_PORT, and RABBITMQ_DIST_PORT in the Configuration guide, as well as RABBITMQ_MNESIA_DIR, RABBITMQ_CONFIG_FILE, and RABBITMQ_LOG_BASE in the File and Directory Locations guide.

You can start multiple nodes on the same host manually by repeated invocation of rabbitmq-server ( rabbitmq-server.bat on Windows). For example:

RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit rabbitmq-server -detached
RABBITMQ_NODE_PORT=5673 RABBITMQ_NODENAME=hare rabbitmq-server -detached
rabbitmqctl -n hare stop_app
rabbitmqctl -n hare join_cluster rabbit@hostname -s
rabbitmqctl -n hare start_app

will set up a two node cluster, both nodes as disc nodes. Note that if the node listens on any ports other than AMQP 0-9-1 and AMQP 1.0 ones, those must be configured to avoid a collision as well. This can be done via command line:

RABBITMQ_NODE_PORT=5672 RABBITMQ_SERVER_START_ARGS=“-rabbitmq_management listener [{port,15672}]” RABBITMQ_NODENAME=rabbit rabbitmq-server -detached
RABBITMQ_NODE_PORT=5673 RABBITMQ_SERVER_START_ARGS=“-rabbitmq_management listener [{port,15673}]” RABBITMQ_NODENAME=hare rabbitmq-server -detached

will start two nodes (which can then be clustered) when the management plugin is installed.
Hostname Changes

RabbitMQ nodes use hostnames to communicate with each other. Therefore, all node names must be able to resolve names of all cluster peers. This is also true for tools such as rabbitmqctl.

In addition to that, by default RabbitMQ names the database directory using the current hostname of the system. If the hostname changes, a new empty database is created. To avoid data loss it’s crucial to set up a fixed and resolvable hostname.

Whenever the hostname changes RabbitMQ node must be restarted.

A similar effect can be achieved by using rabbit@localhost as the broker nodename. The impact of this solution is that clustering will not work, because the chosen hostname will not resolve to a routable address from remote hosts. The rabbitmqctl command will similarly fail when invoked from a remote host. A more sophisticated solution that does not suffer from this weakness is to use DNS, e.g. Amazon Route 53 if running on EC2. If you want to use the full hostname for your nodename (RabbitMQ defaults to the short name), and that full hostname is resolveable using DNS, you may want to investigate setting the environment variable RABBITMQ_USE_LONGNAME=true.

See the section on hostname resolution for more information.
Firewalled Nodes

Nodes can have a firewall enabled on them. In such case, traffic on certain ports must be allowed by the firewall in both directions, or nodes won’t be able to join each other and perform all the operations they expect to be available on cluster peers.

Learn more in the section on ports above and dedicated RabbitMQ Networking guide.
Erlang Versions Across the Cluster

All nodes in a cluster are highly recommended to run the same major version of Erlang: 22.2.0 and 22.2.8 can be mixed but 21.3.6 and 22.2.6 can potentially introduce breaking changes in inter-node communication protocols. While such breaking changes are relatively rare, they are possible.

Incompatibilities between patch releases of Erlang/OTP versions are very rare.
Connecting to Clusters from Clients

A client can connect as normal to any node within a cluster. If that node should fail, and the rest of the cluster survives, then the client should notice the closed connection, and should be able to reconnect to some surviving member of the cluster.

Many clients support lists of hostnames that will be tried in order at connection time.

Generally it is not recommended to hardcode IP addresses into client applications: this introduces inflexibility and will require client applications to be edited, recompiled and redeployed should the configuration of the cluster change or the number of nodes in the cluster change.

Instead, consider a more abstracted approach: this could be a dynamic DNS service which has a very short TTL configuration, or a plain TCP load balancer, or a combination of them.

In general, this aspect of managing the connection to nodes within a cluster is beyond the scope of this guide, and we recommend the use of other technologies designed specifically to address these problems.
Clusters with RAM nodes

RAM nodes keep their metadata only in memory. As RAM nodes don’t have to write to disc as much as disc nodes, they can perform better. However, note that since persistent queue data is always stored on disc, the performance improvements will affect only resource management (e.g. adding/removing queues, exchanges, or vhosts), but not publishing or consuming speed.

RAM nodes are an advanced use case; when setting up your first cluster you should simply not use them. You should have enough disc nodes to handle your redundancy requirements, then if necessary add additional RAM nodes for scale.

A cluster containing only RAM nodes would be too volatile; if the cluster stops you will not be able to start it again and will lose all data. RabbitMQ will prevent the creation of a RAM-node-only cluster in many situations, but it can’t absolutely prevent it.

The examples here show a cluster with one disc and one RAM node for simplicity only; such a cluster is a poor design choice.
Creating RAM nodes

We can declare a node as a RAM node when it first joins the cluster. We do this with rabbitmqctl join_cluster as before, but passing the --ram flag:

on rabbit2

rabbitmqctl stop_app

=> Stopping node rabbit@rabbit2 …done.

rabbitmqctl join_cluster --ram rabbit@rabbit1

=> Clustering node rabbit@rabbit2 with [rabbit@rabbit1] …done.

rabbitmqctl start_app

=> Starting node rabbit@rabbit2 …done.

RAM nodes are shown as such in the cluster status:

on rabbit1

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit1 …

=> [{nodes,[{disc,[rabbit@rabbit1]},{ram,[rabbit@rabbit2]}]},

=> {running_nodes,[rabbit@rabbit2,rabbit@rabbit1]}]

=> …done.

on rabbit2

rabbitmqctl cluster_status

=> Cluster status of node rabbit@rabbit2 …

=> [{nodes,[{disc,[rabbit@rabbit1]},{ram,[rabbit@rabbit2]}]},

=> {running_nodes,[rabbit@rabbit1,rabbit@rabbit2]}]

=> …done.

Changing node types

We can change the type of a node from ram to disc and vice versa. Say we wanted to reverse the types of rabbit@rabbit2 and rabbit@rabbit1, turning the former from a ram node into a disc node and the latter from a disc node into a ram node. To do that we can use the change_cluster_node_type command. The node must be stopped first.

on rabbit2

rabbitmqctl stop_app

=> Stopping node rabbit@rabbit2 …done.

rabbitmqctl change_cluster_node_type disc

=> Turning rabbit@rabbit2 into a disc node …done.

rabbitmqctl start_app

=> Starting node rabbit@rabbit2 …done.

on rabbit1

rabbitmqctl stop_app

=> Stopping node rabbit@rabbit1 …done.

rabbitmqctl change_cluster_node_type ram

=> Turning rabbit@rabbit1 into a ram node …done.

rabbitmqctl start_app

=> Starting node rabbit@rabbit1 …done.

Getting Help and Providing Feedback

If you have questions about the contents of this guide or any other topic related to RabbitMQ, don’t hesitate to ask them on the RabbitMQ mailing list.
Help Us Improve the Docs ❤️

If you’d like to contribute an improvement to the site, its source is available on GitHub. Simply fork the repository and submit a pull request. Thank you!
In This Section

Server Documentation
    Configuration
    Management UI
    Monitoring
    Production Checklist
    TLS Support
    Feature Flags
    Distributed RabbitMQ
    Clustering
        Cluster Formation
        High Availability
        Network Partitions
        Net Tick Time
        TLS for Inter-node (Clustering) Traffic
    Reliable Delivery
    Backup and restore
    Alarms
    Memory Use
    Networking
    Virtual Hosts
    High Availability (pacemaker)
    Access Control (Authorisation)
    Authentication Mechanisms
    LDAP
    Lazy Queues
    Internal Event Exchange
    Firehose (Message Tracing)
    Manual Pages
    Windows Quirks
Client Documentation
Plugins
News
Protocol
Our Extensions
Building
Previous Releases
License

RabbitMQ

Features
Get Started
Support
Community
Docs
Blog

Copyright © 2007-2020 VMware, Inc. or its affiliates. All rights reserved. Terms of Use, Privacy and Trademark Guidelines
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值