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
}
- 更多的节点添加方式是一样的,这里就不再添加更多的了.
- 部署方案按照这个图来,Kafka那边暂时不用管.(图片参考自互联网)
安装RabbitMQ
- 要使用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
- 执行安装脚本安装RabbitMQ
./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.
- 注意,启动RabbitMQ之后要启动管理服务插件,否则15672管理页面无法登录
# 启用 rabbitmq_manager
sudo rabbitmq-plugins enable rabbitmq_management
- 由于guest用户默认只能在localhost登录,所以我们需要创建一个新的用户.
##设置账号密码
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] ...
- 到这里我们安装就完成了,并且可以通过15672端口访问到管理页面,并使用admin账户进行登录.
添加集群
- RabbitMQ集群之间通信确认是否可以通信,使用的是cookie,所以需要保证所有节点的cookie文件中的值一样.
- 由于我们没有开通Ubuntu系统的root账户,所以我们先通过scp命令将25节点的cookie文件发送到用户目录.
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/
- 发送成功后,登入到26,28服务器将该cookie文件覆盖到原安装的默认cookie文件
sudo mv ./.erlang.cookie /var/lib/rabbitmq/.erlang.cookie
- 完成后可以通过cat命令查看两个cookie是否一致.
- 注意,由于我们是由joe账户操作的文件,所以文件所有者发生了变化.导致RabbitMQ无法读取.所以这里还得改一下文件的所有者.
sudo chown rabbitmq /var/lib/rabbitmq/.erlang.cookie
- 然后配置各个节点的hosts文件.使其对应内容一致.
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
- 停止26,28节点的RabbitMQ服务.
# 停止服务
sudo rabbitmqctl stop_app
# 重置服务
sudo rabbitmqctl reset
# 加入时候设置节点为内存节点(默认加入的为磁盘节点)
sudo rabbitmqctl join_cluster rabbit@rabbitmq-01 --ram
#也通过下面方式修改的节点的类型,使用该命令前需要先停止当前节点.
sudo rabbitmqctl change_cluster_node_type disc | ram
- 搞好了后,即可通过管理页面查看
安装haproxy
- 首先我们根据官方地址来查看自己所属版本的命令,这里我是用的Ubuntu 20.04,所以执行如下命令,来安装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
- 接下来编辑haproxy配置信息
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
- 接下来启动haproxy服务,以及常用命令
# 启动服务
sudo systemctl start haproxy
# 查看状态
sudo systemctl status haproxy
# 重启服务
sudo systemctl restart haproxy
# 停止服务
sudo systemctl stop haproxy
- 通过查看服务状态确认无异常后,即可将haproxy服务加入开机自启
sudo systemctl enable haproxy
- 在一切都完成后,我们就可以登录配置的页面查看状态, http://192.168.2.27:8100/stats 由于我们配置了登录验证,输入刚才配置的账号密码(admin:123456)就可以进入页面了
标签:haproxy,192.168,sudo,rabbitmq,集群,RabbitMQ,Ubuntu,节点 来源: https://www.cnblogs.com/dygood/p/16138496.html