RabbitMQ cluster configuration and load balancing configuration

RabbitMQ cluster configuration and load balancing configuration

    • Environment configuration
    • Cluster configuration
      • Install rabbitmq
        • Start rabbitmq
        • Enable remote login
        • Add users and authorize users
        • Add data storage directory and log storage directory
        • View port
        • copy?erlang.cookie
        • Add mq-2 and mq-3 as memory nodes to the mq-1 node cluster
        • View cluster status
        • Add a new queue
    • RabbitMq load balancing configuration-HAProxy
        • Install HAProxy
        • Modify configuration file
        • Login haproxy
        • mysql authorizes root user to log in remotely

Environment configuration

Server ip address
rabbitmq-1 192.168.10.128
rabbitmq-2 192.168.10.129
rabbitmq-3 192.168.10.130

Cluster configuration

Install rabbitmq

  • All three machines are configured with corresponding hosts files.

    [root@localhost ~]# cat /etc/hosts
    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.10.128 rabbitmq-1
    192.168.10.129 rabbitmq-2
    192.168.10.130 rabbitmq-3
    
  • All three machines must arrange rabbitmq Currently, all three machines have uploaded the corresponding installation packages

    [root@localhost ~]# ls
    anaconda-ks.cfg rabbitmq-server-3.7.10-1.el7.noarch.rpm erlang-21.3.8.21-1.el7.x86_64.rpm
    [root@localhost ~]# yum install -y erlang-21.3.8.21-1.el7.x86_64.rpm
    [root@localhost ~]# yum install -y rabbitmq-server-3.7.10-1.el7.noarch.rpm
    
Start rabbitmq
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start rabbitmq-server
Enable remote login
[root@localhost ~]# cd /usr/share/doc/rabbitmq-server-3.7.10/
[root@localhost rabbitmq-server-3.7.10]# cp rabbitmq.config.example /etc/rabbitmq
[root@localhost rabbitmq-server-3.7.10]# cd /etc/rabbitmq/
[root@localhost rabbitmq]# ls
enabled_plugins rabbitmq.config.example
[root@localhost rabbitmq]# mv rabbitmq.config.example rabbitmq.config
[root@localhost rabbitmq]# vim rabbitmq.config

Modify the content of the following configuration file, line 349

# Open rabbitmq’s web access interface:
[root@localhost ~]# rabbitmq-plugins enable rabbitmq_management
  • Enter the corresponding IP to log in

    There is a default user guest and the password is also guest.

Add users and authorize users
[root@localhost ~]# rabbitmqctl add_user root 1
Adding user "root" ...
[root@localhost ~]# rabbitmqctl set_user_tags root administrator
Setting tags for user "root" to [administrator] ...
[root@localhost ~]# rabbitmqctl list_users
Listing users...
user tags
guest [administrator]
root [administrator]
[root@localhost ~]# rabbitmqctl set_permissions -p "/" root ".*" ".*" ".*"
Setting permissions for user "root" in vhost "/" ...
Add data storage directory and log storage directory
[root@localhost ~]# mkdir -p /data/rabbitmq/data
[root@localhost ~]# mkdir -p /data/rabbitmq/logs
[root@localhost ~]# chmod 777 -R /data/rabbitmq
[root@localhost ~]# chown rabbitmq.rabbitmq /data/ -R
Create configuration file:
[root@localhost ~]# vim /etc/rabbitmq/rabbitmq-env.conf
[root@localhost ~]# cat /etc/rabbitmq/rabbitmq-env.conf
RABBITMQ_MNESIA_BASE=/data/rabbitmq/data
RABBITMQ_LOG_BASE=/data/rabbitmq/logs
Restart service
[root@localhost ~]# systemctl restart rabbitmq-server
View port
[root@localhost ~]# ss -ntlp

1564157344617

4369 – erlang discovery port
5672 –Program connection port
15672 – Management interface ui port
25672 – Internal communication port between servers

Copy?erlang.cookie
[root@localhost ~]# cd /var/lib/rabbitmq/
# The scp method copies the value of .erlang.cookie of the rabbitmq-1 node to the other two nodes.
[root@localhost rabbitmq]# scp .erlang.cookie 192.168.10.129:/var/lib/rabbitmq/
[email protected]'s password:
.erlang.cookie 100% 20 14.6KB/s 00:00
[root@localhost rabbitmq]# scp .erlang.cookie 192.168.10.130:/var/lib/rabbitmq/
[email protected]'s password:
.erlang.cookie

