4.Sersync实时同步

实时同步概述

1.什么是实时同步:只要当前目录发生变化则会触发一个事件,事件触发后将变化的目录同步至远程服务器;
2.为什么要实时同步:保证数据的连续性,减少人力维护成本,解决nfs单点故障
3.实时同步实现原理:实时同步需要借助inotify通知接口,用来监控目录的变化,如果监控的目录发生变更,则触发动作,这个动作可以是进行一次同步操作,或其他操作; 4.实时同步工具选择,有sersync、inotify+rsync,通常我们会选择sersync,因为sersync是国人基于rsync+inotify-tools开发的工具,不仅保留了有点同事还强化了试试监控,文件过滤,简化配置等功能,帮助用户提高运行效率,节省时间和网络资源。sersync项目地址

实时同步实践

案例:实现web上传视频文件,实则是写入NFS至存储,当NFS存在新的数据则会试试的复制到备份服务器

角色 外网IP(NAT) 内网IP(LAN) 安装工具
web01 eth0:10.0.0.7 eth1:172.16.1.7
nfs eth0:10.0.0.31 eth1:172.16.1.31 rsync+inotify+sersync
backup eth0:10.0.0.41 eth1:172.16.1.41 rsync

思路:实现web上传视频文件,实则是写入NFS至存储,当NFS存在新的数据则会实时的复制到备份服务器
1.web上传文件至nfs存储
2.web和nfs数据都备份到备份服务器的/backup
3.将nfs的数据实时同步到备份服务器的/data目录


1. nfs服务器

1.1 安装rsync

[root@nfs ~]# yum install nfs-utils -y

1.2 配置

[root@nfs ~]# cat /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

[root@nfs ~]# groupadd -g666 www
[root@nfs ~]# useradd -u666 -g666 www

[root@nfs ~]# mkdir /data
[root@nfs ~]# chown -R www.www /data

1.3 启动

[root@nfs ~]# systemctl restart nfs-server
[root@nfs ~]# sysytemctl enable nfs-server

2. web服务器

2.1 安装服务

[root@web01 ~]# yum install httpd php -y

2.2 配置

#进程运行的身份(最好是和nfs的匿名用户保持一致)
[root@web01 html]# sed -i '/^User/c User www' /etc/httpd/conf/httpd.conf    
[root@web01 html]# sed -i '/^Group/c Group www' /etc/httpd/conf/httpd.conf

#挂载
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /var/www/html        #核心

2.3 启动

[root@web01 ~]# systemctl start httpd

#上传代码
[root@web01 ~]# cd /var/www/html/
[root@web01 html]# rz kaoshi.zip
[root@web01 html]# unzip kaoshi.zip

2.4 修改上传大小

[root@web01 ~]# vim /etc/php.ini中设置:
upload_max_filesize = 200M;
post_max_size = 200M;

2.5 注意: 修改完配置记得重启服务

[root@web01 ~]#  systemctl restart httpd

3 backup服务器

3.1 安装

[root@backup ~]# yum install rsync -y

3.2 配置

[root@backup ~]# cat /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = true
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
path = /backup

[data]
path = /data

3.3 创建用户

[root@backup ~]# groupadd -g666 www
[root@backup ~]# useradd -u666 -g666 www

3.4 准备虚拟连接用户账号和密码

[root@backup ~]# cat /etc/rsync.passwd 
rsync_backup:123456
[root@backup ~]# chmod 600 /etc/rsync.passwd

3.5 创建数据存放的目录

[root@backup ~]# mkdir -p /data /backup
[root@backup ~]# chown -R www.www /data/ /backup/

3.6 启动

[root@backup ~]# systemctl restart rsyncd

4. 数据测试验证

#客户端执行脚本。测试rsync的备份是否ok   (客户端的数据都写入到/backup目录中) 172.16.1.7 172.16.1.31
[root@nfs ~]# sh /server/scripts/client_push_data.sh
sending incremental file list
nfs_172.16.1.31_2019-07-24/
nfs_172.16.1.31_2019-07-24/flag_2019-07-24
nfs_172.16.1.31_2019-07-24/other.tar.gz
nfs_172.16.1.31_2019-07-24/sys.tar.gz

sent 2,052 bytes  received 85 bytes  4,274.00 bytes/sec
total size is 1,817  speedup is 0.85

5. 数据实时同步

监控nfs服务器上面的/data目录,如果发生变化则触发动作,动作可以是执行一次同步;

5.1 服务安装

[root@nfs ~]# yum install inotify-tools       #监控工具
[root@nfs ~]# wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# rz -E sersync2.5.4_64bit_binary_stable_final.tar.gz   
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# mv GNU-Linux-x86/ /usr/local/sersync

5.2 服务配置

