第九周-day39-Sersync 实时同步实战

在这里插入图片描述

一、sersync实时同步

读弃用:-提高网站并发访问量高的方法

sersync实时同步-提高网站并发访问量的方法

介绍

1.Sersync 项目利用 Inotify 和 Rsync 技术实现对服务器数据实时同步的解决方案,其中 Inotify 用于监控 Sersync 所在服务器上文件系统的事件变化,而 Rsync 是目前广泛使用的本地以及异地数据同步工具,其优点是只对变化的目录数据操作,甚至是一个文件不同的部分进行同步,所以其优势大大超过使用挂接文件系统或 scp 等方式进行镜像同步。(国人周洋)

2.同步工具目前使用比较多的同步工具为 Inotify-tools 和 Openduckbill,sersync。Sersync 优于 Inotify-tools 和 Openduckbill

sersync服务要放到nfs的服务器上


实时同步实践

1.创建俩个目录用来存放软件

mkdir -p /server/{scripts,tools}

下载安装此压缩包并解压
sersync_installdir_64bit.zip
#####压缩包链接: 提取码: 7kur

[15:21 root@nfs01 /server/tools]# unzip sersync_installdir_64bit.zip
Archive:  sersync_installdir_64bit.zip
   creating: sersync_installdir_64bit/
   creating: sersync_installdir_64bit/sersync/
   creating: sersync_installdir_64bit/sersync/bin/
  inflating: sersync_installdir_64bit/sersync/bin/sersync  
   creating: sersync_installdir_64bit/sersync/conf/
  inflating: sersync_installdir_64bit/sersync/conf/confxml.xml  
   creating: sersync_installdir_64bit/sersync/logs/

查看一下

[15:21 root@nfs01 /server/tools]# tree
.
├── sersync_installdir_64bit
│   └── sersync
│       ├── bin
│       │   └── sersync
│       ├── conf
│       │   └── confxml.xml
│       └── logs
└── sersync_installdir_64bit.zip    \\此文件

2.创建目录,把文件移动到/app下

mkdir -p /app
mv /server/tools/sersync_installdir_64bit/sersync /app/

查看一下

[15:27 root@nfs01 ~]# tree /app/
/app/
├── logs
└── sersync
    ├── bin
    │   └── sersync
    ├── conf
    │   └── confxml.xml
    └── logs

3.添加执行权限,给此文件创建一个软链接

执行完成后试着敲一下sersync的命令
指定软连接使他变为命令(用全路径可以执行,为让他变的方便。)使他变为命令

[15:27 root@nfs01 ~]#chmod  +x /app/sersync/bin/sersync
[15:27 root@nfs01 ~]#
[15:27 root@nfs01 ~]# ln -s /app/sersync/bin/sersync  /sbin/
[15:29 root@nfs01 ~]# sersync 
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
daemon thread num: 10
parse xml config file
XML Parsing error inside file 'confxml.xml'.
Error: File not found
At line 0, column 0.

4.去rsync的配置文件添加nfsbackup模块

[15:29 root@nfs01 ~]#vim /etc/rsyncd.conf
....
#####################################
[nfsbackup]
comment = www by old0boy 13:14 2019-5-20
path = /nfsbackup
"/etc/rsyncd.conf" 31L, 732C written  

5.服务端创建目录并修改权限

[15:39 root@backup ~]# mkdir -p /nfsbackup
[15:40 root@backup ~]# chown rsync.rsync /nfsbackup/
[15:41 root@backup ~]# ll -d /nfsbackup/
drwxr-xr-x 2 rsync rsync 6 May 24 15:40 /nfsbackup/

6.在服务端本地测试推送一下

[15:41 root@backup ~]# rsync  -avz  /etc/hostname    rsync_backup@172.16.1.41::nfsbackup 
Password: 
sending incremental file list
hostname

sent 101 bytes  received 43 bytes  32.00 bytes/sec
total size is 7  speedup is 0.05

7.再去客户端推送一下

