docker集群

发布于:2025-08-12 ⋅ 阅读:(17) ⋅ 点赞:(0)

一、创建文件夹

mkdir /mydata/rabbitmq
cd rabbitmq/
mkdir rabbitmq01 rabbitmq02 rabbitmq03

二、创建第一个节点

docker run -d --hostname rabbitmq01 --name rabbitmq01 -v /mydata/rabbitmq/rabbitmq01:/var/lib/rabbitmq -p 15673:15672 -p 5673:5672 -e RABBITMQ_ERLANG_COOKIE='atguigu' rabbitmq:management

三、启动第二个节点

docker run -d --hostname rabbitmq02 --name rabbitmq02 -v /mydata/rabbitmq/rabbitmq02:/var/lib/rabbitmq -p 15674:15672 -p 5674:5672 -e RABBITMQ_ERLANG_COOKIE='atguigu' --link rabbitmq01:rabbitmq01 rabbitmq:management

四、启动第三个节点

docker run -d --hostname rabbitmq03 --name rabbitmq03 -v /mydata/rabbitmq/rabbitmq03:/var/lib/rabbitmq -p 15675:15672 -p 5675:5672 -e RABBITMQ_ERLANG_COOKIE='atguigu' --link rabbitmq01:rabbitmq01 --link rabbitmq02:rabbitmq02 rabbitmq:management

五、访问

默认帐号密码都是guest

六、配置集群

这三个节点都准备好了。接下来就来配置集群。

第一步:先在进入rabbitmq01

进入rabbitmq01以后使用rabbitmqctl这个命令,把01的相关信息来初始化一下

rabbitmqctl可以操作rabbit MQ当前节点,比如现在来先来操作rabbitmq01

docker exec -it rabbitmq01 /bin/bash
#暂停一下当前的这个应用
rabbitmqctl stop_app 
#重置应用,如果你这个是刚新建的,就不用重置了,相当于恢复出厂设置
rabbitmqctl reset
#开启应用
rabbitmqctl start_app

第二步:进入rabbitmq02

docker exec -it rabbitmq02 /bin/bash
rabbitmqctl stop_app 
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbitmq01
rabbitmqctl start_app

第三步:进入rabbitmq03

docker exec -it rabbitmq03 /bin/bash
rabbitmqctl stop_app 
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbitmq01
rabbitmqctl start_app

全部的操作如下:

[root@vbox rabbitmq]# docker exec -it rabbitmq01 /bin/bash
root@rabbitmq01:/# rabbitmqctl

Usage

rabbitmqctl [--node <node>] [--timeout <timeout>] [--longnames] [--quiet] <command> [<command options>]

Available commands:

Help:

   autocomplete                                  Provides command name autocomplete variants
   help                                          Displays usage information for a command
   version                                       Displays CLI tools version

Nodes:

   await_startup                                 Waits for the RabbitMQ application to start on the target node
   reset                                         Instructs a RabbitMQ node to leave the cluster and return to its virgin state
   rotate_logs                                   Does nothing [deprecated]
   shutdown                                      Stops RabbitMQ and its runtime (Erlang VM). Monitors progress for local nodes. Does not require a PID file path.
   start_app                                     Starts the RabbitMQ application but leaves the runtime (Erlang VM) running
   stop                                          Stops RabbitMQ and its runtime (Erlang VM). Requires a local node pid file path to monitor progress.
   stop_app                                      Stops the RabbitMQ application, leaving the runtime (Erlang VM) running
   wait                                          Waits for RabbitMQ node startup by monitoring a local PID file. See also 'rabbitmqctl await_online_nodes'

Cluster:

   await_online_nodes                            Waits for <count> nodes to join the cluster
   change_cluster_node_type                      Changes the type of the cluster node
   cluster_status                                Displays all the nodes in the cluster grouped by node type, together with the currently running nodes
   force_boot                                    Forces node to start even if it cannot contact or rejoin any of its previously known peers
   force_reset                                   Forcefully returns a RabbitMQ node to its virgin state
   force_standalone_khepri_boot                  Forces node to start as a standalone node
   forget_cluster_node                           Removes a node from the cluster
   join_cluster                                  Instructs the node to become a member of the cluster that the specified node is in

