Deploy open-source RabbitMQ

更新时间:
复制 MD 格式

RabbitMQ is an open-source message broker that supports multiple protocols, such as AMQP, MQTT, STOMP, and HTTP/WebSockets. It is known for its ease of use, extensibility, and high availability. RabbitMQ is primarily used to coordinate and manage communication in distributed systems, using asynchronous messaging to improve system decoupling, scalability, concurrency, and fault tolerance. Its stable performance, rich feature set, and active community support make it a popular choice for messaging and task processing in enterprise applications. This topic describes how to deploy open-source RabbitMQ on an Elastic Compute Service (ECS) instance.

Deploy RabbitMQ

  • Manual deployment: Suitable for users who have a basic knowledge of Linux commands and want to customize their RabbitMQ deployment.

  • Quick deployment: Bypasses manual installation and configuration to significantly reduce deployment time.

Manual deployment

This section describes how to install the latest version of RabbitMQ. For more information about other versions and installation methods, see Installing RabbitMQ.

Prerequisites

Ensure your ECS instance meets the following requirements:

  • The instance has a public IP address or an associated Elastic IP Address (EIP). For more information, see Elastic IP Address.

  • Operating system: Ubuntu 20.04, Ubuntu 22.04, or Debian 11.0 to Debian 12.5.

    For more information about supported operating systems and installation methods, see Installing RabbitMQ.

  • Security group rules: Add inbound rules to the security group to allow traffic on ports 22, 80, and 15672.

    Note

    If you want to connect to the RabbitMQ server using an MQ tool, you must also allow traffic on port 5672.

Procedure

  1. Connect to the instance where you will deploy RabbitMQ.

    For more information, see Connect to a Linux instance by using Workbench.

  2. Install RabbitMQ.

    1. Install the required dependencies for RabbitMQ.

      sudo apt-get install curl gnupg apt-transport-https -y
    2. Download the GPG public keys for RabbitMQ and Erlang, and add the public keys to the system's trusted keyring.

      curl -1sLf "https://keys.openpgp.org/vks/v1/by-fingerprint/0A9AF2115F4687BD29803A206B73A36E6026DFCA" | sudo gpg --dearmor | sudo tee /usr/share/keyrings/com.rabbitmq.team.gpg > /dev/null
      curl -1sLf https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-erlang.E495BB49CC4BBE5B.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg > /dev/null
      curl -1sLf https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-server.9F4587F226208342.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/rabbitmq.9F4587F226208342.gpg > /dev/null
    3. Add the official APT repositories for RabbitMQ and Erlang to the /etc/apt/sources.list.d/rabbitmq.list file.

      sudo tee /etc/apt/sources.list.d/rabbitmq.list <<EOF
      deb [signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg] https://ppa1.novemberain.com/rabbitmq/rabbitmq-erlang/deb/ubuntu jammy main
      deb-src [signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg] https://ppa1.novemberain.com/rabbitmq/rabbitmq-erlang/deb/ubuntu jammy main
      deb [signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg] https://ppa2.novemberain.com/rabbitmq/rabbitmq-erlang/deb/ubuntu jammy main
      deb-src [signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg] https://ppa2.novemberain.com/rabbitmq/rabbitmq-erlang/deb/ubuntu jammy main
      deb [signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg] https://ppa1.novemberain.com/rabbitmq/rabbitmq-server/deb/ubuntu jammy main
      deb-src [signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg] https://ppa1.novemberain.com/rabbitmq/rabbitmq-server/deb/ubuntu jammy main
      deb [signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg] https://ppa2.novemberain.com/rabbitmq/rabbitmq-server/deb/ubuntu jammy main
      deb-src [signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg] https://ppa2.novemberain.com/rabbitmq/rabbitmq-server/deb/ubuntu jammy main
      EOF
    4. Update the local package index.

      sudo apt-get update -y
    5. Install the required Erlang packages.

      sudo apt-get install -y erlang-base \
                              erlang-asn1 erlang-crypto erlang-eldap erlang-ftp erlang-inets \
                              erlang-mnesia erlang-os-mon erlang-parsetools erlang-public-key \
                              erlang-runtime-tools erlang-snmp erlang-ssl \
                              erlang-syntax-tools erlang-tftp erlang-tools erlang-xmerl
    6. Install RabbitMQ.

      sudo apt-get install rabbitmq-server -y --fix-missing
  3. Configure RabbitMQ.

    1. Enable RabbitMQ to start on boot and start the RabbitMQ service.

      sudo systemctl enable rabbitmq-server
      sudo systemctl start rabbitmq-server
    2. For security, run the following command to delete the default user.

      The default username and password for RabbitMQ are both guest.

      sudo rabbitmqctl delete_user guest
    3. Create an administrator for RabbitMQ.

      1. Create a new user.

        sudo rabbitmqctl add_user <username> <password>

        Replace <username> and <password> with your desired username and password.

      2. Set the new user as an administrator.

        sudo rabbitmqctl set_user_tags <username> administrator
      3. Grant all permissions to the new user.

        sudo rabbitmqctl set_permissions -p / <username> ".*" ".*" ".*"
  4. Enable the RabbitMQ web management interface.

    sudo rabbitmq-plugins enable rabbitmq_management