[15:41 root@nfs01 ~]# rsync  -avz  /etc/hostname    rsync_backup@172.16.1.41::nfsbackup 
Password: 
sending incremental file list

sent 50 bytes  received 20 bytes  20.00 bytes/sec
total size is 7  speedup is 0.10

※8.查看文件下的xml文件

/app/sersync/conf/confxml.xml

https://www.processon.com/view/link/5bf911c0e4b006dc83a24929

[15:53 root@nfs01 ~]#vim /app/sersync/conf/confxml.xml 
  1 <?xml version="1.0" encoding="ISO-8859-1"?>
  2 <head version="2.5">
  3     <host hostip="localhost" port="8008"></host>
  4     <debug start="false"/>		
  5     <fileSystem xfs="false"/>	文件系统\\修改为true
  6     <filter start="false">		\\过滤
  7         <exclude expression="(.*)\.svn"></exclude>      \\排除--exclude
  8         <exclude expression="(.*)\.gz"></exclude>       \\排除--exclude
  9         <exclude expression="^info/*"></exclude>        \\排除--exclude
 10         <exclude expression="^static/*"></exclude>      \\排除--exclude
 11         <delete start="true"/>
 12		</filter>
 13	    </inotify>				\\配置inotify的监控事件,实时监控目录变量
 14         <createFolder start="true"/>
 15         <createFile start="false"/>
 16         <closeWrite start="true"/>
 17         <moveFrom start="true"/>
 18         <moveTo start="true"/>
 19         <attrib start="false"/>			\\文件属性
 20         <modify start="false"/>			\\mtime修改时间
 21     </inotify>					\\sersync什么时候用rsync推送
 22 
 23     <sersync>
 24         	<localpath watch="/opt/tongbu">		        \\要监控哪个目录 发生变量
 25             <remote ip="127.0.0.1" name="tongbu1"/>     \\rsync服务端ip name=模块名
 26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
 27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 28         </localpath>
 29         <rsync>							\\rsync命令配置
 30             <commonParams params="-artuz"/>					\\rsync的参数
 31             <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>  \\认证哪个用户和密码文件
 32             <userDefinedPort start="false" port="874"/><!-- port=874 -->
 33             <timeout start="false" time="100"/><!-- timeout=100 -->
 34             <ssh start="false"/>
 35         </rsync>
 36			<failLog path="/var/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
					\\rsync错误日志指定的路径
 37         <crontab start="false" schedule="600"><!--600mins-->	\\定时任务
 38             <crontabfilter start="false">
 39                 <exclude expression="*.php"></exclude>
 40                 <exclude expression="info/*"></exclude>
 41             	</crontabfilter>
 42        	</crontab>
 43         <plugin start="false" name="command"/>
 44     </sersync>
 45 
 46     <plugin name="command">
 47         <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /opt/tongbu/mmm.sh suffix-->
 48         <filter start="false">
 49             <include expression="(.*)\.php"/>
 50             <include expression="(.*)\.sh"/>
 51         </filter>
 52     </plugin>
 53 
 54     <plugin name="socket">
 55         <localpath watch="/opt/tongbu">
 56             <deshost ip="192.168.138.20" port="8009"/>
 57         </localpath>
 58     </plugin>
 59     <plugin name="refreshCDN">
 60         <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
 61             <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
 62             <sendurl base="http://pic.xoyo.com/cms"/>
 63             <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
 64         </localpath>
 65     </plugin>
 66 </head>
 

记得先复制一份备份

[16:17 root@nfs01 ~]# cp  /app/sersync/conf/confxml.xml /app/sersync/conf/confxml.xml.bak

9.然后修改此配置文件,如下内容即可

[16:17 root@nfs01 ~]# vim /app/sersync/conf/confxml.xml 
 23     <sersync>
 24         <localpath watch="/upload">     \\
 25             <remote ip="172.16.1.41" name="nfsbackup"/>
 26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
 27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 28         </localpath>
 29         <rsync>
 30             <commonParams params="-az"/>
 31             <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
 32             <userDefinedPort start="false" port="874"/><!-- port=874 -->
 33             <timeout start="false" time="100"/><!-- timeout=100 -->
 34             <ssh start="false"/>
 35         </rsync>
 36         <failLog path="/var/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

