Preface
Any service, if only standalone deployment, the performance always have an upper limit, RabbitMQ
is no exception, when a single RabbitMQ
time service the ability to handle messages reach the bottleneck, can be achieved through high availability and load balancing cluster.
How much do you know about the RabbitMQ cluster
Typically, in each cluster we have a service called a node in RabbitMQ
the cluster, the node type can be divided into two types:
- Memory node: Metadata is stored in memory. In order to synchronize data after restart, the memory node will store the address of the disk node in the disk. In addition, if the message is persisted, it will also be stored in the disk, because the memory node reads and writes fast, and the general customer The end will connect to the memory node.
- Disk node: Metadata is stored in disk (default node type), and at least one disk node needs to be guaranteed. Otherwise, once it goes down, the data cannot be restored, and the purpose of high availability of the cluster cannot be achieved.
PS: Metadata refers to basic information including queue name attributes, switch type name attributes, binding information, vhost, etc. It does not include message data in the queue.
RabbitMQ
There are two main modes of clusters in, the normal cluster mode and the mirror queue mode.
Normal cluster mode
In the normal cluster mode, each node in the cluster will only synchronize metadata with each other, that is, message data will not be synchronized. So the question becomes, if we are connected to the A
node, but the message they are stored in the B
node and how to do it?
Regardless of whether it is a producer or a consumer, if the connected node does not store queue data, it will be forwarded internally to the node storing the queue data for storage. Although it can be forwarded internally, because the message is only stored in one node, if the node goes down, will the message be gone? This problem does exist, so this common cluster mode does not achieve the purpose of high availability.
Mirror queue mode
In the mirror queue mode, not only the metadata will be synchronized between nodes, but the message content will also be synchronized between the mirror nodes, and the availability is higher. While this solution improves availability, it also brings network overhead between synchronized data, which will affect performance to a certain extent.
RabbitMQ cluster construction
Let's try together to build a RabbitMQ
cluster:
-
If you had to start before the stand-alone version, then delete the old data
rm -rf /var/lib/rabbitmq/mnesia
or delete the installation in the directoryvar/lib/rabbitmq/mnesia
, my machine is installed in the installation directory, the command is executedrm -rf /usr/local/rabbitmq_server-3.8.4/var/lib/rabbitmq/mnesia/
. -
Next you need to start the following three commands to start three different port number of
RabbitMQ
services, in addition to specifyingRabbitMQ
when the service ports also need to specify additional port back office systems, and must specify thenode
prefix name, because the name of the cluster node is to communicate , So the node name must be unique, the default node name israbbit@hostname
, the following command indicates that the prefix is specified:
RABBITMQ_NODE_PORT=5672 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15672}]" RABBITMQ_NODENAME=rabbit1 rabbitmq-server -detached
RABBITMQ_NODE_PORT=5673 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}]" RABBITMQ_NODENAME=rabbit2 rabbitmq-server -detached
RABBITMQ_NODE_PORT=5674 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15674}]" RABBITMQ_NODENAME=rabbit3 rabbitmq-server -detached
After starting to enter the /usr/local/rabbitmq_server-3.8.4/var/lib/rabbitmq/mnesia/
catalog, find creating a 3
node information:
Also through ps -ef | grep rabbit
can also be found three service process is started.
- The three services started now have no connection with each other. Now we need to use one of the nodes as the master node, and then the other two nodes need to join the master node to form a cluster service. It should be noted that before joining the cluster, it needs to be reset. Node information, that is, nodes with data are not allowed to join the cluster.
//rabbit2 节点重置后加入集群
rabbitmqctl -n rabbit2 stop_app
rabbitmqctl -n rabbit2 reset
rabbitmqctl -n rabbit2 join_cluster --ram rabbit1@`hostname -s` //--ram 表示这是一个内存节点
rabbitmqctl -n rabbit2 start_app
rabbitmqctl -n rabbit3 stop_app
rabbitmqctl -n rabbit3 reset
rabbitmqctl -n rabbit3 join_cluster --disc rabbit1@`hostname -s` //--disc表示磁盘节点(默认也是磁盘节点)
rabbitmqctl -n rabbit3 start_app
- After a successful, execute the command
rabbitmqctl cluster_status
query noderabbit1
status, you can see the following figure, two disk nodes a memory node:
- It should be noted that the cluster started here is just the default normal cluster. If you want to configure a mirrored cluster, you need to execute the following command:
rabbitmqctl -n rabbit1 set_policy ha-all "^" '{"ha-mode":"all"}'
Here RabbitMQ
a cluster even if the build is complete, but should be noted that, because here is a stand-alone version, so do not consider .erlang.cookie
the file consistent.
High-availability cluster based on HAProxy + Keepalived
If a RabbitMQ
cluster, there are multiple memory nodes, where a node is connected we should do? If this strategy of choice is done on the client side, there will be great drawbacks. The most serious one is that the client code must be modified every time the cluster is expanded. Therefore, this method is not very desirable, so we are deploying the cluster. when you need an intermediate proxy component, this component to enable service monitoring and forwarding, such as Redis
the Sentinel
(Sentinel) cluster mode, you can monitor sentinel Redis
node and failover.
In RabbitMQ
a cluster, by Keepalived
and HAProxy
two components to achieve high availability and load balancing cluster.
HAProxy
HAProxy
It is an open source, high-performance load balancing software, which can also be used as load balancing software nginx
, lvs
etc. HAproxy
Support 7
layer load balancing and 4
layer load balancing.
Load balancing
So-called 7
layer load balancing and 4
layer load balancing for the OSI
purposes of the model, shown below is a OSI
communication model:
We see the figure above, the first 7
layer corresponds to the application layer, the second 4
layer corresponds to the transport layer. Commonly used as load-balancing software nginx
generally work in the first 7
layer, lvs
generally work (Linux Virtual Server) in the first 4
layer.
4
Layer load:
4
Layer load uses NAT
(Network Address Translation) technology, namely: Network Address Translation. When receiving the client request, by modifying the source of the data package IP
and the port, and then forward the packet to the corresponding target server. 4
Layer load balancing can only forward requests based on the destination address and source address in the message, and cannot determine or modify the specific types of requested resources.
7
Layer load:
According to the resource path requested by the client, it is forwarded to different target servers.
Highly available HAProxy
HAProxy
Although load balancing is achieved, if only one is deployed HAProxy
, there is a risk of downtime. Once HAProxy
down, it will cause the entire cluster is unavailable, so we need to HAProxy
also implement a cluster, if HAProxy
also realized the cluster, the client should connect Which services? The problem seems to be back to the beginning, stuck in an infinite loop...
Keepalived
In order to achieve HAProxy
high availability, the need to introduce a Keepalived
component, Keepalived
component mainly has the following characteristics:
- With load function, it can monitor the status of nodes in the cluster. If a node in the cluster goes down, failover can be realized.
- Cluster itself can also be achieved, but only one
master
node. master
Foreign node will provide a virtualIP
application side only need to connect this oneIP
on the line. It can be understood as the clusterHAProxy
nodes will also compete for the virtualIP
, which node to the competition, which will be served by a node.
VRRP protocol
VRRP
The protocol is the Virtual Router Redundancy Protocol (Virtual Router Redundancy Protocol). Keepalived
Provided a virtual IP
mechanism belongs to VRRP
, it is to avoid the emergence of a fault-tolerant protocol router single point of failure.
to sum up
This paper describes the RaabbitMQ
relevant knowledge clusters, and compared the difference between ordinary cluster and cluster mirror, and finally through practice to build a RabbitMQ
cluster, but also introduces some drawbacks common clusters can be combined HAProxy
and Keepalived
components to achieve true high availability distributed Cluster service.