haproxy + rabbitmq + keepalived的高可用环境搭建

一、rabbitmq的搭建:参考rabbimq的安装及集群设置

二、安装和配置haproxy

1、安装haproxy
yum install haproxy

2、安装rsyslog
a. 检查rsyslog是否已安装
rpm -q rsyslog

b. 添加haproxy的log配置
cd /etc/rsyslog.d
vim haproxy.conf

=== 文件内容
$ModLoad imudp
$UDPServerRun 514
local2.* /var/log/haproxy.log
local3.* /var/log/haproxy.log
local10.* /var/log/haproxy.log
==========

c. 编辑/etc/sysconfig/rsyslog
vim /etc/sysconfig/rsyslog
添加内容:
SYSLOGD_OPTIONS="-c 2 -r -m 0"

d. 创建日志文件,并授权
cd /var/log
touch haproxy.log
chmod a+w haproxy.log

e. 重启rsyslog服务

用法: /etc/init.d/rsyslog {start|stop|restart|condrestart|try-restart|reload|force-reload|status}

 /etc/init.d/rsyslog start

3、打开防火墙端口,增加两个端口:

9188 用于haproxy的监控界面;5670 用于rabbimq的负载均衡端口

# 添加haproxy监控界面端口
# 打开9188,允许haproxy监控界面端口
iptables -I INPUT -p tcp --dport 9188 -j ACCEPT
# 打开9188,允许rabbimq负载均衡端口
iptables -I INPUT -p tcp --dport 5670 -j ACCEPT
/etc/rc.d/init.d/iptables save
/etc/init.d/iptables restart
/etc/init.d/iptables status

4、配置haproxy
vim /etc/haproxy/haproxy.cfg

=======================================================
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon

# turn on stats unix socket
stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
# 注意:因为要使用tcp的负载,屏蔽掉与http相关的默认配置
defaults
mode http
log global
# option httplog
option dontlognull
# option http-server-close
# option forwardfor except 127.0.0.0/8
option redispatch
retries 3
# timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
# timeout http-keep-alive 10s
timeout check 10s
maxconn 3000

# 打开haproxy的监测界面
listen status
bind 0.0.0.0:9188
mode http
stats enable
stats refresh 30s
stats uri /stats
stats auth admin:123456
stats realm (Haproxy\ statistic)
stats admin if TRUE

# 监听rabbimq_cluster
listen rabbitmq_cluster
bind 0.0.0.0:5670
mode tcp
balance roundrobin
server rabbit200 192.168.200.200:5672 check inter 5000 rise 2 fall 2
server rabbit199 192.168.200.199:5672 check inter 5000 rise 2 fall 2

=========================================================

5、 启动haproxy
haproxy -f /etc/haproxy/haproxy.cfg
/etc/init.d/haproxy restart|start

6、 停止haproxy
killall haproxy

 

三、安装和设置keepalived
1、安装keepalived
> yum install keepalived
> chkconfig --add keepalived
> chkconfig --level 35 keepalived on

2、修改配置文件
配置keepalived
/etc/keepalived/keepalived.conf
内容:
=====================================================================
! Configuration File for keepalived
# 全局配置,demo中配置了邮件信息,此处因为调试中,暂时没有添加
global_defs {

}

# 集群资源监控,组合track_script进行
vrrp_script check_haproxy {
script "killall -0 haproxy"
interval 2
}

vrrp_instance HAPROXY_HA {
# 设置当前主机为主节点,如果是备用节点,则设置为BACKUP
# 备用节点时,设置为:
# state BACKUP
state MASTER

# 指定HA监测网络接口,可以用ifconfig查看来决定设置哪一个
interface eth1

# 虚拟路由标识,同一个VRRP实例要使用同一个标识,主备机
virtual_router_id 80

# 因为当前环境中VRRP组播有问题,改为使用单播发送VRRP报文
# 这个地方需要关注,之前未做此设置,结果主备节点互相不能发现,因此主备节点都升级成了MASTER,并且绑定了VIP
# 备用节点时,内容为:
# unicast_src_ip 192.168.200.199
# unicast_peer {
# 192.168.200.200
# }
unicast_src_ip 192.168.200.200
unicast_peer {
192.168.200.199
}

# 设置优先级,确保主节点的优先级高过备用节点
# 备用节点时,设置为:
# priority 80
priority 100

# 用于设定主备节点间同步检查时间间隔
advert_int 2

# 设置高可用集群中不抢占功能,在主机down后,从机接管,当主机重新恢复后,设置此功能,备机将继续提供服务,从而避免因切换导致的隐患
nopreempt

# 设置主备节点间的通信验证类型及密码,同一个VRRP实例中需一致
authentication {
auth_type PASS
auth_pass 1234
}

# 当keepalived切换状态到MASTER时,执行脚本
notify_master "/etc/keepalived/master.sh"
# 当keepalived切换状态到BACKUP时,执行脚本
notify_backup "/etc/keepalived/backup.sh"
# 当keepalived切换状态到FAULT时,执行脚本
notify_fault "/etc/keepalived/fault.sh"
# 当keepalived切换状态到STOP时,执行脚本
notify_fault "/etc/keepalived/stop.sh"

# 集群资源监控,组合vrrp_script进行
track_script {
check_haproxy
}
# 设置虚拟IP地址,当keepalived状态切换为MASTER时,此IP会自动添加到系统中
# 当状态切换到BACKUP时,此IP会自动从系统中删除
# 可以通过命令ip add查看切换后的状态
virtual_ipaddress {
192.168.200.240/24 dev eth1
}
}
====================================================================

3、启动服务
因为是为了实现haproxy的高可用,启动时需要顺序启动:
(1) 启动haproxy:
> haproxy -f /etc/haproxy/haproxy.cfg
(2) 启动keeepalived:先启动master节点,后启动BACKUP节点
> /etc/init.d/keepalived start

4、停止keeepalived服务
> /etc/init.d/keepalived stop

5、检查keeepalived的运行日志,默认keepalived的日志位于/var/log/message
> tail -n100 /var/log/message


 

来自为知笔记(Wiz)


转载于:https://www.cnblogs.com/mincyw/p/4997284.html

你可能感兴趣的:(haproxy + rabbitmq + keepalived的高可用环境搭建)