前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >rsync+inotify实现分布式集群部署

rsync+inotify实现分布式集群部署

作者头像
小柒2012
发布2018-04-13 14:56:16
1.3K0
发布2018-04-13 14:56:16
举报
文章被收录于专栏:IT笔记IT笔记IT笔记

上一篇:Linux下使用ssh密钥实现无交互备份

上二篇:Linux下使用rsync实现文件备份

缘起

由于公司要对支付做压测,于是使用了公司的服务器搭建了一个简单的集群环境,但是各个节点分别部署确实有点麻烦,由此产生了一下想法,使用rsync+inotify同步部署集群项目,就是主节点项目变更会自动同步到其他集群节点上。

优点

使用rsyn工具和inotify机制相结合,可以实现触发式部署(实时同步),只要原始(主)位置的文档发生变幻,则立即启用增量推送操作,否则处于静态等待状态,这样以来,就避免了分布式集群部署过程复杂的问题。

架构

项目采用springMvc+Dubbo(负载均衡集群)

这里使用了三台服务器

192.168.1.120 服务器A 192.168.1.130 服务器B 192.168.1.150 服务器C + 主节点

rsync+inotify同步逻辑图

111.jpg
111.jpg

安装配置

一、inotify-slave部署 (服务器A和B)

1)检查是否安装rsync工具 rpm -aq rsync

[root@itstyle]# rpm -aq rsync
rsync-3.0.9-17.el7.x86_64

一般来说服务器都自带rsync,没有请 yum intsall rsync -y 安装。

2) 编辑文件 vi /etc/rsyncd.conf

#工作中指定用户(可以不指定为0)
uid = 0
gid = 0
#相当于黑洞.出错定位
use chroot = no
#有多少个客户端同时传文件
max connections = 200
#超时时间
timeout = 300
#进程号文件
pid file = /var/run/rsyncd.pid
#日志文件
lock file = /var/run/rsync.lock
#日志文件
log file = /var/log/rsyncd.log
#模块开始
#模块名称随便起(可以是多个)
[acts_pay]
#需要同步的目录
path = /home/tomcat8/webapps/acts_pay
#表示出现错误忽略错误
ignore errors
#表示网络权限可写(本地控制真正可写)
read only = false
#这里设置IP或让不让同步
list = false
#指定允许的网段
hosts allow = 192.168.1.0/24
#拒绝链接的地址,一下表示没有拒绝的链接。
hosts deny = 0.0.0.0/32
#不要动的东西(默认情况)
#虚拟用户
auth users = rsync_pay
#虚拟用户的密码文件
secrets file = /etc/rsync.password

3)配置虚拟用户的密码文件

[root@inotify-slave /]# echo "rsync_slave:123456" >/etc/rsync.password
[root@inotify-slave /]# cat /etc/rsync.password
rsync_pay:123456   #注:rsync_pay为虚拟用户,123456为这个虚拟用户的密码
[root@inotify-slave /]# chmod 600 /etc/rsync.password #为密码文件提权,增加安全性

4)启动rsync 服务

[root@inotify-slave /]# rsync --daemon   #启动rsync服务
[root@inotify-slave /]# ps -ef |grep rsync
root     14871     1  0 14:24 ?        00:00:00 rsync --daemon
root     14873 14788  0 14:24 pts/0    00:00:00 grep rsync
[root@inotify-slave /]# netstat -lnutp |grep rsync
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      14871/rsync
tcp        0      0 :::873                      :::*                        LISTEN      14871/rsync

5)通过inotify-master测试推送(假设 已经安装并启动了rsync)

[root@inotify-master ~]# echo "123456" >/etc/rsync.password
[root@inotify-master ~]# cat /etc/rsync.password
123456   #注意:这里只要写密码即可,切记。
[root@inotify-master ~]# chmod 600 /etc/rsync.password
[root@inotify-master ~]# ll /etc/rsync.password
-rw------- 1 root root 7 4月  22 14:32 /etc/rsync.password
[root@inotify-master ~]# echo "hello itstyle">test.txt
[root@inotify-master ~]# cat test.txt
hello leesir
[root@inotify-master ~]# rsync -avz test.txt rsync_pay@192.168.1.120::acts_pay --password-file=/etc/rsync.password
sending incremental file list
test.txt
sent 82 bytes  received 27 bytes  72.67 bytes/sec
total size is 13  speedup is 0.12

