nginx+keeplved高可用之keeplived配置
1 nginx和keeplived安装,可参考 “Keepalived 双机热备”;
2 keeplived的配置文件:
master:
[root@master ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_DEVEL
}
vrrp_script check_run {
script"/home/check_nginx.sh"
weight -5
interval 5
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
auth_type PASS
auth_pass 1111
}
track_script {
check_run
}
virtual_ipaddress {
192.168.11.132/24 dev eth0
}
virtual_routes {
0.0.0.0/0.0.0.0 via192.168.11.132 dev eth0
}
}
3. Nginx监控脚本配置
监控脚本主要通过访问本地的80端口(nginx.conf里配置的监控url),去监控Nginx的服务状态,如果没有问题返回0,如果有问题返回1
#!/bin/bash
#This script is used by keepalived for checking nginx running status
CHECK_TIME=2
check()
{
curl -m 2 http://127.0.0.1/status >/dev/null 2>&1
return $?
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check
NGINX_OK=$?
if [ $NGINX_OK -eq 0 ];then
exit 0
fi
if [ $NGINX_OK -ne 1 ] && [ $CHECK_TIME -eq 0 ]
then
exit 1
fi
done
4. 编辑nginx配置文件,加入如下内容
location /status {
stub_status on;
access_log off;
以上只是主Keepalived的配置文件,可以将配置文件scp到从的上面,然后修改标注状态为MASTER,优先级小于100即可。
5.测试Nginx可以正常启动,访问本地的http://127.0.0.1/status可以正常提供服务,模拟宕机,另外一台接管。

浙公网安备 33010602011771号