keepalived对nginx高可用演练脚本
参考文章:http://deidara.blog.51cto.com/400447/302402/
、安装nginx、keepalived、epel-release源
yum install -y epel-release
yum install -y nginx
yum install -y keepalived 、配置好nginx 、设置keepalived配置文件
#主机
vi /etc/keepalived/keepalived.conf
vrrp_script chk_nginx {
script "/usr/local/nginx_pid.sh"
interval
fall #测试失败的次数
rise #测试成功的次数
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass eric
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.14.206
}
} #备机
vi /etc/keepalived/keepalived.conf
vrrp_script chk_nginx {
script "/usr/local/nginx_pid.sh"
interval
fall
rise
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass eric
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.14.206
}
} 、自备都建nginx_chk脚本
vi /opt/nginx_chk.sh
#!/bin/bash
# version 0.0.
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq ];then
/usr/local/nginx/sbin/nginx
sleep
if [ `ps -C nginx --no-header |wc -l` -eq ];then
killall keepalived ## 则结束 keepalived 进程
fi
fi chmod +x /opt/nginx_pid.sh 、启动nginx和keepalived
/etc/init.d/nginx start
/etc/init.d/keepalived start 、查看日志和ip
ip addr
tailf /var/log/messages 、故障演练
killall nginx
tailf /var/log/messages #查看两边的日志