Quick deployment

Marketplace image

Using an Alibaba Cloud Marketplace image automates the installation and configuration, significantly reducing deployment time. The price of the image is specified on its product details page.

  1. Click Open-source message queue middleware RabbitMQ or to go to the image details page.

    On the image details page, you can find image information and user guides.

  2. Click Immediately Buy or .

  3. On the Custom Launch page, configure the parameters and purchase the ECS instance as prompted.

    Note the following parameters: You can configure other parameters as needed. For more information about the parameters, see Create an instance on the Custom Launch tab.

    • Image: The Image section is already set to the image that you purchased.

    • Public IP: Select Assign Public IPv4 Address.

    • Security Group: The security group rules must allow traffic on ports 22, 80, and 15672.

  4. Obtain the username and password for RabbitMQ.

    1. Connect to the RabbitMQ server.

      For more information, see Connect to a Linux instance by using Workbench.

    2. Run the following command to obtain the username and password for RabbitMQ.

      sudo cat /credentials/password.txt

      The command output is similar to the following:

      • rabbitmq_user: The RabbitMQ username.

      • rabbitmq_password: The RabbitMQ login password.

      • rabbitmq_gui_port: The RabbitMQ web management port.

      • rabbitmq_mq_port: The RabbitMQ remote connection port.

      • rabbitmq_er_port: The internal port used by the RabbitMQ server for Erlang inter-process communication.

      • rabbitmq_mqtt_port: The listening port for the MQTT service that is provided by the RabbitMQ server.

      root@iZuf660jbfxxx:~# sudo cat /credentials/password.txt
      ======== credentials for rabbitmq ========
      rabbitmq_user: admin
      rabbitmq_password: OOhWmVE9xxx
      rabbitmq_gui_port: 15672
      rabbitmq_mq_port: 5672
      rabbitmq_er_port: 4369
      rabbitmq_mqtt_port: 1833

Terraform

Terraform is an Infrastructure as Code (IaC) tool for automating the creation, deployment, and management of your infrastructure. You can write simple code to define and configure cloud resources without performing manual operations.

