• Nginx+keepalived 双机主从模式下修改主Nginx自动同步nginx.conf配置文件至备Nginx


    Nginx+keepalived 双机主从模式下修改主Nginx自动同步nginx.conf配置文件至备Nginx

    前言

    上个月搞定了现在终于抽空记录下。

    (东西是越积越多啊...)

    公司目前生产的Nginx都是Nginx+keepalived的主从架构,自动让我开始运维Nginx后才知道对于主Nginx修改配置后,备Nginx是没有进行同步的。

    上次要求我写定时任务每天自动定时同步一次主Nginx配置到备上边去,我提出搞成一旦有修改动作就同步过去备Nginx的方式。

    于是开搞去解决需求。

    写到这里突然发现目前虽然实现主Nginx修改配置后自动传备机但是传过去后备机不会自动nginx -s reload去生效配置欸......

    这样子的话主Nginx一旦挂了还是没用,需求运维人员手工先去reload生效配置......

    再找时间搞一下吧。

    实现

    自动传输的话肯定是需要对文件进行监控,可以使用命令inotifywait实现。

    需要使用如下方式安装:

    CentOS和RedHat的ISO镜像没有这个rpm包,可以从同版本的Oracle Linux的ISO镜像拷贝安装。或者自己从可信赖的网站下载。

    [root@server soft]# rpm -ivh inotify-tools-3.14-1.el6.x86_64.rpm 
    warning: inotify-tools-3.14-1.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
    Preparing...                ########################################### [100%]
       1:inotify-tools          ########################################### [100%]
    [root@server soft]# which inotifywait
    /usr/bin/inotifywait

    先把最终的脚本放在这里,

    vim /monitorNG/monitor_NGconfigchange.sh
    #!/bin/bash
      inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f %e' --event create  /usr/local/tengine/conf | while read  date time file event
      do
          case $event in
              CREATE)
                    case $file in 
                          /usr/local/tengine/conf/nginx.conf)
                               echo [`date '+%Y-%m-%d %H:%M:%S'] ` $event' - '$file >> /monitorNG/watch.log
                               scp /usr/local/tengine/conf/nginx.conf 192.168.1.100:/usr/local/tengine/conf
                        ;;
                  esac
                  ;;
          esac
      done

    其中nginx.conf这个配置文件的路径为/usr/local/tengine/conf,脚本是对整个文件夹做监控而不是单独对nginx.conf文件。

    为什么?

    因为vi/vim由于对文件的编辑方式上比较特殊,vi/vim是会创建中间文件并替换目标文件,因此若是监控这个文件后续由于文件实际上被替换了因此监控不在生效。

    比如我这里对已经存在的文件/soft/test.txt做监控,然后vi/vim这个文件并且修改东西保存退出可以看到如下捕捉到的监控信息。

    会话1:

    [root@server soft]# inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f %e' /soft/test.txt 
    等待输出......
    参数说明
    -m 持续监听
    -r 使用递归形式监视目录
    -q 减少冗余信息,只打印出需要的信息
    -e 指定要监视的事件,多个时间使用逗号隔开
    –timefmt 时间格式
    –format 监听到的文件变化的信息

    这里不写-e,表示监控所有的事件。

    会话2:vi/vim修改东西并保存。

    [root@server soft]# vi test.txt
    sadkfjasdfasdf
    wq!保存退出

    这时的会话1产生如下信息:

    [root@server soft]# inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f %e' /soft/test.txt 
    18/08/21 22:40 /soft/test.txt OPEN
    18/08/21 22:40 /soft/test.txt ACCESS
    18/08/21 22:40 /soft/test.txt CLOSE_NOWRITE,CLOSE
    18/08/21 22:40 /soft/test.txt MOVE_SELF
    18/08/21 22:40 /soft/test.txt ATTRIB
    18/08/21 22:40 /soft/test.txt DELETE_SELF

    可以看到有move_self以及delete_self操作。

    [root@server soft]# inotifywatch  -h
    ...省略部分输出...
    Events:
            access          file or directory contents were read
            modify          file or directory contents were written
            attrib          file or directory attributes changed
            close_write     file or directory closed, after being opened in
                            writeable mode
            close_nowrite   file or directory closed, after being opened in
                            read-only mode
            close           file or directory closed, regardless of read/write mode
            open            file or directory opened
            moved_to        file or directory moved to watched directory
            moved_from      file or directory moved from watched directory
            move            file or directory moved to or from watched directory
            create          file or directory created within watched directory
            delete          file or directory deleted within watched directory
            delete_self     file or directory was deleted
            unmount         file system containing file or directory unmounted

    可以看到delete_self表示有文件或者目录被删除。

    后续会话2在此vi/vim修改文件是不会有任何监控信息了。

    这是由于vi/vim的问题导致的,若是echo等命令编辑文件则不会有任何问题。

    于是,只能对整个目录做监控,比如我这里对目录/soft/做监控,然后vi/vim文件/soft/test.txt并且修改东西保存退出可以看到捕捉到的监控信息,过程如下:

    会话1:

    [root@server soft]# inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f %e' /soft

    会话2:vi/vim修改东西并保存。

    [root@dm8 soft]# inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f %e' /soft
    18/08/21 22:51 /soft/ OPEN,ISDIR
    18/08/21 22:51 /soft/ CLOSE_NOWRITE,CLOSE,ISDIR
    18/08/21 22:51 /soft/ OPEN,ISDIR
    18/08/21 22:51 /soft/ CLOSE_NOWRITE,CLOSE,ISDIR
    18/08/21 22:51 /soft/ OPEN,ISDIR
    18/08/21 22:51 /soft/ CLOSE_NOWRITE,CLOSE,ISDIR
    18/08/21 22:51 /soft/ OPEN,ISDIR
    18/08/21 22:51 /soft/ CLOSE_NOWRITE,CLOSE,ISDIR
    18/08/21 22:51 /soft/test.txt OPEN
    18/08/21 22:51 /soft/.test.txt.swp CREATE
    18/08/21 22:51 /soft/.test.txt.swp OPEN
    18/08/21 22:51 /soft/.test.txt.swx CREATE
    18/08/21 22:51 /soft/.test.txt.swx OPEN
    18/08/21 22:51 /soft/.test.txt.swx CLOSE_WRITE,CLOSE
    18/08/21 22:51 /soft/.test.txt.swx DELETE
    18/08/21 22:51 /soft/.test.txt.swp CLOSE_WRITE,CLOSE
    18/08/21 22:51 /soft/.test.txt.swp DELETE
    18/08/21 22:51 /soft/.test.txt.swp CREATE
    18/08/21 22:51 /soft/.test.txt.swp OPEN
    18/08/21 22:51 /soft/.test.txt.swp MODIFY
    18/08/21 22:51 /soft/.test.txt.swp ATTRIB
    18/08/21 22:51 /soft/test.txt ACCESS
    18/08/21 22:51 /soft/test.txt CLOSE_NOWRITE,CLOSE
    18/08/21 22:51 /soft/.test.txt.swp MODIFY
    18/08/21 22:51 /soft/4913 CREATE
    18/08/21 22:51 /soft/4913 OPEN
    18/08/21 22:51 /soft/4913 ATTRIB
    18/08/21 22:51 /soft/4913 CLOSE_WRITE,CLOSE
    18/08/21 22:51 /soft/4913 DELETE
    18/08/21 22:51 /soft/test.txt MOVED_FROM
    18/08/21 22:51 /soft/test.txt~ MOVED_TO
    18/08/21 22:51 /soft/test.txt CREATE
    18/08/21 22:51 /soft/test.txt OPEN
    18/08/21 22:51 /soft/test.txt MODIFY
    18/08/21 22:51 /soft/test.txt CLOSE_WRITE,CLOSE
    18/08/21 22:51 /soft/test.txt ATTRIB
    18/08/21 22:51 /soft/test.txt ATTRIB
    18/08/21 22:51 /soft/.test.txt.swp MODIFY
    18/08/21 22:51 /soft/test.txt~ DELETE
    18/08/21 22:51 /soft/.test.txt.swp CLOSE_WRITE,CLOSE
    18/08/21 22:51 /soft/.test.txt.swp DELETE

    这下就更清楚了,vi/vim产生中间的隐藏文件.test.txt.swp,在保存动作完成后删除test.txt并重建text.txt。

    更具体的可以查资料,这里只需要确定原text.txt被删除过就行了。

    再回头看脚本:

    vim /monitorNG/monitor_NGconfigchange.sh
    #!/bin/bash
      inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f %e' --event create  /usr/local/tengine/conf | while read  date time file event
      do
          case $event in
              CREATE)
                    case $file in 
                          /usr/local/tengine/conf/nginx.conf)
                               echo [`date '+%Y-%m-%d %H:%M:%S'] ` $event' - '$file >> /monitorNG/watch.log
                               scp /usr/local/tengine/conf/nginx.conf 192.168.1.100:/usr/local/tengine/conf
                        ;;
                  esac
                  ;;
          esac
      done

    对nginx.conf所在目录进行监控,并且只监控create事件。

    同时做判断,如果create时间触发时对应的文件为/usr/local/tengine/conf/nginx.conf,则将相关信息记录到/monitorNG/watch.log日志中并且将修改后的nginx.conf传到备库的某个目录下。

    watch.log样例截图如下,可以知道什么时候对文件进行过修改:

    另外有一点忘记了,主备服务器是需要配置互信的,不然scp文件到远程每次都需要输入密码,对于通过脚本实现的话就有问题了。

    由于我是oracle dba,互信这个事情当然是交给oracle数据库安装包里边的sshUserSetup.sh脚本啦!

    该脚本通过输入参数可以实现互信,大大简略了步骤。

    ./sshsetup/sshUserSetup.sh -user root -hosts "server1 server2" -advanced -noPromptPassphrase
    过程根据提示输入几次对应服务器的root密码就行了。

    当然,也可以手工进行互信,百度吧。

    最后,设置开机自动运行脚本进行监控:

    vi /etc/rc.local
    nohup sh /monitorNG/monitor_NGconfigchange.sh >> /tmp/inotify.log 2>&1  &
  • 相关阅读:
    Http请求头与响应头
    获取ip位置方法
    简单的Http Server实现
    HTTP
    long、int与byte数组之间的相互转换
    GlusterFS简单配置
    创建线程池
    网络编程socket
    面向对象-进阶篇
    面向对象-初级篇
  • 原文地址:https://www.cnblogs.com/PiscesCanon/p/15158998.html
Copyright © 2020-2023  润新知