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

第九周-day39-sersync实时同步.png

一、sersync实时同步

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

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

介绍

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

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

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


nfs01客户端client

backup服务端service

二、实时同步实践

  • \color{red}{测试之前确保机器的rsync服务都开启}

systemctl is-active rsyncd
active
systemctl is-enabled rsyncd
enabled

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下

/app/目录在企业中一般用来放代码文件

[15:24 root@nfs01 ~]#mkdir -p /app
[15:24 root@nfs01 ~]#mv /server/tools/sersync_installdir_64bit/sersync /app/

查看一下

[15:27 root@nfs01 ~]# tree /app/
/app/
└── 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@backup ~]#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 
....
  5     <fileSystem xfs="true"/>  
....
 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.在客户端创建文件测试一下

[16:50 root@nfs01 ~]# touch /upload/oldboy{01..10}.txt
[19:32 root@nfs01 ~]# ll /upload/
total 0
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy01.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy02.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy03.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy04.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy05.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy06.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy07.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy08.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy09.txt
-rw-r--r-- 1 root root 0 May 26 19:32 oldboy10.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
实时同步

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,386评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,142评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,704评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,702评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,716评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,573评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,314评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,230评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,680评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,873评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,991评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,706评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,329评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,910评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,038评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,158评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,941评论 2 355

推荐阅读更多精彩内容

  • 1、第八章 Samba服务器2、第八章 NFS服务器3、第十章 Linux下DNS服务器配站点,域名解析概念命令:...
    哈熝少主阅读 3,734评论 0 10
  • 《鸟哥Linux私房菜》《老男孩Linux运维》 NFS介绍 NFS维基百科:网络文件系统(英语:Network ...
    Zhang21阅读 4,227评论 0 13
  • 转载:https://www.cnblogs.com/liangml/p/5959571.html 一.为什么要用...
    SkTj阅读 1,491评论 0 1
  • 一、服务器配置: 安装软件: yum -y install nfs-utils rpcbind 更改配置文件: v...
    济夏阅读 2,864评论 0 51
  • 今天是什么日子 十连休的第七天 起床:6:30 就寝:11:30 天气:阴转晴 心情:超级舒适~出去遛弯寻春啦 叫...
    脾气最近渐长阅读 262评论 1 1