Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

NFS 檔案伺服器建置詳解:一台伺服器配多台 Web 主機

小型文件服務器的常見架構是「一台檔案伺服器 + 多台 Web 伺服器」,用 NFS 把同一份目錄共享給所有 Web 節點讀寫。這樣資料只存在一處,不必同步,也少了單點資料不一致的困擾。

環境:192.168.56.151(rhel151)為檔案伺服器,192.168.56.152(rhel152)為 Web 伺服器。

0. 確認套件

[root@rhel151 ~]# rpm -qa | grep nfs
system-config-nfs-1.3.23-2.el5
nfs-utils-1.0.9-66.el5
nfs-utils-lib-1.0.8-7.9.el5

1. 建立共享目錄與設定

[root@rhel151 ~]# mkdir -p /opt/nfsshare
[root@rhel151 ~]# echo "/opt/nfsshare 192.168.56.*(rw,no_root_squash)" >/etc/exports
[root@rhel151 ~]# cat /etc/exports
/opt/nfsshare 192.168.56.*(rw,no_root_squash)

這行的意思是:把 /opt/nfsshare 共享給 192.168.56 網段的主機,權限為讀寫,並保留 root 權限(no_root_squash)。其他參數可查 NFS 手冊,這兩個已足以應付多數情境。

2. 啟動服務並設為開機自啟

[root@rhel151 ~]# service portmap restart
[root@rhel151 ~]# service nfs restart
[root@rhel151 ~]# chkconfig portmap on
[root@rhel151 ~]# chkconfig nfs on

3. 客戶端設定

[root@rhel152 ~]# service portmap restart
[root@rhel152 ~]# chkconfig portmap on
[root@rhel152 ~]# mkdir -p /opt/nfsshare

本機掛載點不必與伺服器端同名,這只是習慣。

4. 掛載與一個常見陷阱

[root@rhel152 ~]# mount -t nfs 192.168.56.151:/opt/nfsshare /opt/nfsshare
mount: 192.168.56.151:/opt/nfsshare failed, reason given by server: Permission denied

明明 exports 設好了卻被拒絕,原因在於伺服器端 /etc/hosts 做了主機名與 IP 的對應,而 NFS 在比對來源時會先反解成主機名:

[root@rhel151 ~]# cat /etc/hosts
127.0.0.1   rhel151 localhost.localdomain localhost
192.168.56.151 rhel151
#192.168.56.152 rhel152     <-- 把客戶端的對應註解掉

把客戶端那行註解掉後回到客戶端重試:

[root@rhel152 ~]# mount -t nfs rhel151:/opt/nfsshare /opt/nfsshare

5. 驗收

[root@rhel152 ~]# mount
rhel151:/opt/nfsshare on /opt/nfsshare type nfs (rw,addr=192.168.56.151)

[root@rhel152 ~]# df -h
文件系統                    容量  已用  可用 已用% 掛載點
rhel151:/opt/nfsshare        14G  7.6G  5.5G   59% /opt/nfsshare

從客戶端寫入測試:

[root@rhel152 ~]# cd /opt/nfsshare/
[root@rhel152 nfsshare]# echo 'asdfasdf' > 111
[root@rhel151 ~]# cat /opt/nfsshare/111
asdfasdf

兩端一致,設定完成。之後要再加 rhel153、rhel154…,重複客戶端那幾步即可。

這種「伺服器集中存放、客戶端掛載」的作法,最大的好處是不必在每台 Web 主機準備大容量儲存,也不必同步資料,同時避免了各節點資料不一致的風險。

Copyright © Linux教程網 All Rights Reserved