1访忿、什么是實(shí)時(shí)同步
監(jiān)控一個(gè)目錄的變化传于,當(dāng)觸發(fā)事件時(shí)(創(chuàng)建,刪除醉顽,修改等)就執(zhí)行動(dòng)作沼溜,這個(gè)動(dòng)作可以是rsync同步,也可以是其他
2游添、為什么要實(shí)時(shí)同步
1. 能解決nfs單點(diǎn)故障的問題
2. 能夠讓本地快速切換至云端
3系草、實(shí)時(shí)同步的原理
借助一個(gè)通知接口,inntift.inotif監(jiān)控本地主機(jī)的事件(創(chuàng)建唆涝,刪除找都,修改等)
4、實(shí)時(shí)同步的場(chǎng)景
1.能解決nfs單點(diǎn)故障的問題
- 能夠快速的從本地切換至云端
5廊酣、實(shí)時(shí)同步選擇的工具
1.inotify + rsync 實(shí)現(xiàn)能耻,寫腳本來實(shí)現(xiàn)監(jiān)控,放在后臺(tái) screen
2.sersync 實(shí)時(shí)同步
https://github.com/wsgzao/sersync
6亡驰、實(shí)時(shí)同步案例演示
規(guī)劃
服務(wù)器 | ip地址 | 安裝服務(wù) |
---|---|---|
web | 10.0.0.7—172.16.1.7 | http晓猛,php |
nfs | 10.0.0.31—172.16.1.31 | nfs-server inotify rsync sersync |
backup | 10.0.0.41—172.16.1.41 | rsync-server nfs-server |
1)、實(shí)現(xiàn)web長(zhǎng)傳視頻凡辱,自動(dòng)存儲(chǔ)在NFS
NFS服務(wù)端
1.安裝nfs
[root@nfs ~]# yum install nfs-utils -y
2.配置
[root@nfs ~]# cat /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
3.根據(jù)配置初始化環(huán)境
[root@nfs ~]# groupadd -g 666 www
[root@nfs ~]# useradd -u666 -g666 www
[root@nfs ~]# mkdir /data
[root@nfs ~]# chown -R www.www /data/
4.重新啟動(dòng)nfs服務(wù)
[root@nfs ~]# systemctl restart nfs-server
[root@nfs ~]# systemctl enable nfs-server
WEB服務(wù)端操作: 10.0.0.7 172.16.1.7
1.安裝httpd php
[root@web01 ~]# yum install httpd php -y
2.配置httpd php
3.啟動(dòng)httpd
[root@web01 ~]# systemctl start httpd
4.模擬測(cè)試
[root@web01 ~]# echo "Oldboy Edu.com" > /var/www/html/index.html (可以不測(cè)試)
WEB進(jìn)行掛載,上傳代碼:
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /var/www/html/
[root@web01 ~]# cd /var/www/html
[root@web01 ~]# rz kaoshi.zip
[root@web01 ~]# unzip kaoshi.zip
用戶通過前端的web頁面上傳視頻
如果上傳出現(xiàn)錯(cuò)誤:請(qǐng)檢查日志
[root@web01 html]# tail -f /var/log/httpd/error_log
2)當(dāng)NFS的目錄發(fā)生變化,則觸發(fā)同步? (實(shí)時(shí)同步)
1.怎么監(jiān)控/data? inotify
2.同步給誰rsync ---> backup服務(wù)器 [backup模塊] [data模塊]
rsync服務(wù) 10.0.0.41 172.16.1.41
1.安裝rsync
[root@backup ~]# yum install rsync -y
2.配置rsync
[root@backup ~]# cat /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#############################
[backup]
comment = welcome to oldboyedu backup!
path = /backup
[data]
path = /data
3.根據(jù)rsync配置初始化環(huán)境
[root@backup ~]# useradd -M -s /sbin/nologin rsync
[root@backup ~]# echo "rsync_backup:123456" > /etc/rsync.passwd
[root@backup ~]# chmod 600 /etc/rsync.passwd
[root@backup ~]# mkdir /backup /data -p
[root@backup ~]# chown -R rsync.rsync /backup/ /data/
4.啟動(dòng)rsync
[root@backup ~]# systemctl restart rsyncd
[root@backup ~]# systemctl enable rsyncd
3)nfs服務(wù)配置 10.0.0.31 172.16.1.31
1.安裝rsync inotify
[root@nfs ~]# yum install rsync inotify-tools -y
2.安裝sersync ( rsync inotify )
[root@nfs ~]# wget https://github.com/wsgzao/sersync/raw/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
[root@nfs ~]# mv /usr/local/GNU-Linux-x86/ /usr/local/sersync
3.配置相關(guān)文件(具體配置按照3.3)
[root@nfs/usr/local/rersync]# vim confxml.xml
4.查看文件類型
[root@nfs ~]# file /usr/local/sersync/confxml.xml
[root@nfs ~]# echo "123456" > /etc/rsync.pass
[root@nfs ~]# chmod 600 /etc/rsync.pass
3.啟動(dòng)sersync
[root@nfs ~]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
4.建議: 手動(dòng)執(zhí)行該命令測(cè)試是否ok,這樣方便排查問題
[root@nfs ~]# cd /data && rsync -avz -R --delete ./ --timeout=100 rsync_backup@172.16.1.41::data --password-file=/etc/rsync.pass
3.3 usr/local/rersync/confxml.xml 根據(jù)相關(guān)需求進(jìn)行配置
[root@nfs-31/usr/local/rersync]# vim confxml.xml
5 <fileSystem xfs="true"/> <-- 文件系統(tǒng) -->
6 <filter start="false"> <-- 排除不想同步的文件-->
7 <exclude expression="(.*)\.svn"></exclude>
8 <exclude expression="(.*)\.gz"></exclude>
9 <exclude expression="^info/*"></exclude>
10 <exclude expression="^static/*"></exclude>
11 </filter>
12 <inotify> <-- 監(jiān)控的事件類型 -->
13 <delete start="true"/>
14 <createFolder start="true"/>
15 <createFile start="true"/>
16 <closeWrite start="true"/>
17 <moveFrom start="true"/>
18 <moveTo start="true"/>
19 <attrib start="false"/>
20 <modify start="false"/>
21 </inotify>
23 <sersync>
24 <localpath watch="/data"> <-- 監(jiān)控的目錄 -->
25 <remote ip="172.16.1.41" name="data"/> <-- backup的IP以及模塊 -->
28 </localpath>
29 <rsync> <-- rsync的選項(xiàng) -->
30 <commonParams params="-az"/>
31 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pass"/>
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="true" time="100"/><!-- timeout=100 -->
34 <ssh start="false"/>
35 </rsync>
<-- 每60分鐘執(zhí)行一次同步-->
36 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><--def
ault every 60mins execute once-->
7.模擬NFS故障, web可以快速的切換到backup的NFS服務(wù)上?
1.backup的權(quán)限是否和NFS的權(quán)限一致?
2.backup上是否配置NFS服務(wù)? 是否共享了/data目錄?
3.模擬NFS故障,實(shí)現(xiàn)切換?
解決方案
1.backup的權(quán)限是否和NFS的權(quán)限一致? 10.0.0.41 172.16.1.41
1修改如下內(nèi)容:
[root@backup ~]# vim /etc/rsyncd.conf
uid = www
gid = www
.....
創(chuàng)建用戶
[root@backup ~]# groupadd -g 666 www
[root@backup ~]# useradd -g666 -u666 www
修改權(quán)限
[root@backup ~]# chown -R www.www /data/ /backup/
重啟服務(wù)
[root@backup ~]# systemctl restart rsyncd
2.backup服務(wù)器上是否配置NFS服務(wù)? 是否共享了/data目錄?
安裝NFS
[root@backup ~]# yum install nfs-utils -y
配置NFS
[root@backup ~]# cat /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
[root@backup ~]# chown -R www.www /data/
啟動(dòng)NFS
[root@backup ~]# systemctl enable nfs-server
[root@backup ~]# systemctl start nfs-server
3.模擬NFS故障,實(shí)現(xiàn)切換?
[root@web01 ~]# umount -lf /var/www/html && mount -t nfs 172.16.1.41:/data /var/www/html
基于sersync海量文件實(shí)時(shí)同步