准备工作
搭建 RabbitMQ Server 单机版
搭建 RabbitMQ Server 高可用集群
搭建 HAProxy 负载均衡
一、准备工作
节点 | 系统 | 版本 | ip |
---|---|---|---|
node1 | centos7.2 | 1511 | 172.16.253.57 |
node2 | centos7.2 | 1511 | 172.16.253.14 |
node3 | centos7.2 | 1511 | 172.16.253.62 |
最好三台服务器都可以连网
1.node1:172.16.253.57
1.1修改主机名并修改hosts文件
hostnamectl set-hostname node1
echo "172.16.253.57 node1" >> /etc/hosts
echo "172.16.253.14 node2" >> /etc/hosts
echo "172.16.253.62 node3" >> /etc/hosts
1.2配置时间同步服务器
yum -y install ntp
vi /etc/ntp.conf
取消注释21-24行
#添加以下两行在25-26行
server 127.127.1.0
fudge 127.127.1.0 stratum 10
取消注释并修改17行 (允许网段)
restrict 172.16.253.0 mask 255.255.255.0 nomodify notra
#date -s 设置时间
systemctl restart ntpd
systemctl enable ntpd
reboot
2.node2:172.16.253.14
2.1修改主机名并修改hosts文件
hostnamectl set-hostname node2
echo "172.16.253.57 node1" >> /etc/hosts
echo "172.16.253.14 node2" >> /etc/hosts
echo "172.16.253.62 node3" >> /etc/hosts
2.2node2节点同步时间服务器
yum -y install ntp
ntpdate -u 172.16.253.57
echo "ntpdate -u 172.16.253.57" >> /etc/rc.d/rc.local
systemctl restart ntpd
systemctl enable ntpd
crontab -e
*/3 * * * * /usr/sbin/ntpdate -u 172.16.253.57 &> /dev/null
systemctl restart crond
systemctl enable crond
3.node3参照node2
reboot
二、搭建 RabbitMQ Server 单机版 (node1节点)
1.更新yum源
yum clean all
yum makecache
yum -y upgrade # 更新完之后内核版本本变为 7.4.1708
2.基础服务
2.1 安装RabbitMQ基础服务
wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install epel-release-latest-7.noarch.rpm
yum -y install erlang
2.2安装RabbitMQ:
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.6/rabbitmq-server-3.6.6-1.el7.noarch.rpm
yum -y install rabbitmq-server-3.6.6-1.el7.noarch.rpm
2.3启动mq
systemctl start rabbitmq-server
systemctl enable rabbitmq-server
systemctl status rabbitmq-server
2.4启动 RabbitMQ Web 管理控制台:
rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Applying plugin configuration to rabbit@node1... started 6 plugins.
2.5RabbitMQ Server 默认guest用户,只能localhost地址访问,我们还需要创建管理用户:
rabbitmqctl add_user admin admin123 && rabbitmqctl set_user_tags admin administrator && rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
Creating user "admin" ...
Setting tags for user "admin" to [administrator] ...
Setting permissions for user "admin" in vhost "/" ...
2.6开启防火墙并添加规则
systemctl start firewalld
systemctl status firewalld
firewall-cmd --zone=public --permanent --add-port=4369/tcp && \
firewall-cmd --zone=public --permanent --add-port=25672/tcp && \
firewall-cmd --zone=public --permanent --add-port=5671-5672/tcp && \
firewall-cmd --zone=public --permanent --add-port=15672/tcp && \
firewall-cmd --zone=public --permanent --add-port=61613-61614/tcp && \
firewall-cmd --zone=public --permanent --add-port=1883/tcp && \
firewall-cmd --zone=public --permanent --add-port=8883/tcp
一行显示为:
firewall-cmd --zone=public --permanent --add-port=4369/tcp && firewall-cmd --zone=public --permanent --add-port=25672/tcp && firewall-cmd --zone=public --permanent --add-port=5671-5672/tcp && firewall-cmd --zone=public --permanent --add-port=15672/tcp && firewall-cmd --zone=public --permanent --add-port=61613-61614/tcp && firewall-cmd --zone=public --permanent --add-port=1883/tcp && firewall-cmd --zone=public --permanent --add-port=8883/tcp
#重启防火墙:
firewall-cmd --reload
#success
2.7测试
修改本地hosts文件 添加 “172.16.253.57 node1 172.16.253.14 node2 172.16.253.62 node3” 三条记录
浏览器输入“172.16.253.57:15672” 或者“node1:15672”进入rabbitmq登陆页面
将上面的搭建过程,在node2、node3服务器上,再做重复一边。
三、搭建 RabbitMQ Server 高可用集群
一定要注意关启服务的顺序,否则很可能会报错!!!
1.将node2和node3 关闭节点和应用
rabbitmqctl stop
2.将node1服务器中的.erlang.cookie文件,拷贝到node2&&node3服务器上,保证相同的Erlang Cookie
find / -name ".erlang.cookie"
/var/lib/rabbitmq/.erlang.cookie
cat /var/lib/rabbitmq/.erlang.cookie
SMQKOGEBQYCPVHIOFANL
scp /var/lib/rabbitmq/.erlang.cookie root@node2:/var/lib/rabbitmq
scp /var/lib/rabbitmq/.erlang.cookie root@node3:/var/lib/rabbitmq
3.node2和node3上重新启动节点和应用
cat /var/lib/rabbitmq/.erlang.cookie 必须和node1一致
rabbitmq-server -detached
4.node2、node3执行---将node2节点加入到node1集群
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@node1
rabbitmqctl start_app
查看各个节点集群状态
rabbitmqctl cluster_status
Cluster status of node rabbit@node1 ...
[{nodes,[{disc,[rabbit@node1,rabbit@node2,rabbit@node3]}]},
{running_nodes,[rabbit@node2,rabbit@node3,rabbit@node1]},
{cluster_name,<<"rabbit@node1">>},
{partitions,[]},
{alarms,[{rabbit@node2,[]},{rabbit@node3,[]},{rabbit@node1,[]}]}]
Cluster status of node rabbit@node2 ...
[{nodes,[{disc,[rabbit@node1,rabbit@node2,rabbit@node3]}]},
{running_nodes,[rabbit@node1,rabbit@node3,rabbit@node2]},
{cluster_name,<<"rabbit@node1">>},
{partitions,[]},
{alarms,[{rabbit@node1,[]},{rabbit@node3,[]},{rabbit@node2,[]}]}]
Cluster status of node rabbit@node3 ...
[{nodes,[{disc,[rabbit@node1,rabbit@node2,rabbit@node3]}]},
{running_nodes,[rabbit@node2,rabbit@node1,rabbit@node3]},
{cluster_name,<<"rabbit@node1">>},
{partitions,[]},
{alarms,[{rabbit@node2,[]},{rabbit@node1,[]},{rabbit@node3,[]}]}]
5.在创建集群前后报错及解决方案
报错信息1:
rabbit@node2:
* connected to epmd (port 4369) on node2
* epmd reports: node 'rabbit' not running at all
no other nodes on node2
* suggestion: start the node
报错信息:2
Stopping and halting node rabbit@node2 ...
Error: unable to connect to node rabbit@node2: nodedown
DIAGNOSTICS
===========
attempted to contact: [rabbit@node2]
rabbit@node2:
* connected to epmd (port 4369) on node2
* epmd reports node 'rabbit' running on port 25672
* TCP connection succeeded but Erlang distribution failed
* suggestion: hostname mismatch?
* suggestion: is the cookie set correctly?
* suggestion: is the Erlang distribution using TLS?
解决方案:先强制杀死,再重启节点和应用
ps -ef |grep rabbitmq|grep -v grep |awk '{print $2}'|xargs kill -9
rabbitmq-server -detached
rabbitmqctl stop_app 仅关闭应用,不关闭节点
rabbitmqctl start_app 开启应用
rabbitmq--server -detached 启动节点和应用
rabbitmqctl 关闭节点和应用
6.web页面显示
四、搭建 HAProxy 负载均衡
1.由于RabbitMQ 本身不提供负载均衡,搭建 HAProxy,用作 RabbitMQ 集群的负载均衡。
2.HAProxy 安装在node1服务器上
yum -y install haproxy
3.配置 HAProxy:
#copy配置文件并修改
cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
vi /etc/haproxy/haproxy.cfg #删除以前代码并添加以下代码
global
log 127.0.0.1 local0 info
log 127.0.0.1 local1 notice
daemon
maxconn 4096
defaults
log global
mode tcp
option tcplog
option dontlognull
retries 3
option abortonclose
maxconn 4096
timeout connect 5000ms
timeout client 3000ms
timeout server 3000ms
balance roundrobin
listen private_monitoring
bind 0.0.0.0:8100
mode http
option httplog
stats refresh 5s
stats uri /stats
stats realm Haproxy
stats auth admin:admin
listen rabbitmq_admin
bind 0.0.0.0:8102
server node1 node1:15672
server node2 node2:15672
server node3 node3:15672
listen rabbitmq_cluster
bind 0.0.0.0:8101
mode tcp
option tcplog
balance roundrobin
timeout client 3h
timeout server 3h
server node1 node1:5672 check inter 5000 rise 2 fall 3
server node2 node2:5672 check inter 5000 rise 2 fall 3
server node3 node3:5672 check inter 5000 rise 2 fall 3
4.启动 HAProxy:
haproxy -f /etc/haproxy/haproxy.cfg
5.外部访问的话,需要关闭下防火墙:
systemctl stop firewalld.service
systemctl disable firewalld.service
6.负载均衡web显示
HAProxy 配置了三个地址:
http://node1:8100/stats:HAProxy 负载均衡信息地址,账号密码:admin/admin。
http://node1:8101:RabbitMQ Server Web 管理界面(基于负载均衡)。
http://node1:8102:RabbitMQ Server 服务地址(基于负载均衡)。
通过访问http://node1:8100/stats,查看 HAProxy 负载均衡信息:
至此,rabbitmq单机、集群、负载均衡都已配置完毕!