nfs 實時同步與單點故障解決


一、統一用戶

1.httpd
2.NFS掛載目錄

1.服務器創建統一用戶

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666

2.修改httpd用戶

[root@web01 ~]# vim /etc/httpd/conf/httpd.conf
User www
Group www

#重啟服務
[root@web01 ~]# systemctl restart httpd
[root@web01 ~]# ps -ef | grep httpd
root      31537      1  1 21:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       31538  31537  0 21:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       31539  31537  0 21:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       31540  31537  0 21:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       31541  31537  0 21:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       31542  31537  0 21:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root      31544  18601  0 21:57 pts/1    00:00:00 grep --color=auto httpd

3.修改NFS服務端權限

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

#重啟
[root@nfs /data]# systemctl restart nfs

1597388307766

4.NFS服務端重新授權目錄

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

5.web端重新掛載

[root@web01 /var/www/html]# umount /var/www/html/upload
[root@web01 /var/www/html]# mount -t nfs 172.16.1.31:/data ./upload

二、NFS總結

1.NFS存儲優點

1.簡單易用,部署方便
2.數據可查,服務穩定

2.NFS服務缺點

1.存在單點故障, 如果構建高可用維護麻煩web->nfs()->backup
2.NFS數據明文, 並不對數據做任何校驗。
3.客戶端掛載NFS服務沒有密碼驗證, 安全性一般(內網使用)

3.NFS應用建議

1.開機掛載
    如果希望NFS文件共享服務能一直有效,則需要將其寫入到fstab文件中
    #編輯fstab文件
    [root@nfs-client ~]# vim /etc/fstab
    172.16.1.31:/data /nfsdir nfs defaults 0 0
    #驗證fstab是否寫正確
    [root@nfs-client ~]# mount -a
    
2.生產場景應將靜態數據盡可能往前端推,減少后端存儲壓力
3.必須將存儲里的靜態資源通過CDN緩存,jpg\png\mp4\avi\css\js
4.如果沒有緩存或架構本身歷史遺留問題太大, 在多存儲也無用

1597390707752

三、Rsync+NFS解決單點故障

1.准備環境

主機 角色 IP
backup rsync服務端,NFS服務端 172.16.1.41
nfs rsync客戶端,NFS服務端 172.16.1.31
web01 rsync客戶端,NFS客戶端 172.16.1.7,10.0.0.7

1597392640005

2.web01搭建上傳作業平台

1)安裝httpd和php

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

2)上傳代碼

[root@web01 ~]# rz kaoshi.zip
[root@web01 ~]# yum unzip
[root@web01 ~]# unzip kaoshi.zip -d /var/www/html/ 

3)創建httpd用戶

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666

4)配置httpd

[root@web01 ~]# vim /etc/httpd/conf/httpd.conf
User www
Group www

#啟服務
[root@web01 ~]# systemctl start httpd

5)修改權限

[root@web01 ~]# chown -R www.www /var/www/html/

3.nfs服務器搭建nfs服務端

1)安裝NFS

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

2)配置NFS

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

3)查看配置生效

[root@backup ~]# cat /var/lib/nfs/etab 
/data 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash)

4)創建用戶

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666

5)創建目錄

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

4.掛載web01數據目錄

[root@web01 ~]# showmount -e 172.16.1.31

[root@web01 ~]# mount -t nfs 172.16.1.31:/data /var/www/html/upload

5.backup搭建rsync服務端

1)安裝

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]
comment = "文件備份目錄"
path = /backup

[data]
comment = "數據備份目錄"
path = /data

3)重啟rsyncd

[root@backup ~]# systemctl restart rsyncd

6.NFS數據實時備份至backup服務器

0)安裝inotify

[root@nfs ~]# yum -y install inotify-tools

1)編寫腳本實時備份data目錄

