一、环境
- 两台nginx
主:192.168.10.10
备:192.168.10.20 - 两台机子上安装好nginx和keppalive
二、配置主nginx
vim /etc/keepalived/keepalived.conf
- 修改打了备注的配置:
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
router_id LVS_DEVEL
}
vrrp_script check_nginx { #这里自己添加上去
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33 #这里修改为ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script { #这里自己添加上去
check_nginx
}
virtual_ipaddress {
192.168.10.101 #192.168.10为你自己的网关,后面随便设置
}
................
- 编写检测脚本
vim /etc/keepalived/check_nginx.sh
- 加入以下命令
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/webserver/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
- 服务执行脚本权限
chmod u+x /etc/keepalived/check_nginx.sh
三、配置备nginx
1、修改下列几项配置
四、测试
- 开启主nginx和备nginx中的nginx和keepalived服务
service keepalived restart
service keepalived restart
- 打开主nginx使用命令关闭主nginx
/usr/local/webserver/nginx/sbin/nginx -s stop
- 打开浏览器访问主nginx,如果能访问则说明配置成功