serrsync:
1.什么是實時同步
監(jiān)控一個目錄的變化,當該目錄觸發(fā)事件(創(chuàng)建/刪除/修改)就執(zhí)行動作,這個動作可以是rsync同步,也可以是其他
2.為什么要實時同步
- 能解決nfs單點故障的問題
- 能夠讓本地快速切換至云端
3.實時同步的原理
借助一個通知接口,inotify. inotify監(jiān)控本地主機的事件(創(chuàng)建/刪除/修改),當本地主機觸發(fā)事件后,就執(zhí)行動作,這個動作可以是rsync
4.實時同步的場景
能解決nfs單點故障的問題
-
能夠讓本地快
速切換至云端
5.實時同步工具選擇
- inotify+rsync實現(xiàn),只不過需要通過腳本來實現(xiàn)監(jiān)控,并且放在后臺screen
- 寫腳本
- 同步
- sersync實現(xiàn)同步
- lsync
6.實時同步案例演示
規(guī)劃:
服務器名稱 | 外網(wǎng)ip | 內網(wǎng)ip | 服務 |
---|---|---|---|
web | 10.0.0.7 | 172.16.1.7 | httpd/php |
nfs | 10.0.0.31 | 172.16.1.31 | nfs inotify sync sersync |
backup | 10.0.0.41 | 172.16.1.41 | rsync nfs |
nfs服務端
-
- 安裝nfs
[root@nfs ~]# yum install nfs-utils -y
- 配置
[root@nfs ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
- 初始化環(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/
- 重啟nfs服務
[root@nfs ~]# systemctl restart nfs-server
[root@nfs ~]# systemctl enable nfs-server
web服務端操作
-
- 安裝
[root@web01 ~]# yum install httpd php -y
- 配置httpd php
- 啟動httpd
[root@web01 ~]# systemctl start httpd
- 模擬測試
[root@web01 ~]# echo "Oldboy Edu.com" > /var/www/html/index.html (可以不測試)
-
WEB進行掛載,上傳代碼:
[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
用戶通過前端的wen頁面上上傳視頻
如果上傳出現(xiàn)錯誤:請檢查日志
[root@web01 html]# tail -f /var/log/httpd/error_log
同步到backup服務器的backup模塊
-
- 安裝
[root@backup ~]# yum install rsync -y
- 配置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
- 根據(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/
- 啟動rsync
[root@backup ~]# systemctl restart rsyncd
[root@backup ~]# systemctl enable rsyncd
nfs服務配置
-
- 安裝rsync inotify
[root@nfs ~]# yum install rsync inotify-tools -y
- 安裝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 [root@nfs01 sersync]# vim 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="false"/> #文件系統(tǒng) <filter start="false"> #排除不想同步的文件 <exclude expression="(.*)\.svn"></exclude> <exclude expression="(.*)\.gz"></exclude> <exclude expression="^info/*"></exclude> <exclude expression="^static/*"></exclude> </filter> <inotify> #監(jiān)控的事件類型 <delete start="true"/> <createFolder start="true"/> <createFile start="true"/> <closeWrite start="true"/> <moveFrom start="true"/> <moveTo start="true"/> <attrib start="false"/> <modify start="true"/> </inotify> <sersync> <localpath watch="/data"> #監(jiān)控的目錄 <remote ip="172.16.1.41" name="data"/> #backup的ip以及模塊 <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> #rsync的選項 <commonParams params="-avz"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pass"/> <userDefinedPort start="false" port="874"/><!-- port=874 --> <timeout start="false" time="100"/><!-- timeout=100 --> <ssh start="false"/> </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"> <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix--> <filter start="false"> <include expression="(.*)\.php"/> <include expression="(.*)\.sh"/> </filter> </plugin> <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>
[root@nfs ~]# file /usr/local/sersync/confxml.xml
[root@nfs ~]# echo "123456" > /etc/rsync.pass
[root@nfs ~]# chmod 600 /etc/rsync.pass
3. 啟動sersync
```bash
[root@nfs ~]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
- 建議:手動執(zhí)行該命令測試是否OK,這樣方便排查問題
[root@nfs ~]# cd /data && rsync -avz -R --delete ./ --timeout=100 rsync_backup@172.16.1.41::data --password-file=/etc/rsync.pass
模擬NFS故障,當web可以快速切換到backup的NFS服務上
1.backup的權限是否和NFS權限一致
2.backup上是否配置NFS服務,是否共享了/data
3.模擬NFS故障,實現(xiàn)切換
-
backup的權限是否和NFS的權限一致 10.0.0.41 172.16.1.41
修改內容如下:
[root@backup ~]# vim /etc/rsyncd.conf
uid = www
gid = www
.....
創(chuàng)建用戶
[root@backup ~]# groupadd -g 666 www
[root@backup ~]# useradd -g666 -u666 www
修改權限
[root@backup ~]# chown -R www.www /data/ /backup/
重啟服務
[root@backup ~]# systemctl restart rsyncd
- backup服務器上是否配置NFS服務?是否共享了/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/
啟動NFS
[root@backup ~]# systemctl enable nfs-server
[root@backup ~]# systemctl start nfs-server
- 模擬NFS故障,實現(xiàn)切換
[root@web01 ~]# umount -lf /var/www/html && mount -t nfs 172.16.1.41:/data /var/www/html