Users:

   add_user                                      Creates a new user in the internal database. This user will have no permissions for any virtual hosts by default.
   authenticate_user                             Attempts to authenticate a user. Exits with a non-zero code if authentication fails.
   change_password                               Changes the user password
   clear_password                                Clears (resets) password and disables password login for a user
   clear_user_limits                             Clears user connection/channel limits
   delete_user                                   Removes a user from the internal database. Has no effect on users provided by external backends such as LDAP
   list_user_limits                              Displays configured user limits
   list_users                                    List user names and tags
   set_user_limits                               Sets user limits
   set_user_tags                                 Sets user tags

Access Control:

   clear_permissions                             Revokes user permissions for a vhost
   clear_topic_permissions                       Clears user topic permissions for a vhost or exchange
   list_permissions                              Lists user permissions in a virtual host
   list_topic_permissions                        Lists topic permissions in a virtual host
   list_user_permissions                         Lists permissions of a user across all virtual hosts
   list_user_topic_permissions                   Lists user topic permissions
   list_vhosts                                   Lists virtual hosts
   set_permissions                               Sets user permissions for a vhost
   set_permissions_globally                      Sets user permissions for all virtual hosts.
   set_topic_permissions                         Sets user topic permissions for an exchange

Monitoring, observability and health checks:

   activate_free_disk_space_monitoring           [Re-]activates free disk space monitoring on a node
   check_if_any_deprecated_features_are_used     Generate a report listing all deprecated features in use
   deactivate_free_disk_space_monitoring         Deactivates free disk space monitoring on a node
   list_bindings                                 Lists all bindings on a vhost
   list_channels                                 Lists all channels in the node
   list_ciphers                                  Lists cipher suites supported by encoding commands
   list_connections                              Lists AMQP connections for the node
   list_consumers                                Lists all consumers for a vhost
   list_exchanges                                Lists exchanges
   list_hashes                                   Lists hash functions supported by encoding commands
   list_node_auth_attempt_stats                  Lists authentication attempts on the target node
   list_queues                                   Lists queues and their properties
   list_unresponsive_queues                      Tests queues to respond within timeout. Lists those which did not respond
   ping                                          Checks that the node OS process is up, registered with EPMD and CLI tools can authenticate with it
   report                                        Generate a server status report containing a concatenation of all server status information for support purposes
   schema_info                                   Lists schema database tables and their properties
   status                                        Displays status of a node

Parameters:

   clear_global_parameter                        Clears a global runtime parameter
   clear_parameter                               Clears a runtime parameter.
   list_global_parameters                        Lists global runtime parameters
   list_parameters                               Lists runtime parameters for a virtual host
   set_global_parameter                          Sets a runtime parameter.
   set_parameter                                 Sets a runtime parameter.

Policies:

   clear_operator_policy                         Clears an operator policy
   clear_policy                                  Clears (removes) a policy
   list_operator_policies                        Lists operator policy overrides for a virtual host
   list_policies                                 Lists all policies in a virtual host
   set_operator_policy                           Sets an operator policy that overrides a subset of arguments in user policies
   set_policy                                    Sets or updates a policy

Virtual hosts:

   add_vhost                                     Creates a virtual host
   clear_vhost_limits                            Clears virtual host limits
   delete_vhost                                  Deletes a virtual host
   disable_vhost_deletion_protection             Removes deletion protection from a virtual host (so that it can be deleted)
   enable_vhost_deletion_protection              Protects a virtual host from deletion (until the protection is removed)
   list_vhost_limits                             Displays configured virtual host limits
   reconcile_vhosts                              Makes sure all virtual hosts were initialized on all reachable cluster nodes
   restart_vhost                                 Restarts a failed vhost data stores and queues
   set_vhost_limits                              Sets virtual host limits
   set_vhost_tags                                Sets virtual host tags
   trace_off                                     
   trace_on                                      
   update_vhost_metadata                         Updates metadata (tags, description, default queue type, protection from deletion) a virtual host

Queues:

   delete_queue                                  Deletes a queue
   purge_queue                                   Purges a queue (removes all messages in it)

Configuration and Environment:

   decode                                        Decrypts an encrypted advanced.config value
   decrypt_conf_value                            Decrypts an encrypted configuration value
   encode                                        Encrypts a sensitive configuration value to be used in the advanced.config file
   encrypt_conf_value                            Encrypts a sensitive configuration value to be used in the advanced.config file
   environment                                   Displays the name and value of each variable in the application environment for each running application
   set_cluster_name                              Sets the cluster name
   set_disk_free_limit                           Sets the disk_free_limit setting
   set_log_level                                 Sets log level in the running node
   set_vm_memory_high_watermark                  Sets the vm_memory_high_watermark setting

