1、什麼是NFS
NFS(Network File System)網絡文件系統
它的主要功能是通過網絡(一般是局域網)讓不同主機系統之間共享文件或目錄
NFS客戶端(應用服務器,例如web)可以掛載(mount)的方式將NFS服務器端共享的數據目錄掛載到NFS客戶端本地系統中(某一個掛載點下)
從客戶端本地來看,NFS服務器端共享的目錄就好像是客戶端自己的磁盤分區或目錄一樣,而實際上卻是遠端的NFS服務器的目錄
所有的Web服務器會向 NFS服務器進行數據交互
應用場景:
NFS適用於中小型企業:NFS服務器,而且默認只能有一臺NFS服務器
大公司或大門戶(高併發):Moosefs(MFS)、GlusterFS、FastDFS等(分佈式文件系統 )
NFS在傳輸數據時使用的端口會隨機選擇。那麼就需要RPC
NFS軟件列表:
nfs-utils #NFS服務的主程序
rpcbind #RPC的主程序
安裝:yum install nfs-utils rpcbind -y (服務端必須安裝,客戶端強烈建議安裝)
檢查:rpm -aq nfs-utils rpcbind
1.1 NFS工作原理流程圖:
NFS服務端,首先要先啓動RPC服務,在啓動NFS服務,NFS服務會自動向RPC服務註冊啓動的端口,客戶端需要通過自身的RPC服務,來向NFS服務端的RPC獲取NFS的端口號,然後客戶端在通過得到的端口號,單獨向NFS發起TCP建立。
file:///Users/xiongminghao/Library/Mobile%20Documents/iCloud~com~coderforart~iOS~MWeb/Documents/mweb_documents_library/docs/media/15413998107562/15414008422850.jpg
1.2 NFS共享目錄
NFS共享的目錄:必須是實際存在的目錄,要用絕對路徑,注意共享目錄的本地權限,如果需要讀寫共享,要讓本地目錄可以被NFS客戶端的用戶(nfsnobody)讀寫
NFS客戶端地址:爲NFS服務端授權的可以訪問共享目錄的NFS客戶端地址,可爲單獨的IP地址或主機名、域名等,也可以使整個網段地址,還可以用“*”來匹配所有客戶端服務器,沒有對賬號密碼,幾乎都是用IP地址,這些所謂的客戶端一般來說是前端的業務服務器,
權限參數:對授權的NFS客戶端的訪問權限
NFS服務端默認配置文件路徑/etc/exports,並且內容默認爲空
[root@web01 ~]# ls -ld /etc/exports
-rw-r--r--. 1 root root 0 Jun 7 2013 /etc/exports
[root@web01 ~]# cat /etc/exports
exports配置文件的規範(兩種寫法):
NFS共享的目錄 NFS客戶端地址1(參1,參2...)客戶端地址2(參1,參2...)
NFS共享的目錄 NFS客戶端地址1(參1,參2...)
2、NFS搭建
查看系統版本
[root@web01 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@web01 ~]# uname -r
3.10.0-862.el7.x86_64
[root@web01 ~]# uname -m
x86_64
檢查並安裝nfs和rpc服務
[root@nfs /]#yum install nfs-utils rpcbind -y
[root@nfs /]# rpm -aq nfs-utils rpcbind
rpcbind-0.2.0-44.el7.x86_64
nfs-utils-1.3.0-0.54.el7.x86_64
2.1 服務端配置
啓動RPC
[root@nfs /]# systemctl start rpcbind
[root@nfs /]# systemctl status rpcbind #查看rpcbind是否正常運行
● rpcbind.service - RPC bind service
Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2018-11-05 15:03:34 CST; 1min 0s ago
Process: 52393 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS)
Main PID: 52394 (rpcbind)
CGroup: /system.slice/rpcbind.service
└─52394 /sbin/rpcbind -w
Nov 05 15:03:34 nfs systemd[1]: Starting RPC bind service...
Nov 05 15:03:34 nfs systemd[1]: Started RPC bind service.
[root@nfs /]# netstat -lntup |grep rpc
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 52394/rpcbind
tcp6 0 0 :::111 :::* LISTEN 52394/rpcbind
udp 0 0 0.0.0.0:841 0.0.0.0:* 52394/rpcbind
udp 0 0 0.0.0.0:111 0.0.0.0:* 52394/rpcbind
udp6 0 0 :::841 :::* 52394/rpcbind
udp6 0 0 :::111 :::* 52394/rpcbind
[root@nfs /]# lsof -i :111 #111爲RPC程序端口號
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 52394 rpc 6u IPv4 100989 0t0 UDP *:sunrpc
rpcbind 52394 rpc 8u IPv4 100991 0t0 TCP *:sunrpc (LISTEN)
rpcbind 52394 rpc 9u IPv6 100992 0t0 UDP *:sunrpc
rpcbind 52394 rpc 11u IPv6 100994 0t0 TCP *:sunrpc (LISTEN)
啓動NFS
[root@nfs /]# systemctl start nfs
[root@nfs /]# systemctl status nfs
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
Active: active (exited) since Mon 2018-11-05 15:08:02 CST; 3s ago
Process: 54371 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
Process: 54366 ExecStartPre=/bin/sh -c /bin/kill -HUP `cat /run/gssproxy.pid` (code=exited, status=0/SUCCESS)
Process: 54365 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Main PID: 54371 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/nfs-server.service
Nov 05 15:08:02 nfs systemd[1]: Starting NFS server and services...
Nov 05 15:08:02 nfs systemd[1]: Started NFS server and services.
再次查看RPC端口
#當NFS啓動後,NFS向rpc註冊端口
[root@nfs /]# netstat -lntup |grep rpc
tcp 0 0 0.0.0.0:37998 0.0.0.0:* LISTEN 54333/rpc.statd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 52394/rpcbind
tcp 0 0 0.0.0.0:20048 0.0.0.0:* LISTEN 54363/rpc.mountd
tcp6 0 0 :::33985 :::* LISTEN 54333/rpc.statd
tcp6 0 0 :::111 :::* LISTEN 52394/rpcbind
tcp6 0 0 :::20048 :::* LISTEN 54363/rpc.mountd
udp 0 0 0.0.0.0:841 0.0.0.0:* 52394/rpcbind
udp 0 0 0.0.0.0:20048 0.0.0.0:* 54363/rpc.mountd
udp 0 0 0.0.0.0:111 0.0.0.0:* 52394/rpcbind
udp 0 0 0.0.0.0:45174 0.0.0.0:* 54333/rpc.statd
udp 0 0 127.0.0.1:703 0.0.0.0:* 54333/rpc.statd
udp6 0 0 :::45876 :::* 54333/rpc.statd
udp6 0 0 :::841 :::* 52394/rpcbind
udp6 0 0 :::20048 :::* 54363/rpc.mountd
udp6 0 0 :::111 :::* 52394/rpcbind
提示:
查看nfs服務向rpc註冊的端口信息
rpcinfo -p localhost
NFS常見進程講解
[root@nfs /]# ps -ef|egrep "rpc|nfs"
rpc 52394 1 0 15:03 ? 00:00:00 /sbin/rpcbind -w
rpcuser 54333 1 0 15:08 ? 00:00:00 /usr/sbin/rpc.statd
root 54341 2 0 15:08 ? 00:00:00 [rpciod]
root 54363 1 0 15:08 ? 00:00:00 /usr/sbin/rpc.mountd
root 54364 1 0 15:08 ? 00:00:00 /usr/sbin/rpc.idmapd
root 54373 2 0 15:08 ? 00:00:00 [nfsd4_callbacks]
root 54379 2 0 15:08 ? 00:00:00 [nfsd]
root 54380 2 0 15:08 ? 00:00:00 [nfsd]
root 54381 2 0 15:08 ? 00:00:00 [nfsd]
root 54382 2 0 15:08 ? 00:00:00 [nfsd]
root 54383 2 0 15:08 ? 00:00:00 [nfsd]
root 54384 2 0 15:08 ? 00:00:00 [nfsd]
root 54385 2 0 15:08 ? 00:00:00 [nfsd]
root 54386 2 0 15:08 ? 00:00:00 [nfsd]
nfsd(rpc.nfsd) #主進程,主要是管理客戶端能否登入服務端,登入者ID判別。
mountd(rpc.mountd) #管理NFS文件系統,登入者的權限管理
rpc.lockd(非必要) #用來鎖定文件,用於客戶端同時寫入
rpc.statd(非必要) #檢查文件一致性
rpc.idmapd #名字映射後臺進程
將rpcbind和nfs加入開機自啓動
systemctl enable rpcbind.service
systemctl enable nfs-server.service
配置共享/data目錄(編輯/etc/exports配置文件)
[root@nfs ~]# cat >>/etc/exports<<EOF
> #shared /data by jason for bingbing at 2018-08-25
> /data 172.16.1.0/24(rw,sync)
> EOF
[root@nfs ~]# cat /etc/exports
#shared /data by oldboy for bingbing at 2018-08-25
/data 172.16.1.0/24(rw,sync)
圖解上述操作的意義:
提示:
必須要在NFS和RPC啓動之後修改/etx/export文件
創建共享目錄(客戶端訪問)
#創建/data目錄
[root@nfs /]# mkdir /data -p
[root@nfs /]# ls -ld /data/
drwxr-xr-x 2 root root 6 Nov 5 14:31 /data/
將共享目錄加入到nfs的虛擬用戶和用戶組
[root@nfs /]# chown -R nfsnobody.nfsnobody /data/
#nfsnobody是nfs的虛擬用戶
[root@nfs /]# id nfsnobody
uid=65534(nfsnobody) gid=65534(nfsnobody) groups=65534(nfsnobody)
上述操作完畢後,平滑重啓nfs
[root@nfs /]# systemctl reload nfs
使用nfs自帶的showmount,測試nfs是否共享成功
#測試NFS目錄是否共享成功
#在nfs端本地測試
[root@nfs /]# showmount -e localhost
Export list for localhost:
/data 172.16.1.0/24
#可以-e指定nfs端地址
[root@nfs /]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
nfs端自己掛載自己的/data目錄(可忽略)
[root@nfs /]# mount -t nfs 172.16.1.31:/data /mnt/
mount.nfs: /mnt is busy or already mounted
[root@nfs /]# df -h |grep mnt
172.16.1.31:/data 22G 1.7G 21G 8% /mnt #掛摘成功
[root@nfs /]# umount /mnt/ #測試完成後,記得卸載
提示:
測試nfs共享目錄,使用showmount命令即可
umount /mnt -lf 可強制卸載
2.2 客戶端配置
安裝nfs和rpc(rpc必須安裝,nfs需要使用到自帶的一些工具,所以兩者都裝)
[root@web01 ~]# yum isntall nfs-utils rpcbind -y
[root@web01 ~]# rpm -qa nfs-utils rpcbind
nfs-utils-1.3.0-0.54.el7.x86_64
rpcbind-0.2.0-44.el7.x86_64
啓動rpc,並加入開機自啓動
[root@web01 ~]# systemctl start rpcbind #啓動rpc
[root@web01 ~]# systemctl status rpcbind #查看rpc狀態
● rpcbind.service - RPC bind service
Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2018-11-05 15:39:55 CST; 4s ago
Process: 17432 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS)
Main PID: 17433 (rpcbind)
CGroup: /system.slice/rpcbind.service
└─17433 /sbin/rpcbind -w
Nov 05 15:39:55 web01 systemd[1]: Starting RPC bind service...
Nov 05 15:39:55 web01 systemd[1]: Started RPC bind service.
[root@web01 ~]# systemctl enable rpcbind.service #開機自啓動
測試掛載
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24 #共享目錄沒問題
[root@web01 ~]# telnet 172.16.1.31 111
Trying 172.16.1.31...
Connected to 172.16.1.31. #連接到對端rpc也沒問題
Escape character is '^]'.
將客戶端的/mnt目錄掛載到nfs服務端
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /mnt/
[root@web01 ~]# df -h |grep mnt
172.16.1.31:/data 22G 1.7G 21G 8% /mnt
#掛載完成
在掛載目錄/mnt下創建文件,然後在nfs服務端查看
#客戶端創建文件
[root@web01 ~]# cd /mnt/
[root@web01 mnt]# touch test.txt
[root@web01 mnt]# ll
-rw-r--r-- 1 nfsnobody nfsnobody 0 Nov 5 16:05 test.txt
#服務端查看
[root@nfs /]# cd /data/
[root@nfs data]# ll
-rw-r--r-- 1 nfsnobody nfsnobody 0 Nov 5 16:05 test.txt
注:當客戶端訪問/nmt的時候,相當於是在訪問172.16.1.31這個NFS服務端的/data目錄
實現開機自動掛載
[root@nfs /]# echo "mount -t nfs 172.16.1.31:/data /mnt/" >>/etc/rc.local
[root@nfs /]# tail -1 /etc/rc.local
mount -t nfs 172.16.1.31:/data /mnt/
提示:
也可以放入/etc/fatab,需要注意一些情況
NFS網絡文件系統最好不要放到fstab裏實現開機掛載,
但如果在開機自啓動服務裏設置並啓動了netfs服務,放入fastb裏也是可
以開機掛載的