新聞中心
項目環(huán)境:
三臺主機(centos7):

nfs-server主:172.16.1.20
nfs-server從:172.16.1.30
client(客戶機):172.16.1.40
項目操作:
1, 首先搭建nfs服務器
主從nfs-server都需搭建,相同的操作。
[root@nfs-master ~]# yum -y install nfs-utils #安裝nfs服務
[root@nfs-master ~]# yum -y install rpcbind #安裝遠程傳輸控制協(xié)議[root@nfs-master ~]# vim /etc/exports #編寫nfs文件
/nfs-share 172.16.1.*(rw,sync,no_root_squash)參數解釋:
172.16.1.*:表示允許該網段,也可以自定義ip地址
rw:可讀可寫
sync:同步數據到磁盤
no_root_squash:加上這個選項后,root用戶就會對共享的目錄擁有至高的權限控制,就像是對本機的目錄操作一樣。
[root@nfs-master ~]# mkdir /nfs-share #創(chuàng)建共享目錄
[root@nfs-master ~]# systemctl start rpcbind #先啟動該服務
[root@nfs-master ~]# systemctl start nfs2, 在從nfs-server上搭建rsync:
//安裝rsync:
[root@nfs-slave ~]# yum -y install rsync//修改rsync配置文件:
[root@nfs-slave ~]# vim /etc/rsyncd.conf修改內容如下:

//為授權賬戶創(chuàng)建數據文件:
[root@nfs-slave ~]# vim /etc/rsyncd_users.db
#文件名可以自定義,但是必須與上面配置文件中數據文件名相同
注意:用戶名得和rsync配置文件中同一個用戶。
//授予權限
[root@nfs-slave ~]# chmod 600 /etc/rsyncd_users.db//啟動rsync服務:
[root@nfs-slave ~]# rsync --daemon
3, 在主nfs-server上安裝inotify工具:
下載并上傳inotify-tools-3.14.tar.gz安裝包
[root@nfs-master ~]# tar zxf inotify-tools-3.14.tar.gz
[root@nfs-master ~]# cd inotify-tools-3.14/
[root@nfs-master inotify-tools-3.14]# ./configure && make && make install##編寫觸發(fā)式同步腳本:[root@nfs-master ~]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /nfs-share"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /nfs-share sunqiuming@172.16.1.30::rsync"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
$RSYNC_CMD
done
注意:該腳本用于監(jiān)控本地的共享目錄/nfs-share,只要監(jiān)控到對該目錄有任何操作,就會同步數據到從nfs-server服務器上的/nfs-share目錄下(需要權限)。
//創(chuàng)建腳本中的密碼文件(為了在同步的過程中不用在輸入密碼)
[root@nfs-master ~]# echo "123456" > /etc/server.pass
##文件名自定義,只要確保與上述腳本中相同[root@nfs-master ~]# chmod 600 /etc/server.pass #授予權限
//對從nfs-server服務上的共享目錄授予權限
[root@nfs-slave ~]# chmod 777 /nfs-share/
[root@nfs-slave ~]# ls -ld /nfs-share/
drwxrwxrwx 2 root root 6 Nov 14 23:14 /nfs-share///將腳本文件加入開機自啟:[root@nfs-master ~]# echo '/root/inotify.sh' >> /etc/rc.local
//執(zhí)行該觸發(fā)腳本:[root@nfs-master ~]# sh inotify.sh & #后臺運行
4, client客戶機進行掛載:
[root@client ~]# mkdir /test #創(chuàng)建測試掛載目錄
[root@client ~]# mount -t nfs -o soft,timeo=5 172.16.1.20:/nfs-share /test
#注意使用軟掛載,默認是硬掛載,使用軟掛載,當服務端宕機,不會一直阻塞##在測試目錄下編寫測試文件:
[root@client test]# echo "hello" > index.html
[root@client test]# echo "ha ha ha " > index.php//在主nfs-server上進行查看:
[root@nfs-master ~]# cd /nfs-share/
[root@nfs-master nfs-share]# cat index.html
hello
[root@nfs-master nfs-share]# cat index.php
ha ha ha//再次到從nfs-server上進行查看(是否已經觸發(fā)腳本,并同步數據)
[root@nfs-slave ~]# cd /nfs-share/
[root@nfs-slave nfs-share]# ls
nfs-share
[root@nfs-slave nfs-share]# cd nfs-share/
[root@nfs-slave nfs-share]# cat index.html
hello
[root@nfs-slave nfs-share]# cat index.php
ha ha ha數據同步成功。。。。。。。。。。
5, 模擬nfs-server故障:
模擬故障前首先在客戶機上編寫檢測腳本:
[root@client ~]# vim nfs.sh
#!/bin/bash
while true;
do
ping 172.16.1.20 -c 4 &> /dev/null
if [ $? -ne 0 ];
then
umount -l /test && mount -t nfs -o soft,timeo=5 172.16.1.30:/nfs-share /test
fi
sleep 1
done注意:這個腳本會每秒檢測一次,只要當主nfs-server故障后,就會重新掛載到從nfs-server上的共享目錄下。
//執(zhí)行該腳本:[root@client ~]# sh nfs.sh & #讓其后臺運行
//接下來將主nfs-server主機進行關機或者停止服務(模擬宕機)。
//最后驗證客戶機是否檢測到,并且將目錄掛載到從nfs-server服務器上。
[root@client ~]# cd /test
[root@client test]# ls
nfs-share
[root@client test]# ls nfs-share/
123.txt index.html index.php---------------------------------可以看到當主nfs-server故障后,會執(zhí)行腳本重新掛載到備用nfs-server上,以實現數據不會丟失,并且不會中斷正在運行的服務---------------------
———————— 本文至此結束,感謝閱讀 ————————
另外有需要云服務器可以了解下創(chuàng)新互聯cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網站名稱:部署nfs高可用rsync+inotify-創(chuàng)新互聯
本文路徑:http://www.dlmjj.cn/article/hspsp.html


咨詢
建站咨詢