[root@nfs /data]# vim /scripts/backup_data.sh
#!/bin/bash
dir=/data
export RSYNC_PASSWORD=123456
/usr/bin/inotifywait  -mrq  --format '%w %f' -e create,delete,attrib,close_write  $dir | while read line;do
        cd  $dir  && rsync -az -R  --delete  .  rsync_backup@172.16.1.41::data >/dev/null 2>&1
done  &

2)啟動腳本

[root@nfs /data]# sh /scripts/backup_data.sh

3)測試

1.訪問交作業頁面,上傳圖片
2.查看web服務器
[root@web01 ~]# ll /var/www/html/upload
3.查看nfs服務器data目錄
[root@nfs ~]# ll /data/
4.查看backup服務器data目錄
[root@backup ~]# ll /data/

7.backup服務器搭建NFS服務端

1)安裝NFS

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

2)配置nfs

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

3)創建用戶

[root@backup ~]# groupadd www -g 666
[root@backup ~]# useradd www -u 666 -g 666

4)啟動服務

[root@backup ~]# systemctl start nfs

8.測試

1)nfs出現故障

[root@nfs ~]# systemctl stop nfs

2)切換掛載機器

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

四、實時同步

1.什么實時同步

實時同步是一種只要當前目錄發生變化則會觸發一個事件,事件觸發后會將變化的目錄同步至遠程服務器

2.為什么實時同步

保證數據的連續性, 減少人力維護成本,解決nfs單點故障

3.實時同步工具選擇

sersync+RSYNC(√)、inotify+rsync

Inotify是一個通知接口,用來監控文件系統的各種變化,如果文件存取,刪除,移動。可以非常方便地實現文件異動告警,增量備份,並針對目錄或文件的變化及時作出響應。rsync+inotify可以實觸發式實時同步增量備份

sersync是國人基於rsync+inotify-tools開發的工具,不僅保留了優點同時還強化了實時監控,文件過濾,簡化配置等功能,幫助用戶提高運行效率,節省時間和網絡資源。

4.安裝sersync(NFS服務器)

1)安裝rsync和inotify

[root@nfs01 ~]# yum install rsync inotify-tools -y

2)下載sersync包

[root@nfs01 ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz

3)解壓安裝包

[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz

4)移動並改名

[root@nfs ~]# mv GNU-Linux-x86 /usr/local/sersync

5)配置

[root@nfs ~]# cat /usr/local/sersync/confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
	#主機IP
    <host hostip="localhost" port="8008"></host>
    #調試模式
    <debug start="false"/>
    <fileSystem xfs="false"/>
    #文件過濾
    <filter start="false">
	<exclude expression="(.*)\.svn"></exclude>
	<exclude expression="(.*)\.gz"></exclude>
	<exclude expression="^info/*"></exclude>
	<exclude expression="^static/*"></exclude>
    </filter>
    #inotify監控配置
    <inotify>
    #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">
		#遠程IP及模塊名字
	    <remote ip="172.16.1.41" name="data"/>
	</localpath>
	<rsync>
		#rsync同步時的參數
	    <commonParams params="-artuz"/>
	    #開啟認證
	    <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
	    #如果rsync服務不是873端口,需要開啟
	    <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-->
	#定時任務,默認600分鍾進行一次全備
	<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 ~]#

6)創建密碼文件

[root@nfs ~]# echo 123456 > /etc/rsync.password
[root@nfs ~]# chmod 600 /etc/rsync.password

7)啟動

#查看參數
[root@nfs ~]# /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 ~]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/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: -d 	run as a daemon
option: -r 	rsync all the local files to the remote servers before the sersync work
option: -o 	config xml name:  /usr/local/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost	host port: 8008
will ignore the inotify createFile event 
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 /data && rsync -artuz -R --delete ./ rsync_backup@172.16.1.41::data --password-file=/etc/rsync.password >/dev/null 2>&1 
run the sersync: 
watch path is: /data

作業:

1.恢復快照
2.搭建交作業系統
3.配置掛載數據目錄
4.nfs的數據目錄實時備份到backup服務器(要求使用sersync)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM