|
脚本一启动的速度要快一些哦,因为脚本二要判断两次以后才启动哎
这两个一般配合keepalived使用
脚本一:
#!/bin/bash
#author:fansik
#description:check nginx service
run
=`ps -C nginx --no-header | wc -l`
if [ $run -eq 0 ]
then /etc/rc.d/init.d/nginx start
sleep 3
fi
脚本二:
#!/bin/bash
#author:fansik
#description:check nginx service
count
=0
for (( k=0; k<2; k++ ))
do check_code
=$( curl --connect-timeout 3 -sL -w "%{http_code}\\n" http://localhost/index.html -o /dev/null ) if [ "$check_code" != "200" ]; then
count=$(expr $count + 1)
sleep 3
continue
else
count=0
break
fi
done
if [ "$count" != "0" ]; then
/etc/init.d/nginx start
exit 1
else
exit 0
fi
keepalived的配置文件/etc/init.d/keepalived.conf:主配置文件如下,从配置文件将MASTER改成BACKUP,双主模型的时候在添加一个vrrp_instance VI_1主里面变成BACKUP,从里面变成MASTER,virtual_router_id变一下
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from fanjb@localhost
smtp_server
127.0.0.1 smtp_connect_timeout
30 router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script
"/etc/keepalived/nginx1.sh" interval
1 weight
-2 fall
2 rise
1
}
vrrp_script chk_notify {
script
"/etc/keepalived/chk_nginx.sh" interval
1 weight
-2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id
51 priority
99 advert_int
1 authentication {
auth_type PASS
auth_pass
1111 }
virtual_ipaddress {
10.10.10.250 }
track_script {
chk_nginx
chk_notify
}
notify_master
"/etc/keepalived/notify.sh master" notify_backup
"/etc/keepalived/notify.sh backup" notify_fault
"/etc/keepalived/notify.sh fault"
}
通知脚本:/etc/keepalived/nodify.sh
#!/bin/bash
#author:fansik
#description:check nging status
vip
=10.10.10.250
contact
='root@localhost'
notify() {
mailsubject
="`hostname` to be $1:$vip floating" mailbody
="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` change to be $1" echo $mailbody | mail -s "$mailsubject" $contact
}
case "$1" in master)
notify master
exit
0 ;;
backup)
notify backup
exit
0 ;;
fault)
notify fault
exit
0 ;;
*)echo "Usage:`basename $0` {master|backup|fault}" exit
1 ;;
esac |
|
|
|
|
|
|