Preparation: Three clean servers
192.168.52.156
192.168.52.157
192.168.52.158
These three servers are all connected, and the RabbitMQ cluster nodes must be on the same segment. If they are connected across domains, the effect will be worse. Turn off firewall and selinux
1. Install and deploy rabbitmq on three machines
Install dependencies
[root@rabbitmq-1 ~]# yum install -y epel-release gcc-c + + unixODBC unixODBC-devel openssl-devel ncurses-devel
yum install erlang
[root@rabbitmq-1 ~]# curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bash
[root@rabbitmq-1 ~]# yum install erlang-21.3.8.21-1.el7.x86_64
test
Install rabbitmq
[root@rabbitmq-1 ~]# yum install rabbitmq-server-3.7.10-1.el7.noarch.rpm
start up
[root@rabbitmq-1 ~]# systemctl daemon-reload
[root@rabbitmq-1 ~]# systemctl start rabbitmq-server
[root@rabbitmq-1 ~]# systemctl enable rabbitmq-server
2. Create users
View user list
When setting permissions here, pay attention to the need for spaces between ‘.*’. The three ‘.*’ represent conf permissions, read permissions and write permissions respectively.
Each machine operates to open rabbitmq’s web access interface:
[root@rabbitmq-1 ~]# rabbitmq-plugins enable rabbitmq_management
Modify configuration file
[root@rabbitmq-1 ~]# cd /etc/rabbitmq/
[root@rabbitmq-1 rabbitmq]# cp /usr/share/doc/rabbitmq-server-3.7.10/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
[root@rabbitmq-1 rabbitmq]# ls
enabled_plugins rabbitmq.config
[root@rabbitmq-1 rabbitmq]# vim rabbitmq.config
All three machines operate the restart service:
[root@rabbitmq-1 ~]# systemctl restart rabbitmq-server
View ports
4369 — erlang discovery port
5672 –Program connection port
15672 — Management interface ui port
25672 — Internal communication port between servers
3. Access in browser
rabbitmq default administrator user: guest password: guest
The newly added user is: qf Password: 123456
4. Start deploying cluster services (operate on all three machines)
Create the data storage directory and log storage directory:
[root@rabbitmq-1 ~]# mkdir -p /data/rabbitmq/data
[root@rabbitmq-1 ~]# mkdir -p /data/rabbitmq/logs
[root@rabbitmq-1 ~]# chmod 777 -R /data/rabbitmq
[root@rabbitmq-1 ~]# chown rabbitmq.rabbitmq /data/ -R
Create configuration file:
[root@rabbitmq-1 ~]# vim /etc/rabbitmq/rabbitmq-env.conf
Restart service
[root@rabbitmq-1 ~]# systemctl restart rabbitmq-server
5.Copy?erlang.cookie
The Rabbitmq cluster relies on the Erlang cluster to operate, so the Erlang cluster environment must be built first. Each node in the Erlang cluster is implemented through a magic cookie. This cookie is stored in /var/lib/rabbitmq/.erlang.cookie, and the file has 400 permissions. Therefore, it is necessary to ensure that the cookies of each node are consistent, otherwise the nodes will not be able to communicate with each other.
If you execute #rabbitmqctl stop_app this command will report an error: Need to execute
The scp method copies the value of .erlang.cookie of the rabbitmq-1 node to the other two nodes.
[root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie [email protected]:/var/lib/rabbitmq/ [root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie [email protected]:/var/lib/rabbitmq/
6.Add rabbitmq-2 and rabbitmq-3 as memory nodes to the rabbitmq-1 node cluster
Execute the following commands in mq-2 and mq-3: [root@rabbitmq-2 ~]# systemctl restart rabbitmq-server [root@rabbitmq-2 ~]# rabbitmqctl stop_app #Stop node [root@rabbitmq-2 ~]# rabbitmqctl reset #If there is data that needs to be reset, if not, don’t use it. [root@rabbitmq-2 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1 #Add to disk node Clustering node 'rabbit@rabbitmq-2' with 'rabbit@rabbitmq-1' ... [root@rabbitmq-2 ~]# rabbitmqctl start_app #Start node Starting node 'rabbit@rabbitmq-2' ...
[root@rabbitmq-3 ~]# systemctl restart rabbitmq-server [root@rabbitmq-3 ~]# rabbitmqctl stop_app Stopping node 'rabbit@rabbitmq-3' ... [root@rabbitmq-3 ~]# rabbitmqctl reset Resetting node 'rabbit@rabbitmq-3' ... [root@rabbitmq-3 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1 Clustering node 'rabbit@rabbitmq-3' with 'rabbit@rabbitmq-1' ... [root@rabbitmq-3 ~]# rabbitmqctl start_app Starting node 'rabbit@rabbitmq-3' ...
#By default rabbitmq is a disk node after startup. Under this cluster command, mq-2 and mq-3 are memory nodes.
mq-1 is the disk node.
#If you want to make mq-2 and mq-3 both disk nodes, remove the –ram parameter.
#If you want to change the node type, you can use the command rabbitmqctl change_cluster_node_type
disc(ram), the premise is that the rabbit application must be stopped?
View cluster status
Each machine displays three nodes, indicating that it has been added successfully!
7. Log in to the rabbitmq web management console and create a new queue
Open the browser and enter http://192.168.52.156:15672, enter the default Username: guest, enter the default
Password:guest
Queue created successfully! The cluster was built successfully!
In the RabbitMQ cluster, there must be at least two disk nodes, otherwise the queue metadata cannot be written to the cluster.
When the disk node goes down, the cluster will not be able to write new queue metadata information.