系统相关
首页 > 系统相关> > Ubuntu安装RabbitMQ及部署集群

Ubuntu安装RabbitMQ及部署集群

作者:互联网

由于为了学习RabbitMQ的使用,我在我服务器上安装了RabbitMQ.
目前服务器配置如下:

{
    vCPU: 2核,
    RAM : 3GB,
    ROM : 50G,
    磁盘节点: 192.168.2.25
    内存节点: 192.168.2.26,192.168.2.28
    HAProxy: 192.168.2.27 
}
安装RabbitMQ
sudo nano ./installrabbitmq.sh
#!/usr/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
## Launchpad PPA that provides modern Erlang releases
curl -1sLf "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xf77f1eda57ebb1cc" | sudo gpg --dearmor | sudo tee /usr/share/keyrings/net.launchpad.ppa.rabbitmq.erlang.gpg > /dev/null
## PackageCloud RabbitMQ repository
curl -1sLf "https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey" | sudo gpg --dearmor | sudo tee /usr/share/keyrings/io.packagecloud.rabbitmq.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
##
## "bionic" as distribution name should work for any reasonably recent Ubuntu or Debian release.
## See the release to distribution mapping table in RabbitMQ doc guides to learn more.
deb [signed-by=/usr/share/keyrings/net.launchpad.ppa.rabbitmq.erlang.gpg] http://ppa.launchpad.net/rabbitmq/rabbitmq-erlang/ubuntu bionic main
deb-src [signed-by=/usr/share/keyrings/net.launchpad.ppa.rabbitmq.erlang.gpg] http://ppa.launchpad.net/rabbitmq/rabbitmq-erlang/ubuntu bionic main

## Provides RabbitMQ
##
## "bionic" as distribution name should work for any reasonably recent Ubuntu or Debian release.
## See the release to distribution mapping table in RabbitMQ doc guides to learn more.
deb [signed-by=/usr/share/keyrings/io.packagecloud.rabbitmq.gpg] https://packagecloud.io/rabbitmq/rabbitmq-server/ubuntu/ bionic main
deb-src [signed-by=/usr/share/keyrings/io.packagecloud.rabbitmq.gpg] https://packagecloud.io/rabbitmq/rabbitmq-server/ubuntu/ bionic 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 chmod 755 ./installrabbitmq.sh
./installrabbitmq.sh
# 启动 rabbitmq 服务
sudo service rabbitmq-server start
# 关闭 rabbitmq 服务
sudo service rabbitmq-server stop
# 重启 rabbitmq 服务
sudo service rabbitmq-server restart
# 查看 rabbitmq 状态
sudo service rabbitmq-server status
# 启用 rabbitmq_manager
sudo rabbitmq-plugins enable rabbitmq_management
##设置账号密码
joe@rabbitmq-01:~$ sudo rabbitmqctl add_user admin 123456
Adding user "admin" ...
joe@rabbitmq-01:~$ sudo rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
Setting permissions for user "admin" in vhost "/" ...
joe@rabbitmq-01:~$ sudo rabbitmqctl set_user_tags admin administrator
Setting tags for user "admin" to [administrator] ...

添加集群
sudo scp /var/lib/rabbitmq/.erlang.cookie joe@192.168.2.26:/home/joe/
sudo scp /var/lib/rabbitmq/.erlang.cookie joe@192.168.2.28:/home/joe/
sudo mv ./.erlang.cookie /var/lib/rabbitmq/.erlang.cookie
sudo chown rabbitmq /var/lib/rabbitmq/.erlang.cookie
sudo nano /etc/hosts
# 根据自己的实际情况,加入如下信息
192.168.2.25 rabbitmq-01
192.168.2.26 rabbitmq-02
192.168.2.28 rabbitmq-03
sudo rabbitmq-server -detached
# 停止服务
sudo rabbitmqctl stop_app
# 重置服务
sudo rabbitmqctl reset
# 加入时候设置节点为内存节点(默认加入的为磁盘节点)
sudo rabbitmqctl join_cluster rabbit@rabbitmq-01 --ram
#也通过下面方式修改的节点的类型,使用该命令前需要先停止当前节点.
sudo rabbitmqctl change_cluster_node_type disc | ram

安装haproxy
sudo apt-get install --no-install-recommends software-properties-common
sudo add-apt-repository ppa:vbernat/haproxy-2.5
sudo apt-get install haproxy=2.5.\*
sudo haproxy -v
HAProxy version 2.5.5-1ppa1~focal 2022/03/14 - https://haproxy.org/
Status: stable branch - will stop receiving fixes around Q1 2023.
Known bugs: http://www.haproxy.org/bugs/bugs-2.5.5.html
Running on: Linux 5.4.0-107-generic #121-Ubuntu SMP Thu Mar 24 16:04:27 UTC 2022 x86_64
sudo nano /etc/haproxy/haproxy.cfg
#绑定配置
listen rabbitmq_cluster
        bind 0.0.0.0:5672
        #配置TCP模式
        mode tcp
        #加权轮询
        balance roundrobin
        #RabbitMQ集群节点配置,其中ip1~ip(n)为RabbitMQ集群节点ip地址
        server rabbitmq-02 192.168.2.26:5672 check inter 5000 rise 2 fall 3 weight 1
        server rabbitmq-03 192.168.2.28:5672 check inter 5000 rise 2 fall 3 weight 1

#haproxy监控页面地址
listen monitor
        bind 0.0.0.0:8100
        mode http
        option httplog
        stats enable
        stats uri /stats
        stats refresh 5s
global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

        # Default SSL material locations
        ca-base /etc/ssl/certs
        crt-base /etc/ssl/private

        # See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediate
        ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
        ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
        ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

#绑定配置
listen rabbitmq_cluster
        bind 0.0.0.0:5672
        #配置TCP模式
        mode tcp
        #加权轮询
        balance roundrobin
        #RabbitMQ集群节点配置,其中ip1~ip(n)为RabbitMQ集群节点ip地址
        server rabbitmq-02 192.168.2.26:5672 check inter 5000 rise 2 fall 3 weight 1
        server rabbitmq-03 192.168.2.28:5672 check inter 5000 rise 2 fall 3 weight 1

#haproxy监控页面地址
listen monitor
        bind 0.0.0.0:8100
        mode http
        option httplog
        stats enable
        stats uri /stats
        stats refresh 5s
        # 添加验证
        stats auth admin:123456
# 启动服务
sudo systemctl start haproxy
# 查看状态
sudo systemctl status haproxy
# 重启服务
sudo systemctl restart haproxy
# 停止服务
sudo systemctl stop haproxy
sudo systemctl enable haproxy

标签:haproxy,192.168,sudo,rabbitmq,集群,RabbitMQ,Ubuntu,节点
来源: https://www.cnblogs.com/dygood/p/16138496.html