Feature flags:

   enable_feature_flag                           Enables a feature flag or all supported feature flags on the target node
   list_deprecated_features                      Lists deprecated features
   list_feature_flags                            Lists feature flags

Operations:

   close_all_connections                         Instructs the broker to close all connections for the specified vhost or entire RabbitMQ node
   close_all_user_connections                    Instructs the broker to close all connections of the specified user
   close_connection                              Instructs the broker to close the connection associated with the Erlang process id
   eval                                          Evaluates a snippet of Erlang code on the target node
   eval_file                                     Evaluates a file that contains a snippet of Erlang code on the target node
   exec                                          Evaluates a snippet of Elixir code on the CLI node
   force_gc                                      Makes all Erlang processes on the target node perform/schedule a full sweep garbage collection
   remove_classic_queue_mirroring_from_policies  Removes keys that enable classic queue mirroring from all regular and operator policies
   resume_listeners                              Resumes client connection listeners making them accept client connections again
   suspend_listeners                             Suspends client connection listeners so that no new client connections are accepted

Definitions:

   export_definitions                            Exports definitions in JSON or compressed Erlang Term Format.
   import_definitions                            Imports definitions in JSON or compressed Erlang Term Format.

Management plugin:

   reset_stats_db                                Resets statistics database. This will remove all metrics data!

Other:

   hash_password                                 Hashes a plaintext password

Deprecated:

   node_health_check                             DEPRECATED. This command is a no-op. See https://www.rabbitmq.com/monitoring.html#health-checks
   rename_cluster_node                           DEPRECATED. This command is a no-op. Node renaming is incompatible with Raft-based features such as quorum queues, streams, Khepri. 
   update_cluster_nodes                          DEPRECATED. This command is a no-op. Node update is incompatible with Raft-based features such as quorum queues, streams, Khepri. 

Use 'rabbitmqctl help <command>' to learn more about a specific command
root@rabbitmq01:/# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq01 ...
root@rabbitmq01:/# rabbitmqctl reset
Resetting node rabbit@rabbitmq01 ...
root@rabbitmq01:/# rabbitmqctl start_app
Starting node rabbit@rabbitmq01 ...
root@rabbitmq01:/# exit
exit
[root@vbox rabbitmq]# docker exec -it rabbitmq02 /bin/bash
root@rabbitmq02:/# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq02 ...
root@rabbitmq02:/# rabbitmqctl reset
Resetting node rabbit@rabbitmq02 ...
root@rabbitmq02:/# rabbitmqctl join_cluster --ram rabbit@rabbitmq01
Clustering node rabbit@rabbitmq02 with rabbit@rabbitmq01
root@rabbitmq02:/# rabbitmqctl start_app
Starting node rabbit@rabbitmq02 ...
root@rabbitmq02:/# exit
exit
[root@vbox rabbitmq]# docker exec -it rabbitmq03 /bin/bash
root@rabbitmq03:/# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq03 ...
root@rabbitmq03:/# rabbitmqctl reset
Resetting node rabbit@rabbitmq03 ...
root@rabbitmq03:/# rabbitmqctl join_cluster --ram rabbit@rabbitmq01
Clustering node rabbit@rabbitmq03 with rabbit@rabbitmq01
root@rabbitmq03:/# rabbitmqctl start_app
Starting node rabbit@rabbitmq03 ...
root@rabbitmq03:/# exit
exit
[root@vbox rabbitmq]# 

七、测试集群

随便访问一个节点

八、配置集群镜像(3.8以后废弃了这个东西)

现在默认还是普通的镜像,即数据只有在存在三个节点之一,不能解决单点故障的问题,镜像模式就是数据在每个节点都保存,实现了高可用

docker exec -it rabbitmq01 /bin/bash
rabbitmqctl set_policy -p / ha "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}' 
rabbitmqctl list_policies -p /

这里我的版本是4.1.2,所以我就没有设置这一步

九、测试集群

进入01节点

添加一个队列

发现02 03节点也都有这个队列,包括给队列存消息,三个节点都能看到这个消息,消费后都看不到这个消息了

官方文档(英文):RabbitMQ Documentation | RabbitMQ

 官方文档(中文):RabbitMQ 文档 | RabbitMQ 消息队列


网站公告

今日签到

点亮在社区的每一天
去签到