1. 程式人生 > 其它 >NFS服務搭建

NFS服務搭建

一、NFS簡介

NFS是Network File System 的縮寫以及網路檔案系統。NFS主要功能是通過區域網讓不同的主機系統之間可以共享檔案或者目錄。

NFS系統和window 網路共享,網路驅動器類似,只不過Windows用於區域網,NFS用於企業叢集架構中,如果是大型網站會,會用到更加複雜的分散式檔案系統FastDFS,glusterfs,HDFS,ceph

二、NFS 應用

1 使用者訪問NFS客戶端,將強求轉化為函式
2 NFS 通過TCP/IP連線服務端
3 NFS服務端接受請求,會先呼叫portmap 程序進行埠對映
4 RPc.nfsd 程序用於判斷NFS 客戶能否連結服務端
5 .Rpc.mount程序用於判斷客戶端對服務端的操作許可權 6 如果通過許可權驗證,可以對服務端進操作,修改或讀取

三、NFS實踐

1、服務端

1、安裝NFS和rpcbind
[root@nfs ~]# yum install nfs-utils rpcbind -y

2、建立掛載點
[root@nfs ~]# mkdir /web/nfs{1..9}
檢視建立的目錄:  ll /web/ 
3、配置掛載點
[root@nfs ~]# vim /etc/exports
格式:
[掛載點] [可以訪問的IP]([許可權])
/web/nfs1  172.16.1.0/20(rw,sync,all_squash)

4、關閉selinux和防火牆 [root@nfs ~]# setenforce 0 [root@nfs ~]# systemctl disable --now firewalld 5、啟動Nfs和rpcbind服務 [root@nfs ~]# systemctl start nfs-server [root@nfs ~]# systemctl start rpcbind 6、檢查服務端是否正常 [root@nfs ~]# showmount -e [服務端的地址,預設是本機地址] [root@nfs ~]# showmount -e Export list for nfs: /web/nfsv1 172.16
.1.0/20 [root@nfs ~]# showmount -e 172.16.1.31 Export list for 172.16.1.31: /web/nfsv1 172.16.1.0/20 [root@nfs ~]# cat /var/lib/nfs/etab 7、給掛載點授權 [root@nfs ~]# chown -R nfsnobody.nfsnobody /web 檢視授權是否成功: ll /web/ total 0 drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 10:20 nfs1 drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 10:20 nfs2 drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 10:20 nfs3 drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 10:20 nfs4 drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 10:20 nfs5 drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 10:20 nfs6 drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 10:20 nfs7 drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 10:20 nfs8 drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 10:20 nfs9

2、客戶端

 1、安裝NFS
[root@web01 opt]# yum install -y nfs-utils

2、建立目錄
[root@web01 opt]# mkdir /opt/nfs/
ll  
3、掛載NFS
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1  /opt/nfs/

4、測試NFS檔案同步功能
[root@web01 opt]# touch nfs/{1..9}.txt
[root@web01 opt]# ll nfs/ 
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 1.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 2.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 3.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 4.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 5.txt
-rw-r--r-- 1 nfsnobody nfsnobody 



0 Dec 30 11:08 6.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 7.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 8.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 9.txt
[root@nfs ~]# ll /web/nfs1
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 1.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 2.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 3.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 4.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 5.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 6.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 7.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 8.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 11:08 9.txt

 

四、NFS配置詳解

nfs共享引數
引數作用
rw 讀寫許可權(常用)
ro

制度許可權(不常用)

root-squash 當NFS客戶端以root管理員訪問時,對映為NFS伺服器的匿名使用者 (不常用)
no-root-squash 當NFS客戶端以root管理員訪問時,對映為NFS伺服器的root管理員 (不常用)
all-squash 無論NFS客戶端使用什麼賬戶訪問,均對映為NFS伺服器的匿名使用者 (常用)
no-all-squash 無論NFS客戶端使用什麼賬戶訪問,都不進行壓縮 (不常用)
sync 同時將資料寫入到記憶體與硬碟中,保證不丟失資料 (常用)
async 優先將資料儲存到記憶體,然後再寫入硬碟;這樣效率更高,但可能會丟失資料 (不常用)
anonuid 配置all_squash使用,指定NFS的使用者UID,必須存在系統 (常用)
anongid 配置all_squash使用,指定NFS的使用者GID,必須存在系統 (常用)