Each node is implemented through a magic cookie. This cookie is stored in /var/lib/rabbitmq/.erlang.cookie. 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.

(The official document introducing the cluster mentioned that .erlang.cookie generally exists in these two addresses: the first is home/.erlang.cookie; the second is /var/lib/rabbitmq/.erlang.cookie . If we use the decompression method to install the deployed rabbitmq, then this file will be in the {home} directory, which is $home/.erlang.cookie. If we use rpm and other installation packages to install, then this file will be in /var/lib/rabbitmq directory.)

Add mq-2 and mq-3 as memory nodes to the mq-1 node cluster
Execute the following commands in mq-2 and mq-3:
[root@localhost ~]# systemctl restart rabbitmq-server
[root@localhost ~]# rabbitmqctl stop_app #Stop node
[root@localhost ~]# rabbitmqctl reset #If there is data that needs to be reset, if not, don’t use it.
[root@localhost ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1 #Add to disk node
Clustering node 'rabbit@rabbitmq-2' with 'rabbit@rabbitmq-1' ...
[root@localhost ~]# rabbitmqctl start_app #Start node
Starting node 'rabbit@rabbitmq-2' ...
================================================== ====================
[root@localhost ~]# systemctl restart rabbitmq-server
[root@localhost ~]# rabbitmqctl stop_app
[root@localhost ~]# rabbitmqctl reset
[root@localhost ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1
Clustering node 'rabbit@rabbitmq-3' with 'rabbit@rabbitmq-1' ...
[root@localhost ~]# rabbitmqctl start_app
Starting node 'rabbit@rabbitmq-3' ...

(1) 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.
(2) If you want to make mq-2 and mq-3 both disk nodes, remove the --ram parameter.
(3) 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?
Note:
#If you need to use disk nodes to join the cluster
 [root@rabbitmq-2 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-1
 [root@rabbitmq-3 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-1

image-20231108154240796

If you log in and have the above, it means the addition is successful

View cluster status
# You can view it on any one
Execute rabbitmqctl cluster_status on any node of the RabbitMQ cluster to check whether the cluster configuration is successful.
Check on the mq-1 disk node
[root@localhost ~]# rabbitmqctl cluster_status

1564158874154

Add a new queue
  • Add queue

image-20231108154447835

  • Change to cluster viewable

    Currently this is only available for mq-1

image-20231108154617880

Modify After modification, it will be the same as above

[root@localhost ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'

RabbitMq load balancing configuration-HAProxy

Install HAProxy
[root@localhost ~]# yum install haproxy
Modify configuration file
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
bal
    log 127.0.0.1 local2
    chroot /var/lib/haproxy
    pidfile /var/run/haproxy.pid
    maxconn 4000
    user haproxy
    group haproxy
    nbproc 4
    daemon
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
#------------------------------------------------ -------------------
defaults
    mode http
    log global
    retries 3
    timeout connect 10s
    timeout client 1m
    timeout server 1m
    timeout check 10s
    maxconn 2048
#------------------------------------------------ -------------------
##Monitor and view local status#####
listenadmin_stats
        bind *:88
    mode http
    option httplog
    option httpclose
    log 127.0.0.1 local0 err
    stats uri /haproxy
    stats auth root:1
    stats refresh 30s
####################################
###Anti-generation monitoring
frontend server
    bind *:5670
    log global
    mode tcp
    #option forwardfor
    default_backend rabbitmq
    maxconn 3
backend rabbitmq
    mode tcp
    log global
    balance roundrobin
    server rabbitmq1 192.168.10.128:5672 check inter 2000s rise 2 fall 3
    server rabbitmq2 192.168.10.129:5672 check inter 2000s rise 2 fall 3
    server rabbitmq3 192.168.10.130:5672 check inter 2000s rise 2 fall 3
    
[root@localhost ~]# systemctl start haproxy
[root@localhost ~]# ss -ntlp | grep 88
LISTEN 0 128 *:88 *:* users:(("haproxy",pid=17070,fd=5),("haproxy",pid=17069,fd=5),("haproxy",pid=17068,fd =5),("haproxy",pid=17067,fd=5))
Log in to haproxy

192.168.10.128:88/haproxy

mysql authorizes root user to log in remotely
[root@localhost ~]# mysql -uroot -p'Cjb@1234'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant all on *.* to 'root'@'%' identified by 'Cjb@1234';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> \q
Bye