[root@nfs sersync]# cat confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>                                                          #调试模式
<fileSystem xfs="true"/>                                                        #Centos7默认是xfs文件系统,要开启
<filter start="false">                                                          #文件排除
    <exclude expression="(.*)\.svn"></exclude>
    <exclude expression="(.*)\.gz"></exclude>
    <exclude expression="^info/*"></exclude>
    <exclude expression="^static/*"></exclude>
</filter>
<inotify>                                                                       #文件变化类型
    <delete start="true"/>
    <createFolder start="true"/>
    <createFile start="true"/>
    <closeWrite start="true"/>
    <moveFrom start="true"/>
    <moveTo start="true"/>
    <attrib start="true"/>
    <modify start="true"/>
</inotify>

<sersync>
    <localpath watch="/data">                                                   #监控上面变化的目录
        <remote ip="172.16.1.41" name="data"/>                                  #变化后传到的目的模块
        <!--<remote ip="192.168.8.39" name="tongbu"/>-->
        <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    </localpath>                
    <rsync>
        <commonParams params="-az"/>                                            #rsync执行时加的参数
        <auth start="true" users="rsync_backup" passwordfile="/etc/password"/>          #虚拟用户和虚拟用户的密码
        <userDefinedPort start="false" port="874"/><!-- port=874 -->
        <timeout start="true" time="100"/><!-- timeout=100 -->                  #超时时间
        <ssh start="false"/>                                                    #ssh协议是否开启,走rsync协议,所以不需要
    </rsync>
    <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->    #错误日志地址
    <crontab start="false" schedule="600"><!--600mins-->
        <crontabfilter start="false">
            <exclude expression="*.php"></exclude>
            <exclude expression="info/*"></exclude>
        </crontabfilter>
    </crontab>
    <plugin start="false" name="command"/>
</sersync>

<plugin name="command">
    <filter start="false">
    </filter>
<plugin name="socket">
    <localpath watch="/opt/tongbu">
        <deshost ip="192.168.138.20" port="8009"/>
    </localpath>
</plugin>
<plugin name="refreshCDN">
    <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
        <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
        <sendurl base="http://pic.xoyo.com/cms"/>
        <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
    </localpath>
</plugin>
</head>

5.2 服务启动

[root@nfs sersync]# /usr/local/sersync/sersync2 -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
________________________________________________________________

#启动
[root@nfs sersync]# ./sersync2 -dro ./confxml.xml

#启动sersync后一定要提取同步的命令,手动运行一次,检查是否存在错误
[root@nfs ~]#  cd /data && rsync -az -R --delete ./  --timeout=100 [email protected]::data --password-file=/etc/rsync.password

#停止
[root@nfs sersync]# pkill sersync

6 数据服务迁移

如果nfs出现故障,怎么快速切换到backup服务器上,并且保证NFS数据和后续上传的文件都同步

项目需求: 最近涉及到数百万张图片从本地存储迁移到云存储,为了使完成图片迁移,并保证图片无缺失,业务不中断,决定采用实时同步,同步完后再做流量切换。在实时同步方案中进行了几种尝试。

项目总结:sersync对数百万张图片数据做到了实时同步,新增的数据能够立马同步到另一台,最后上百G的图片数据实现了在线迁移。

6.1 backup服务器上需要运行和nfs服务器上一样的业务环境

#创建用户
[root@backup ~]# groupadd -g 666 www
[root@backup ~]# useradd -u666 -g666 www
#配置
[root@backup ~]# yum install -y nfs-utils -y
[root@backup ~]# cat /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
#启动
[root@backup ~]# systemctl restart nfs-server

6.2 先实现实时的同步 √

6.3 在web上实现切换,卸载nfs的/data目录,重新挂载backup服务的/data目录

[root@web01 ~]# umount -lf /var/www/html/ && mount -t nfs 172.16.1.41:/data /var/www/html/

7. 数据同步总结

1.为什么要使用实时同步?
    1.解决nfs单点
    2.大量的静态资源迁移(本地迁移云端)
2.实时同步能解决什么问题?
    1.平滑的迁移
    2.备份:减少人为的干预
3.实时同步工具选择?
    rsync+inotify   少量文件同步,麻烦。同步大文件太慢,遍历扫描,非常影响效率。
    sersync         配置简单,多线程同步,同步块。适合大量的小文件或者图片。
    lsryncd 
4.demo:用户上传文件-->web-->写入-->nfs存储-->inotify-->action-->rsync--->backup
        用户上传文件-->web-->写入-->nfs存储(本地)--->实时的同步到-->存储(云端)
                     web-->卸载存储(本地)--->重新挂载存储(云端)

猜你喜欢

转载自www.cnblogs.com/chenmiao531759321/p/11435805.html