Ip |
节点说明 |
端口 |
管控台地址 |
192.168.17.17 |
rabbitmq master |
5672 |
http:// 192.168.17.17:15672 |
192.168.17.18 |
rabbitmq slave |
5672 |
http:// 192.168.17.18:15672 |
192.168.17.19 |
rabbitmq slave |
5672 |
http:// 192.168.17.19:15672 |
192.168.17.20 |
Haproxy+keepalived |
8100 |
http:// 192.168.17.20:8100/rabbitmq-stats |
192.168.17.21 |
Haproxy+keepalived |
8100 |
http:// 192.168.17.21:8100/rabbitmq-stats |
Ip |
Hostname |
Hosts |
192.168.17.17 |
rabbitmq1 |
192.168.17.17 rabbitmq1 192.168.17.18 rabbitmq2 192.168.17.19 rabbitmq3 |
192.168.17.18 |
rabbitmq2 |
192.168.17.17 rabbitmq1 192.168.17.18 rabbitmq2 192.168.17.19 rabbitmq3 |
192.168.17.19 |
rabbitmq3 |
192.168.17.17 rabbitmq1 192.168.17.18 rabbitmq2 192.168.17.19 rabbitmq3 |
192.168.17.20 |
ha1 |
192.168.17.17 rabbitmq1 192.168.17.18 rabbitmq2 192.168.17.19 rabbitmq3 |
192.168.17.21 |
ha2 |
192.168.17.17 rabbitmq1 192.168.17.18 rabbitmq2 192.168.17.19 rabbitmq3 |
注意修改vim /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.5/ebin/rabbit.app文件
修改:loopback_users 中的 <<"guest">>,只保留guest ,即{loopback_users, [guest]},
修改:heartbeat 为1
//首先启动服务
/etc/init.d/rabbitmq-server start stop status restart
//查看服务有没有启动:
lsof -i:5672或者 netstat -tnlp|grep 15672
[root@rabbitmq1 rabbitmq]# lsof -i:5672
beam 9208 rabbitmq 49u IPv6 33771 0t0 TCP *:amqp (LISTEN)
//启动管理插件
rabbitmq-plugins enable rabbitmq_management
出现如下错误:
-bash: lsof: command not found
通过yum安装
yum install lsof
组建集群前先停止所有节点的服务
[root@rabbitmq1 ~]# rabbitmqctl stop
Stopping and halting node rabbit@rabbitmq1 ...
[root@rabbitmq1 ~]# lsof -i:5672
[root@rabbitmq2 ~]# rabbitmqctl stop
Stopping and halting node rabbit@rabbitmq1 ...
[root@rabbitmq2 ~]# lsof -i:5672
[root@rabbitmq3 ~]# rabbitmqctl stop
Stopping and halting node rabbit@rabbitmq1 ...
[root@rabbitmq3 ~]# lsof -i:5672
查找.erlang.cookie文件
[root@rabbitmq1 ~]# cd /var/lib/rabbitmq
[root@rabbitmq1 rabbitmq]# ls -ia
529536 . 136 .. 705310 .erlang.cookie 17066434 mnesia
scp .erlang.cookie 192.168.17.18:/var/lib/rabbitmq
scp .erlang.cookie 192.168.17.19:/var/lib/rabbitmq
[root@rabbitmq1 ~]# rabbitmq-server -detached
Warning: PID file not written; -detached was passed.
[root@rabbitmq1 ~]# lsof -i:5672
[root@rabbitmq2 ~]# rabbitmq-server -detached
Warning: PID file not written; -detached was passed.
[root@rabbitmq2 ~]# lsof -i:5672
[root@rabbitmq3 ~]# rabbitmq-server -detached
Warning: PID file not written; -detached was passed.
[root@rabbitmq3 ~]# lsof -i:5672
注意,只操作slave节点,不操作主节点
[root@rabbitmq2 ~]# rabbitmqctl stop_app
[root@rabbitmq2 ~]# rabbitmqctl join_cluster rabbit@rabbitmq1
[root@rabbitmq2 ~]# rabbitmqctl start_app
[root@rabbitmq2 ~]# lsof -i:5672
[root@rabbitmq3 ~]# rabbitmqctl stop_app
[root@rabbitmq3 ~]# rabbitmqctl join_cluster rabbit@rabbitmq1
[root@rabbitmq3~]# rabbitmqctl start_app
[root@rabbitmq3 ~]# lsof -i:5672
//在另外其他节点上操作要移除的集群节点
rabbitmqctl forget_cluster_node rabbit@rabbitmq3
//任意节点修改
[root@rabbitmq1 ~]# rabbitmqctl set_cluster_name rabbitmq_cluster1
Setting cluster name to rabbitmq_cluster1 ...
//任意节点查看
[root@rabbitmq1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq1 ...
[{nodes,[{disc,[rabbit@rabbitmq1,rabbit@rabbitmq2,rabbit@rabbitmq3]}]}, //磁盘存储方式
{running_nodes,[rabbit@rabbitmq3,rabbit@rabbitmq2,rabbit@rabbitmq1]},//正在运行节点
{cluster_name,<<"rabbitmq_cluster1">>}, //集群名称
{partitions,[]},
{alarms,[{rabbit@rabbitmq3,[]},{rabbit@rabbitmq2,[]},{rabbit@rabbitmq1,[]}]}]
http://192.168.49.17:15672/
http://192.168.49.18:15672/
http://192.168.49.19:15672/
Nodes变为3个
设置镜像队列策略(在任意一个节点上执行
[root@rabbitmq1 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
Setting policy "ha-all" for pattern "^" to "{\"ha-mode\":\"all\"}" with priority "0" ...
将所有队列设置为镜像队列,即队列会被复制到各个节点,各个节点状态一致,RabbitMQ高可用集群就已经搭建好了,我们可以重启服务,查看其队列是否在从节点同步。
比如17的队列收到消息也会发送到18、19上,如果17挂了,18、19也可以处理消息
Haproxy是一款高可用性、负载均衡及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特别大的web站点,这些站点通常又需要会话保持或七层代理。HAProxy运行时下的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以简单安全的整合到架构中,同时保护web服务器不被暴露到网络上
//下载依赖包
yum install gcc vim wget
//下载haproxy
wget http://www.haproxy.org/download/1.6/src/haproxy-1.6.5.tar.gz
//解压
tar -zxvf haproxy-1.6.5.tar.gz -C /usr/local
//进入目录、进行编译、安装
cd /usr/local/haproxy-1.6.5
make TARGET=linux31 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
mkdir /etc/haproxy
//赋权
groupadd -r -g 149 haproxy
useradd -g haproxy -r -s /sbin/nologin -u 149 haproxy
//创建haproxy配置文件
touch /etc/haproxy/haproxy.cfg
vi /etc/haproxy/haproxy.cfg
#logging options
global
log 127.0.0.1 local0 info
maxconn 5120
chroot /usr/local/haproxy ##red
uid 99
gid 99
daemon
quiet
nbproc 20
pidfile /var/run/haproxy.pid
defaults
log global
#使用4层代理模式,”mode http”为7层代理模式
mode tcp
#if you set mode to tcp,then you nust change tcplog into httplog
option tcplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5s
##客户端空闲超时时间为 60秒 则HA 发起重连机制
clitimeout 60s ##red
##服务器端链接超时时间为 15秒 则HA 发起重连机制
srvtimeout 15s ##red
#front-end IP for consumers and producters
listen rabbitmq_cluster
bind 0.0.0.0:5672 ##red
#配置TCP模式
mode tcp
#balance url_param userid
#balance url_param session_id check_post 64
#balance hdr(User-Agent)
#balance hdr(host)
#balance hdr(Host) use_domain_only
#balance rdp-cookie
#balance leastconn
#balance source //ip
#简单的轮询
balance roundrobin
#rabbitmq集群节点配置 #inter 每隔五秒对mq集群做健康检查, 2次正确证明服务器可用,2次失败证明服务器不可用,并且配置主备机制
server bhz76 192.168.17.17:5672 check inter 5000 rise 2 fall 2 ##red
server bhz77 192.168.17.18:5672 check inter 5000 rise 2 fall 2 ##red
server bhz78 192.168.17.19:5672 check inter 5000 rise 2 fall 2 ##red
#配置haproxy web监控,查看统计信息
listen stats
bind 192.168.17.20:8100 ##red
mode http
option httplog
stats enable
#设置haproxy监控地址为http://localhost:8100/rabbitmq-stats
stats uri /rabbitmq-stats ##red
stats refresh 5s
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
//查看haproxy进程状态
ps -ef | grep haproxy
访问如下地址可以对rmq节点进行监控:http://192.168.17.20:8100/rabbitmq-stats
killall haproxy
ps -ef | grep haproxy
//如果关闭出现异常
[root@rabbitmq2 etc]# killall haproxy
-bash: killall: 未找到命令
[root@rabbitmq2 etc]# yum install psmisc
Keepalived,它是一个高性能的服务器高可用或热备解决方案,Keepalived主要来防止服务器单点故障的发生问题,可以通过其与Nginx、Haproxy等反向代理的负载均衡服务器配合实现web服务端的高可用。Keepalived以VRRP协议为实现基础,用VRRP协议来实现高可用性(HA).VRRP(Virtual Router Redundancy Protocol)协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成一个设备,对外提供虚拟路由器IP(一个或多个)。
管理LVS负载均衡软件
实现LVS集群节点的健康检查中
作为系统网络服务的高可用性(failover)
Keeplived高可用服务对之间的故障切换转移,是通过VRRP(虚拟路由器冗余协议)来实现的。在keeplived服务正常工作时,主Master会不断向备节点发送(多播的方式)心跳消息,用以告诉备Backup节点自己还活着,当主Master节点发生故障时,无法发送心跳消息,备节点也就因此无法继续监测到来自主Master节点的心跳了,于是调用自身的接管程序,接管主Master节点的IP资源及服务。当主Master节点恢复时,备Backup节点又会释放主节点故障时自身接管的IP资源及服务,恢复到原来备用的角色
全称Virtual Router Redundancy Rrotocol,中文名为虚拟路由冗余协议,VRRP的出现就是为了解决静态路由的单点故障问题,VRRP是通过一种竞选机制来将路由的任务交给某台VRRP路由器的
//安装所需软件包,否则编译时报错
yum install -y openssl openssl-devel
//下载
wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz
//解压、编译、安装
tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/
cd /usr/local/keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived
make && make install
//将keepalived安装成Linux系统服务,因为没有使用keepalived的默认安装路径(默认路径:/usr/local),安装完成之后,需要做一些修改工作
//首先创建文件夹,将keepalived配置文件进行复制:
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
//然后复制keepalived脚本文件:
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
//可以设置开机启动:chkconfig keepalived on,到此我们安装完毕!
chkconfig keepalived on
安装软连接时,如出现类似异常,做如下类似处理
[root@ha1 keepalived-1.2.18]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/
ln: 无法创建符号链接"/sbin/keepalived": 文件已存在
[root@ha1 keepalived-1.2.18]# rm -f /sbin/keepalived
[root@ha1 keepalived-1.2.18]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/
修改keepalived.conf配置文件
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id ha1 ##标识节点的字符串,通常为hostname ##red
}
vrrp_script chk_haproxy {
script "/etc/keepalived/haproxy_check.sh" ##执行脚本位置,自己要写的 ##red
interval 2 ##检测时间间隔
weight -20 ##如果条件成立则权重减20
}
vrrp_instance VI_1 {
state MASTER ## 主节点为MASTER,备份节点为BACKUP ##red
interface ens32 ## 绑定虚拟IP的网络接口(网卡),与本机IP地址所在的网络接口相同(我这里是eth0)。ip a ##red
virtual_router_id 20 ## 虚拟路由ID号(主备节点一定要相同) ##red
mcast_src_ip 192.168.17.20 ## 本机ip地址 ##red
priority 100 ##优先级配置(0-254的值) ##red
nopreempt
advert_int 1 ## 组播信息发送间隔,俩个节点必须配置一致,默认1s
authentication { ## 认证匹配
auth_type PASS
auth_pass bhz
}
track_script {
chk_haproxy
}
virtual_ipaddress {
192.168.17.30 ## 虚拟ip,可以指定多个 ##red
}
}
! Configuration File for keepalived
global_defs {
router_id ha2 ##标识节点的字符串,通常为hostname ##red
}
vrrp_script chk_haproxy {
script "/etc/keepalived/haproxy_check.sh" ##执行脚本位置 ##red
interval 2 ##检测时间间隔
weight -20 ##如果条件成立则权重减20
}
vrrp_instance VI_1 {
state BACKUP ## 主节点为MASTER,备份节点为BACKUP ##red
interface ens32 ## 绑定虚拟IP的网络接口(网卡),与本机IP地址所在的网络接口相同(我这里是eno16777736)。ip a ##red
virtual_router_id 20 ## 虚拟路由ID号(主备节点一定要相同) ##red
mcast_src_ip 192.168.17.21 ## 本机ip地址 ##red
priority 90 ##优先级配置(0-254的值) ##red
nopreempt
advert_int 1 ## 组播信息发送间隔,俩个节点必须配置一致,默认1s
authentication { ## 认证匹配
auth_type PASS
auth_pass bhz
}
track_script {
chk_haproxy
}
virtual_ipaddress {
192.168.17.30 ## 虚拟ip,可以指定多个 ##red
}
}
添加文件位置为/etc/keepalived/haproxy_check.sh(79、80两个节点文件内容一致即可
#!/bin/bash
COUNT=`ps -C haproxy --no-header |wc -l`
if [ $COUNT -eq 0 ];then
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
sleep 2
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
解释:
如果ha服务不存在,则启动ha服务,如果发现ha服务未启动成功,直接停掉keepalvie服务
赋权
脚本授权,赋予可执行权限.
chmod +x /etc/keepalived/haproxy_check.sh
//启动两台机器的keepalived
service keepalived start | stop | status | restart
//查看状态
ps -ef | grep haproxy
ps -ef | grep keepalived
vip在20节点上
停掉20节点的服务
service keepalived stop
查单21节点
此时,VIP漂移到了21节点上,那么21节点的haproxy可以继续对外提供服务!