1、控制讀寫
rw、ro
檢視是否啟動成功了
`systemctl status nfs-server rpcbind
解除安裝掛載
`umount /opt/nfs/
檢視掛載詳情
df -h
刪除/opt/nfs/*目錄下的所有的內容
rm -rf /opt/nfs/*
2、控制檔案許可權
root_squash
no_root_squash
all_squash
no_all_squash

3、控制寫模式
sync
async

4、控制使用者
anonuid
anongid

統一使用者:

1、建立使用者
[root@nfs nfs1]# groupadd www -g 666
[root@nfs nfs1]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin 

2、修改掛載點許可權
[root@nfs nfs1]# chown -R www.www /web/

3、使用

五、搭建考試系統

搭建WEB服務

1、安裝web軟體
[root@web01 opt]# yum install httpd php php-devel -y

2、將程式碼放置於網站的根目錄
[root@web01 opt]# cd /var/www/html/

# 上傳程式碼
檢視: ll
解壓:
[root@nfs html]# unzip kaoshi.zip

3、授權
[root@web01 html]# chown -R www.www /var/www/html

4、關閉selinux和防火牆
[root@nfs ~]# setenforce 0
[root@nfs ~]# systemctl disable --now firewalld

5、修改web軟體的使用者
[root@web01 html]# vim /etc/httpd/conf/httpd.conf
User www
Group www

6、啟動web軟體
[root@web01 html]# systemctl start httpd

7、測試

    1、上傳  172.16.1.7
    尋找上傳資料
    image.baidu.com
    檢視錯誤;檢視日誌
    [root@web01 html]# tail -f /var/log/messgges
    檢視錯誤日誌
    [root@web01 html]# tail -f /var/log/httpd/
tail: error reading ‘/var/log/httpd/’: Is a directory
tail: /var/log/httpd/: cannot follow end of this type of file; giving up on this name
tail: no files remaining
[root@web01 html]# tail -f /var/log/httpd/error_log
[Thu Dec 30 16:42:29.769337 2021] [suexec:notice] [pid 2534] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::20c:29ff:fe8b:7a67. Set the 'ServerName' directive globally to suppress this message
[Thu Dec 30 16:42:29.816757 2021] [lbmethod_heartbeat:notice] [pid 2534] AH02282: No slotmem from mod_heartmonitor
[Thu Dec 30 16:42:29.828160 2021] [mpm_prefork:notice] [pid 2534] AH00163: Apache/2.4.6 (CentOS) PHP/5.4.16 configured -- resuming normal operations
[Thu Dec 30 16:42:29.828180 2021] [core:notice] [pid 2534] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'


[root@web01 html]# ll
total 80
-rw-r--r-- 1 www www 38772 Apr 27  2018 bg.jpg
-rw-r--r-- 1 www www  2633 May  4  2018 index.html
-rw-r--r-- 1 www www    52 May 10  2018 info.php
-rw-r--r-- 1 www www 26995 Dec 30 16:39 kaoshi.zip
drwxr-xr-x 2 www www    29 Dec 30 16:52 upload
-rw-r--r-- 1 www www  1192 Jan 10  2020 upload_file.php
[root@web01 html]# ll /ipload
ls: cannot access /ipload: No such file or directory
[root@web01 html]# ll /upload
ls: cannot access /upload: No such file or directory
[root@web01 html]# 
[root@web01 html]# ll upload
total 84
-rw-r--r-- 1 www www 83100 Dec 30 16:52 1_linux.jpg.jpg
    2、訪問
    http://172.16.1.7/upload/1_linux.jpg.jpg
    3、同時設定web2 web3 
1、安裝web軟體
[root@web01 opt]# yum install httpd php php-devel -y

2、將程式碼放置於網站的根目錄
[root@web01 opt]# cd /var/www/html/

# 上傳程式碼
[root@web01 html]# scp kaoshi.zip 172.16.1.8:/var/www/html/
[root@web03 html]# unzip kaoshi.zip
[root@web02 html]# unzip kaoshi.zip
3、授權
[root@web01 html]# chown -R www.www /var/www/html

4、關閉selinux和防火牆
[root@nfs ~]# setenforce 0
[root@nfs ~]# systemctl disable --now firewalld

5、修改web軟體的使用者
[root@web01 html]# vim /etc/httpd/conf/httpd.conf
在末行模式輸入apache 全部修改成www即可
User www
Group www

6、啟動web軟體
[root@web01 html]# systemctl start httpd

7、測試

配合NFS實現檔案共享

1、修改NFS配置檔案
[root@nfs nfs1]# vim /etc/exports
/web/upload  172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)

2、建立掛載點
[root@nfs nfs1]# mkdir /web/upload
[root@nfs nfs1]# chown www.www /web/upload

3、重啟NFS
[root@nfs nfs1]# systemctl restart nfs-server rpcbind

4、客戶端安裝NFS軟體
[root@web01 html]# yum install nfs-utils -y
[root@web02 html]# yum install nfs-utils -y
[root@web03 html]# yum install nfs-utils -y

5、掛載
[root@web01 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web02 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web03 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload

6、測試
用web2上傳,web3檢視