10.然后用diff对比一下修改的内容

diff /app/sersync/conf/confxml.xml /app/sersync/conf/confxml.xml.bak 

11.接着在客户端配置如下的环境

[16:24 root@nfs01 ~]# echo '123456' >/etc/rsync.password 
[16:24 root@nfs01 ~]# chmod 600 /etc/rsync.password 
[16:24 root@nfs01 ~]# mkdir -p /upload
[16:25 root@nfs01 ~]# chown nfsnobody.nfsnobody /upload/
[16:25 root@nfs01 ~]# ll -d /upload/
drwxr-xr-x 2 nfsnobody nfsnobody 21 May 23 08:42 /upload/

※12.然后查看sersync配置参数

[16:25 root@nfs01 ~]# sersync -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参数,则默认执行同步程序
________________________________________________________________

13.然后测试一下

/nfs端

[16:50 root@nfs01 ~]# touch /upload/oldboy{01..10}.txt

14.接着推送一下:

sersync -rd -o /app/sersync/conf/confxml.xml
  • 我们要把这条命令放到 /etc/rc.d/rc.local 开机自启动里,不然重启后就失效了。

[16:52 root@nfs01 ~]# tail -1 /etc/rc.d/rc.local 
sersync -rd -o /app/sersync/conf/confxml.xml
[16:52 root@nfs01 ~]# sersync  -rd  -o  /app/sersync/conf/confxml.xml
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
option: -r 	rsync all the local files to the remote servers before the sersync work
option: -d 	run as a daemon
option: -o 	config xml name:  /app/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost	host port: 8008
WARNING XFS FILE SYSTEM WORK
daemon start,sersync run behind the console 
use rsync password-file :
user is	rsync_backup
passwordfile is 	/etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /upload && rsync -az -R --delete ./ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password >/dev/null 2>&1 
run the sersync: 
watch path is: /upload

15.在backup服务端查看:实时同步了

[16:51 root@backup ~]# ll /nfsbackup/
total 0
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy01.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy02.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy03.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy04.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy05.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy06.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy07.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy08.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy09.txt
-rw-r--r-- 1 rsync rsync 0 May 24 16:52 oldboy10.txt


二、接下来讲云服务器

以阿里云为例https://www.aliyun.com/

1.云服务器不能用25端口号的SMTP发送邮件

可以使用465端口,465端口只支持加密传输

STARTLS标准出炉,规定用587端口以STARTTLS方式提交邮件

587端口专门被设计用来提交邮件,传输可以加密也可以不加密。

查找端口号的命令
grep smtp /etc/services

2.云服务器的名称

负载均衡是SLB

内外网交换机都是VPC(虚拟局域网)

云web服务器是ECS

云数据库是RDS

云存储是NAS(类似nfs)

云同步服务器是ECS

云公网ip(弹性公网ip)

3.选择购买时要注意的名词含义:

地域   region :地区 北京 上海 杭州    不同地区的服务器内网不通
可用区  zone  :同一个地区的 不同机房  同1个地区的不同可用区 内网相通

4.在购买阿里云服务器比较坑的步骤

对外的端口一定要开放 安全组
873/873

安全组,顾名思义“安全”“组”,是阿里云为了提高服务器安全,从软件角度开发的一套效果与防火墙很相似的一套安全体系。另外,安全组还有个组的概念,一个安全组可以配置给多台服务器,这对以后服务器的增量管理是个非常占优势的地方。安全组的使用,首先需要理解的是公网,私网。购买云服务器时候,阿里云已经为我们选择了默认安全组,目前默认规则是:对公网开启22,3389,80端口,其他端口默认关闭。


周末作业:
通过阿里云 实现 nfs backup web01
实时同步

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值