nginx keepalived实现双机热备

说明:
Nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能。在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发,从而达到Master进程的存活高可靠性,Slave(worker)进程所有的业务信号都 由主进程发出,Slave(worker)进程所有的超时任务都会被Master中止,属于非阻塞式任务模型。

Keepalived是Linux下面实现VRRP 备份路由的高可靠性运行件。基于Keepalived设计的服务模式能够真正做到主服务器和备份服务器故障时IP瞬间无缝交接,作用主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现

环境:
master:192.168.45.86
slave:192.168.45.85
vip:192.168.45.254 虚拟ip
 
实现:
1.安装keepalived,主备服务器都一样

# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
# tar zxvf keepalived-1.2.2.tar.gz
# cd keepalived
# ./configure –prefix=/usr/local/keepalived
# make
# make install
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# mkdir /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# service keepalived start|stop #做成系统启动服务方便管理.

 
2.修改MASTER 的keepalived.conf

# vim /etc/keepalived/keepalived.conf  //添加以下内容
vrrp_script chk_http_port {
script "/usr/local/scripts/nginx_pid.sh" ###监控脚本
interval 2 ###监控时间
weight 2
}
vrrp_instance VI_1 {
state MASTER ### 设置为主
interface eth0 ### 监控网卡
virtual_router_id 51 ### 这个两台服务器必须一样
priority 101 ### 权重值MASTRE一定要高于BAUCKUP
authentication {
auth_type PASS ### 加密
auth_pass test ### 加密的密码,两台服务器一定要一样,不然会出错
}
track_script {
chk_http_port ### 执行监控的服务
}
virtual_ipaddress {
192.168.45.254 ### VIP 地址
}
}

 
3.修改BACKUP keepalived.conf

#vim /etc/keepalived/keepalived.conf
vrrp_script chk_http_port {
script "/opt/nginx_pid.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP ### 设置为备份机
interface eth0
virtual_router_id 51 ### 与 MASTRE 设置值一样
priority 80 ### 比 MASTRE权重值低authentication {
auth_type PASS
auth_pass test ### 密码 与 MASTRE 一样
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.45.254
}
}

 
4.master,backup上分别编写监控nginx监控脚本

#vim /usr/local/scripts/nginx_pid.sh

#!/bin/bash
A=`ps -C nginx --no-header |wc -l` ## 查看是否有nginx进程,把值赋给变量A
if [ "$A" == "0" ];then # 如果没有nginx进程,就结束keepalived进程
/usr/bin/killall keepalived
fi

 
测试:
测试截图不知道怎么上传上来。。。。。,这里就讲下测试步骤:
master服务器
关闭nginx进程,nginx_pid.sh脚本会执行killall keepalived,关闭keepalived进程。此时查看master的/var/log/messages日志,tail -10f /var/log/messages可以看到有master

backup部分,tail -10f /var/log/messages可以看到
Transition to master等条目,表示backup已经切换成master

master服务器再开启nginx,keepalived进程
查看backup服务器/var/log/messages可以看到,Receviced higher prio…..Entering Backup

有兴趣的,可以查看用网页试试,数秒内可以切换过去。

附录:
1.master服务器/var/log/messages一直重复出现信息:
Jul 15 06:32:15 lvs1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.45.254
Jul 15 06:32:16 lvs1 Keepalived_vrrp: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Jul 15 06:32:16 lvs1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.45.254
Jul 15 06:32:17 lvs1 Keepalived_vrrp: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Jul 15 06:32:17 lvs1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.45.254

解决1:
将防火墙关闭。考虑添加防火墙规则。





发表评论

邮箱地址不会被公开。 必填项已用*标注