今天尝试配置 Nginx + Keepalive 做前端高可用。最开始在Openstack平台上做,发现keepalive配置的VIP在平台会被限制访问。然后就通过kvm直接创建了两台虚拟机,保证网络完全畅通。vip不受限制。
1,IP规划:
VIP:172.16.115.200
MASTER:172.16.115.201
BACKUP: 172.16.115.202
2,keepalive配置文件:(这里直接给配置文件,配置方法,网上很多,也很好)
-
! Configuration File for keepalived
-
-
global_defs {
-
notification_email {
-
pingshunbobo@sina.cn
-
}
-
-
notification_email_from pingshunbobo@sina.cn
-
smtp_server 127.0.0.1
-
smtp_connect_timeout 30
-
router_id LVS_DEVEL
-
-
}
-
-
vrrp_script check_run {
-
script "/opt/nginx_check.sh"
-
interval 2
-
weight -5
-
fall 3
-
rise 2
-
}
-
-
vrrp_instance VI_1 {
-
state MASTER #备机使用 BACKUP
-
interface eth0
-
virtual_router_id 51
-
mcast_src_ip 172.16.115.201 #备机使用172.16.115.202
-
priority 101 #备机使用 99 小于主机
-
advert_int 1
-
authentication {
-
auth_type PASS
-
auth_pass 1111
-
}
-
-
virtual_ipaddress {
-
172.16.115.200
-
}
-
-
track_script {
-
check_run
-
}
-
}
注意:keepalive配置文件中 { 前,通常有空格,省去会出错。
3,检测脚本:
-
#!/bin/sh
-
# check nginx server status
-
netstat -tlnp | grep 80 | grep nginx > /dev/null
-
if [ $? -ne 0 ];then
-
/etc/init.d/nginxa restart
-
sleep 3
-
netstat -tlnp | grep 80 | grep nginx > /dev/null
-
[ $? -ne 0 ] && /etc/init.d/keepalived stop
-
fi
4,原理说明:
脚本检测到本台机器的ng没有正常监听,直接关闭keepalive,backup机器接收不到advertisement会抢占使用VIP,提供服务。
阅读(2722) | 评论(0) | 转发(0) |