一点触发多点生效

来源【原创】 / 1158人浏览
时间:2012-03-02 11:43:00
0

这个是我实际应用中目前正在使用的一套高效脚本

在inotify+rsync的基础上的实际应用脚本。
环境:4台nginx服务器(A、B、C、D),对nginx配置的修改、生效,拓扑如图所示。

功能介绍:

一台nginx配置修改,其他3台自动同步修改;对配置生效结果检测,若没问题,使用一条命令,使得其他三台服务器同时生效。原理:全程配合需要有三块脚本功能支撑
 触发端服务器(A)上包含两块功能三个脚本,实时同步nginx.conf的脚本(rsyncNginx.sh)和同步生效开关文件的脚本(rsyncSwitch.sh)、命令脚本(allNginxEffective.sh)发出全部生效命令。
 其他服务器(B、C、D)各包含一条实时检查生效开关的脚本(rsyncEffective.sh),一单生效开关文件发生变动则执行nginx配置文件重新加载命令。


二:基础支持服务安装
 1、rsync服务
 2、inotify-tools
 以上两个服务很好安装,前面我已经有安装配置博客。


三、脚本编辑,这里我直接给出脚本
工作目录在/root/work/inotify下
1、A服务器
脚本:rsyncNginx.sh : 一单A服务器上的nginx.conf 发生变动,则实时同步到B、C、D服务器上。

#!/bin/bash
src=/usr/local/nginx/conf/
des=inotifyhome
ip=172.X.X.2 172.X.X.4 172.x.x.5
user=sysman
/usr/local/bin/inotifywait -mrq -e modify $src |  while read file
do
    for i in $ip
    do
        rsync  -zrtopgl  --delete --progress  --include=nginx.conf  --password-file=/etc/rsyncd.pwd  $src $user@$i::$des
done
done
#
#

#####################################################################################


脚本:rsyncSwitch.sh : 一单A服务器上的开关文件switch.txt 发生变动,则实时同步到B、C、D服务器上。

这里的switchHome目录和switch.txt文件需要手动创建,                

#!/bin/bash
 switchHome=/root/work/inotify/switchHome/
 desSW=nginxSwitch
 ip=172.X.X.2 172.X.X.4 172.x.x.5
 user=sysman
 /usr/local/bin/inotifywait -mrq -e modify  $switchHome |  while read file
 do
   for i in $ip
   do
rsync  -zrtopgl  --delete --progress  --include=switch.txt  --password-file=/etc/rsyncd.pwd  $switchHome  $user@$i::$desSW
   done
 done

#####################################################################################


脚本:allNginxEffective.sh :一个修改switch.txt文件的脚本,作用是用来发出命令,使其他服务器nginx.conf同时生效。              


#!/bin/bash
#this script is rsync exec  the nginx.conf to effective
 date=`date +%Y%m%d%T`
 echo $date >> /root/work/inotify/switchHome/switch.txt
#
#

#####################################################################################




2、另外三台B、C、D 服务器上均为同一个脚本

脚本:rsyncEffective.sh : 用来监控B、C、D服务器上的开关文件switch.txt,该文件一但发生变动,则执行nginx配置文件重新加载命令
 (从严谨的角度考虑,配置文件重新加载前检查一下文件的正确性,如果正确则加载,若错误则不加载,并发送一个报警信息到手机上)。                

#!/bin/bash
src=/root/work/inotify/switchHome/
/usr/local/bin/inotifywait -rmq  -e modify $src |  while read  event
do
Yes=`/usr/local/nginx/sbin/nginx -t 2> temp.txt ; grep successful temp.txt  | wc -l ; rm -rf  temp.txt`
if [ $Yes -eq 1 ];then
kill -HUP `ps -ef |grep master |grep -v grep | awk '{print $2}'`
else
   echo `hostname`:nginx.conf configure error.  | mutt -s warring   151xxxxxxx@139.com
fi
done

#####################################################################################

应用的关键是思路,可以根据这个思路衍生出其他各式的按需要的脚本以上为个人拙笔,欢迎高手提出意见


2012-06-18补充:

实践应用中遇到脚本莫名奇妙的消失,于是写个了类似守护进程来检查几个脚本是否存在

[root@nginx-m inotify]# cat rsyncdaemon.sh
#!/bin/bash
function rsyncSwitchdaemon
{
rsyncSwitchCount=` ps -ef | grep rsyncSwitch | grep -v grep  | wc -l`
if [ $rsyncSwitchCount -ne 2 ] ;then
        if [ $rsyncSwitchCount -eq 0 ];then
           (/root/work/inotify/rsyncSwitch.sh )
        else if [ $rsyncSwitchCount -eq 1 ];then
                kill -9 `ps -ef |grep  rsyncSwitch | grep -v grep | awk '{print $2}'`                                                       (/root/work/inotify/rsyncSwitch.sh )
             fi
        fi
fi
}
function rsyncNginxdaemon
{
rsyncNginxCount=` ps -ef | grep rsyncNginx | grep -v grep  | wc -l`
if [ $rsyncNginxCount -ne 2 ] ;then
        if [ $rsyncNginxCount -eq 0 ];then
           (/root/work/inotify/rsyncNginx.sh )
        else if [ $rsyncNginxCount -eq 1 ];then
                kill -9 `ps -ef |grep  rsyncNginx | grep -v grep | awk '{print $2}'`                                                           (/root/work/inotify/rsyncNginx.sh )
             fi
        fi
fi
}
rsyncSwitchdaemon
rsyncNginxdaemon
#
#

#####################################################################################

加入到定时任务中   0  0  *  *  *  /root/work/inotify/rsyncdaemon.sh

打赏

站内搜索

搜索文章