Procedure

  1. Click Run with one click or to open Terraform Explorer and automatically load the Terraform code.

    Click to view the complete Terraform code

    provider "alicloud" {
      region = var.region
    }
    variable "region" {
      default = "cn-beijing"
    }
    variable "common_name" {
      description = "Common name for resources."
      type        = string
      default     = "deploy_rabbitmq_by_tf"
    }
    variable "system_disk_category" {
      default     = "cloud_essd"
      description = "The category of the system disk."
    }
    variable "instance_password" {
      description = "The password of the instance. The password must be 8 to 30 characters in length and contain at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters (`~!@#$%^&*_-+=|{}[]:;'<>?,./)."
      type        = string
      default     = "Test@123456"
    }
    variable "image_id" {
      description = "Image of instance. Supported only on Ubuntu."
      type        = string
      default     = "ubuntu_22_04_x64_20G_alibase_20241224.vhd"
    }
    variable "instance_type" {
      description = "Instance type."
      type        = string
      default     = "ecs.e-c1m2.large"
    }
    variable "access_ip" {
      description = "The IP address you used to access the ECS."
      type        = string
      default     = "0.0.0.0/0"
    }
    variable "rabbitmq_user_name" {
      description = "Create a new user for RabbitMQ."
      type        = string
      default     = "rabbitmq@new_user"
    }
    variable "rabbitmq_user_password" {
      description = "Password for a new RabbitMQ user."
      type        = string
      default     = "rabbitmq@pw12345"
    }
    # Query available zones that meet the specified conditions.
    data "alicloud_zones" "default" {
      available_disk_category     = var.system_disk_category
      available_resource_creation = "VSwitch"
      available_instance_type     = var.instance_type
    }
    resource "alicloud_vpc" "rabbitmq_vpc" {
      vpc_name   = "${var.common_name}-vpc"
      cidr_block = "192.168.0.0/16"
    }
    resource "alicloud_vswitch" "rabbitmq_vsw" {
      vpc_id       = alicloud_vpc.rabbitmq_vpc.id
      vswitch_name = "${var.common_name}-vsw"
      cidr_block   = "192.168.0.0/24"
      zone_id      = data.alicloud_zones.default.zones.0.id
    }
    resource "alicloud_security_group" "rabbitmq_sg" {
      security_group_name = "${var.common_name}-sg"
      vpc_id              = alicloud_vpc.rabbitmq_vpc.id
    }
    resource "alicloud_security_group_rule" "allow_tcp_22" {
      type              = "ingress"
      ip_protocol       = "tcp"
      nic_type          = "intranet"
      policy            = "accept"
      port_range        = "22/22"
      priority          = 1
      security_group_id = alicloud_security_group.rabbitmq_sg.id
      cidr_ip           = var.access_ip
    }
    resource "alicloud_security_group_rule" "allow_tcp_80" {
      type              = "ingress"
      ip_protocol       = "tcp"
      nic_type          = "intranet"
      policy            = "accept"
      port_range        = "80/80"
      priority          = 1
      security_group_id = alicloud_security_group.rabbitmq_sg.id
      cidr_ip           = var.access_ip
    }
    resource "alicloud_security_group_rule" "allow_tcp_15672" {
      type              = "ingress"
      ip_protocol       = "tcp"
      nic_type          = "intranet"
      policy            = "accept"
      port_range        = "15672/15672"
      priority          = 1
      security_group_id = alicloud_security_group.rabbitmq_sg.id
      cidr_ip           = var.access_ip
    }
    resource "alicloud_security_group_rule" "allow_icmp_all" {
      type              = "ingress"
      ip_protocol       = "icmp"
      nic_type          = "intranet"
      policy            = "accept"
      port_range        = "-1/-1"
      priority          = 1
      security_group_id = alicloud_security_group.rabbitmq_sg.id
      cidr_ip           = "0.0.0.0/0"
    }
    resource "alicloud_instance" "rabbitmq_ecs" {
      availability_zone          = data.alicloud_zones.default.zones.0.id
      security_groups            = [alicloud_security_group.rabbitmq_sg.id]
      instance_type              = var.instance_type
      system_disk_category       = var.system_disk_category
      image_id                   = var.image_id
      instance_name              = "${var.common_name}-ecs"
      vswitch_id                 = alicloud_vswitch.rabbitmq_vsw.id
      internet_max_bandwidth_out = 10
      password                   = var.instance_password
    }
    locals {
      command_content = <<SHELL
    #!/bin/sh
    sudo apt-get install curl gnupg apt-transport-https -y
    ## Team RabbitMQ's main signing key
    curl -1sLf "https://keys.openpgp.org/vks/v1/by-fingerprint/0A9AF2115F4687BD29803A206B73A36E6026DFCA" | sudo gpg --dearmor | sudo tee /usr/share/keyrings/com.rabbitmq.team.gpg > /dev/null
    ## Community mirror of Cloudsmith: modern Erlang repository
    curl -1sLf https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-erlang.E495BB49CC4BBE5B.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg > /dev/null
    ## Community mirror of Cloudsmith: RabbitMQ repository
    curl -1sLf https://github.com/rabbitmq/signing-keys/releases/download/3.0/cloudsmith.rabbitmq-server.9F4587F226208342.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/rabbitmq.9F4587F226208342.gpg > /dev/null
    ## Add apt repositories maintained by Team RabbitMQ
    sudo tee /etc/apt/sources.list.d/rabbitmq.list <<EOF
    ## Provides modern Erlang/OTP releases
    deb [arch=amd64 signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg] https://ppa1.rabbitmq.com/rabbitmq/rabbitmq-erlang/deb/ubuntu jammy main
    deb-src [signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg] https://ppa1.rabbitmq.com/rabbitmq/rabbitmq-erlang/deb/ubuntu jammy main
    # another mirror for redundancy
    deb [arch=amd64 signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg] https://ppa2.rabbitmq.com/rabbitmq/rabbitmq-erlang/deb/ubuntu jammy main
    deb-src [signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg] https://ppa2.rabbitmq.com/rabbitmq/rabbitmq-erlang/deb/ubuntu jammy main
    ## Provides RabbitMQ
    deb [arch=amd64 signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg] https://ppa1.rabbitmq.com/rabbitmq/rabbitmq-server/deb/ubuntu jammy main
    deb-src [signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg] https://ppa1.rabbitmq.com/rabbitmq/rabbitmq-server/deb/ubuntu jammy main
    # another mirror for redundancy
    deb [arch=amd64 signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg] https://ppa2.rabbitmq.com/rabbitmq/rabbitmq-server/deb/ubuntu jammy main
    deb-src [signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg] https://ppa2.rabbitmq.com/rabbitmq/rabbitmq-server/deb/ubuntu jammy main
    EOF
    ## Update package indices
    sudo apt-get update -y
    ## Install Erlang packages
    sudo apt-get install -y erlang-base \
                            erlang-asn1 erlang-crypto erlang-eldap erlang-ftp erlang-inets \
                            erlang-mnesia erlang-os-mon erlang-parsetools erlang-public-key \
                            erlang-runtime-tools erlang-snmp erlang-ssl \
                            erlang-syntax-tools erlang-tftp erlang-tools erlang-xmerl
    ## Install rabbitmq-server and its dependencies
    sudo apt-get install rabbitmq-server -y --fix-missing
    sudo systemctl enable rabbitmq-server
    sudo systemctl start rabbitmq-server
    sudo rabbitmqctl delete_user guest
    sudo rabbitmqctl add_user ${var.rabbitmq_user_name} ${var.rabbitmq_user_password}
    sudo rabbitmqctl set_user_tags ${var.rabbitmq_user_name} administrator
    sudo rabbitmqctl set_permissions -p / ${var.rabbitmq_user_name} ".*" ".*" ".*"
    sudo rabbitmq-plugins enable rabbitmq_management
    SHELL
    }
    resource "alicloud_ecs_command" "deploy_rabbitmq" {
      name            = "DeploydRabbitMQ"
      type            = "RunShellScript"
      command_content = base64encode(local.command_content)
      timeout         = 3600
      working_dir     = "/root"
    }
    resource "alicloud_ecs_invocation" "invocation" {
      instance_id = [alicloud_instance.rabbitmq_ecs.id]
      command_id  = alicloud_ecs_command.deploy_rabbitmq.id
      timeouts {
        create = "5m"
      }
    }
    output "rabbitmq-login_url" {
      value = format("http://%s:15672", alicloud_instance.rabbitmq_ecs.public_ip)
    }
  2. Modify the parameters as needed. By default, one Virtual Private Cloud (VPC), one VSwitch, one security group, and one pay-as-you-go ECS instance are created in the cn-beijing region. For more information about the billing of ECS instances, see Billing overview.

  3. Click Initiate Debugging to preview the resources to be created. If the information is correct, click Plan & Apply to create the resources. When the execution is successful, the deployment is complete.

Log in to the RabbitMQ console

  1. In the address bar of your browser, enter http://<your_instance_public_ip>:15672.

    The RabbitMQ Management login page appears, confirming a successful installation. Enter the username and password in the Username and Password fields, and then click Login to access the management console.

  2. After you enter the administrator credentials and click Login, you are redirected to the RabbitMQ management interface.

    The Overview tab is displayed by default. This tab shows information such as the RabbitMQ and Erlang versions, cluster node resource usage (such as file descriptors, memory, and disk space), and global statistics including the number of connections and queues. This confirms that RabbitMQ is deployed and running correctly.

Migrate open-source RabbitMQ to the cloud

To resolve common stability issues in self-managed RabbitMQ clusters, such as message backlogs and split-brain problems, and to enable high concurrency, distributed deployment, and elastic scaling, you can migrate your open-source RabbitMQ cluster to ApsaraMQ for RabbitMQ. For more information, see Migrate open-source RabbitMQ to the cloud.