inotify-slave检查:

[root@inotify-slave /]# ll /home/tomcat8/webapps/acts_pay/
总用量 4
-rw-r--r--. 1 rsync rsync 17 2月  28 14:34 test.txt
[root@inotify-slave /]# cat /home/tomcat8/webapps/acts_pay/test.txt
hello itstyle
二、 inotify-master部署

1) 查看当前系统是否支持inotify

[root@rmpapp]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 2月  28 10:15 max_queued_events
-rw-r--r-- 1 root root 0 2月  28 10:15 max_user_instances
-rw-r--r-- 1 root root 0 2月  28 10:15 max_user_watches

说明:

/proc/sys/fs/inotify/max_queued_evnets

表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。

/proc/sys/fs/inotify/max_user_instances 

表示每一个real user ID可创建的inotify instatnces的数量上限。

/proc/sys/fs/inotify/max_user_watches 

表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。

例如: echo 30000000 > /proc/sys/fs/inotify/max_user_watches

2)下载inotify源码包并编译安装

inotify是一种强大的、细粒度的、异步的文件系统事件控制机制。linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施监控的软件。

root@inotify-master tools]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz  #下载inotify源码包

[root@inotify-master tools]# tar zxf inotify-tools-3.14.tar.gz
[root@inotify-master tools]# cd inotify-tools-3.14
[root@inotify-master inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-3.14 #配置inotify,并指定安装路径为/usr/local/inotify-3.14
................................
[root@inotify-master inotify-tools-3.14]# make && make install

小技巧:执行make命令时,可以 make -j 4 #使用4个CPU同时编译。

3) inotify之inotifywait命令常用参数详解

[root@inotify-master inotify-tools-3.14]# cd /usr/local/inotify-3.14/
[root@inotify-master inotify-3.14]# ./bin/inotifywait --help
-r|--recursive   Watch directories recursively. #递归查询目录
-q|--quiet      Print less (only print events). #打印监控事件的信息
-m|--monitor   Keep listening for events forever.  Without this option, inotifywait will exit after one  event is received.        #始终保持事件监听状态
--excludei <pattern>  Like --exclude but case insensitive.    #排除文件或目录时,不区分大小写。
--timefmt <fmt> strftime-compatible format string for use with %T in --format string. #指定时间输出的格式
--format <fmt>  Print using a specified printf-like format string; read the man page for more details.
#打印使用指定的输出类似格式字符串
-e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s).  If omitted, all events are  listened for.   #通过此参数可以指定需要监控的事件,如下所示:
Events:
access           file or directory contents were read       #文件或目录被读取。
modify           file or directory contents were written    #文件或目录内容被修改。
attrib            file or directory attributes changed      #文件或目录属性被改变。
close            file or directory closed, regardless of read/write mode    #文件或目录封闭,无论读/写模式。
open            file or directory opened                    #文件或目录被打开。
moved_to        file or directory moved to 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     #文件或目录被删除
unmount         file system containing file or directory unmounted  #文件系统被卸载

4)主节点 inotify_pay.sh 脚本

#!/bin/bash
host120=172.16.1.120                           #inotify-slave的ip地址
src=/home/tomcat8_pay/webapps/acts_pay/         #本地监控的目录
inotify_home=/usr/local/inotify-3.14           #inotify的安装目录

#判断目录是否存在
if [ ! -e "$src" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ];
then

echo "Check File and Folder"
exit 9

fi

${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,modify,delete,create,attrib $src \
| while read file

do
 
 #把acts_pay目录下的所有文件推送到 120服务器 支付项目中 排除spring-context-dubbo.xml 由于host地址不同 无需同步
 rsync -az  --delete   ${src}   --exclude 'WEB-INF/classes/spring-context-dubbo.xml' rsync_pay@${host120}:acts_pay --password-file=/etc/rsync.password

done

exit 0

5)后台执行脚本

nohup sh inotify_pay & #后台运行,你关掉终端也会继续运行

6)测试

主节点创建一个文件 touch test.html

查看各个自己节点是否有更新。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-02-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 缘起
  • 优点
  • 架构
  • 安装配置
    • 一、inotify-slave部署 (服务器A和B)
      • 二、 inotify-master部署
      相关产品与服务
      负载均衡
      负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档