部署NFS服務(wù)端
1.yum -y install nfs-utils #安裝NFS軟件
------------------------------------------------
2? 創(chuàng)建nfs運(yùn)行的虛擬用戶(hù)
服務(wù)端創(chuàng)建用戶(hù),默認(rèn)是nfsnobody诀诊。為配合rsync備份數(shù)據(jù)湘今,改成www用戶(hù)。
groupadd -g 666 www
-----------------------------
useradd -s /sbin/nologin -M -u 666 -g 666 www
---------------------------------------------
[root@nfs /data]# id? www
uid=666(www) gid=666(www) groups=666(www)
3.修改nfs服務(wù)端配置文件
vi? /etc/exports? #配置nfs服務(wù)
---------------------------
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)? #配置內(nèi)容
--------------------------------------------------------------------
cat /etc/exports? ? #檢測(cè)
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) #檢測(cè)結(jié)果
-----------------------------------------------------------------------
4.創(chuàng)建數(shù)據(jù)目錄并授權(quán)
[root@nfs ~]# mkdir -p /data
---------------------------------------
[root@nfs ~]# chown -R www.www /data/
---------------------------------------
5.啟動(dòng)rpcbind和nfs服務(wù)
systemctl start rpcbind? ? #打開(kāi)rpc
--------------------------------------
systemctl status rpcbind #檢測(cè)
---------------------------------------------
systemctl start nfs-server.service #打開(kāi)NFS
--------------------------------------------------
systemctl status nfs-server.service #檢測(cè)
---------------------------------------------------
設(shè)置開(kāi)機(jī)自啟:
systemctl enable rpcbind nfs-server #開(kāi)機(jī)自啟
-----------------------------------------------------------
systemctl restart rpcbind nfs-server #重啟rpc
-----------------------------------------------------------------
6.服務(wù)端檢查命令
[root@nfs ~]# showmount -e 172.16.1.31
----------------------------------------------
Export list for 172.16.1.31:
/data 172.16.1.0/24
7.查看端口
#rpc主端口:111
#nfs主端口:2049(隨機(jī)的,可變)
[root@nfs /server/scripts]# rpcinfo -p localhost
? program vers proto? port? service
? ? 100000? ? 4? tcp? ? 111? portmapper
? ? 100000? ? 3? tcp? ? 111? portmapper
? ? 100000? ? 2? tcp? ? 111? portmapper
? ? 100000? ? 4? udp? ? 111? portmapper
? ? 100000? ? 3? udp? ? 111? portmapper
? ? 100000? ? 2? udp? ? 111? portmapper
? ? 100005? ? 1? udp? 20048? mountd
? ? 100005? ? 1? tcp? 20048? mountd
? ? 100005? ? 2? udp? 20048? mountd
? ? 100005? ? 2? tcp? 20048? mountd
? ? 100005? ? 3? udp? 20048? mountd
? ? 100005? ? 3? tcp? 20048? mountd
? ? 100003? ? 3? tcp? 2049? nfs
? ? 100003? ? 4? tcp? 2049? nfs
? ? 100227? ? 3? tcp? 2049? nfs_acl
? ? 100003? ? 3? udp? 2049? nfs
? ? 100003? ? 4? udp? 2049? nfs
? ? 100227? ? 3? udp? 2049? nfs_acl
? ? 100021? ? 1? udp? 42622? nlockmgr
? ? 100021? ? 3? udp? 42622? nlockmgr
? ? 100021? ? 4? udp? 42622? nlockmgr
? ? 100021? ? 1? tcp? 44645? nlockmgr
? ? 100021? ? 3? tcp? 44645? nlockmgr
? ? 100021? ? 4? tcp? 44645? nlockmgr
部署NFS客戶(hù)端在【172.16.1.41】配置
1. yum -y install nfs-utils? #安裝NFS軟件
---------------------------------
2.啟動(dòng)rpcbind服務(wù)
#客戶(hù)端只需要啟動(dòng)rpcbind拇囊,不需要啟動(dòng)nfs
systemctl start rpcbind? ? #啟動(dòng)rpc
systemctl enable rpcbind #開(kāi)機(jī)自啟
systemctl status rpcbind #檢測(cè)
-------------------------------------------------
3 創(chuàng)建www虛擬用戶(hù)
創(chuàng)建相同uid gid的用戶(hù),如果客戶(hù)端沒(méi)有創(chuàng)建這個(gè)用戶(hù) ,查看文件的時(shí)候顯示的是666寥假。
groupadd -g 666 www
useradd -s /sbin/nologin -M -u 666 -g 666 www
--------------------------------------------------
4 查詢(xún)NFS遠(yuǎn)程共享信息
使用showmount命令查看nfs共享信息有鹿,其輸出格式為“共享的目錄名稱(chēng) 允許使用客戶(hù)端地址”吊奢。
[root@backup ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
--------------------------------------
5 客戶(hù)端掛載并查看
在 NFS 客戶(hù)端創(chuàng)建一個(gè)掛載目錄, 使用 mount 命令并結(jié)合-t 參數(shù), 指定要掛載的文件系統(tǒng)的類(lèi)型, 并在命令后面寫(xiě)上服務(wù)器的 IP 地址, 以及服務(wù)器上的共享目錄, 最后需要寫(xiě)上要掛載到本地系統(tǒng)(客戶(hù)端)的目錄
mkdir -p /data
mount -t nfs 172.16.1.31:/data /data
#查看是否掛載成功
[root@backup ~]# df -h
Filesystem? ? ? ? ? ? ? ? ? ? Size? Used Avail Use% Mounted on
/dev/mapper/centos_mode-root? 37G? 1.8G? 36G? 5% /
devtmpfs? ? ? ? ? ? ? ? ? ? ? 476M? ? 0? 476M? 0% /dev
tmpfs? ? ? ? ? ? ? ? ? ? ? ? 488M? ? 0? 488M? 0% /dev/shm
tmpfs? ? ? ? ? ? ? ? ? ? ? ? 488M? 7.6M? 480M? 2% /run
tmpfs? ? ? ? ? ? ? ? ? ? ? ? 488M? ? 0? 488M? 0% /sys/fs/cgroup
/dev/sda1? ? ? ? ? ? ? ? ? ? 1014M? 130M? 885M? 13% /boot
tmpfs? ? ? ? ? ? ? ? ? ? ? ? ? 98M? ? 0? 98M? 0% /run/user/0
172.16.1.31:/data? ? ? ? ? ? ? 37G? 2.8G? 35G? 8% /data
------------------------------------------------------------------------
6 寫(xiě)入開(kāi)機(jī)自動(dòng)掛載
[root@backup ~]# tail -1? /etc/fstab
172.16.1.31:/data ? /data? ? ? nfs? ? defaults ? ? 0 0
----------------------------------------------------------------------
mount -a? #重新加載/etc/fstab,不用重啟直接掛載咧叭。
-----------------------------------------------------
7 卸載
umount /data/
umount -rl /data #強(qiáng)制卸載
#卸載的時(shí)候如果提示"umount.nfs: /nfsdir: device is busy"先切換到其他目錄再卸載
nfs 服務(wù)程序的配置文件為/etc/exports蚀乔,需要嚴(yán)格按照共享目錄的路徑 允許訪問(wèn)的 NFS 客戶(hù)端(共享權(quán)限參數(shù))格式書(shū)寫(xiě),定義要共享的目錄與相應(yīng)的權(quán)限菲茬,具體書(shū)寫(xiě)方式如下圖所示
![image-20200330175531035](C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20200330175531035.png)
擴(kuò)展
NFS天坑
1.如果在客戶(hù)端掛載172.16.1.31:/data后吉挣,服務(wù)端重啟網(wǎng)卡,會(huì)導(dǎo)致客戶(hù)端查看data目錄卡頓婉弹。
解決:強(qiáng)制卸載后重新掛載
umount-rl/data
mount-tnfs172.16.1.31:/data /data
?
2.如果nfs服務(wù)掛了睬魂,訪問(wèn)客戶(hù)端data目錄會(huì)卡住。
解決:1.服務(wù)端重啟nfs即可 2.客戶(hù)端強(qiáng)制卸載umunt-rl/data
?
3.如果客戶(hù)端設(shè)置了開(kāi)機(jī)自動(dòng)掛載马胧,服務(wù)端nfs掛掉了汉买,重啟客戶(hù)端會(huì)卡住很久,會(huì)一直在等掛載佩脊。
解決:
#如果是物理服務(wù)器
? ? 1蛙粘、觀察讀秒時(shí)間,如果比較短威彰,寧愿等1分42秒(看讀秒)出牧,也不進(jìn)單用戶(hù)模式。進(jìn)單用戶(hù)模式會(huì)花費(fèi)至少五分鐘以上歇盼。
#如果是云服務(wù)器
? ? 1.不愿意等就進(jìn)單用戶(hù)模式舔痕,注釋掉開(kāi)機(jī)自動(dòng)掛載的參數(shù)。
?
#進(jìn)入單用戶(hù)模式的方法:
?? 開(kāi)機(jī)啟動(dòng)時(shí)按e,然后光標(biāo)移動(dòng)到倒數(shù)第二行尾部添加rd.break
ctrl+x
mount-oremount,rw /sysroot/
chroot/sysroot/? ?
NFS共享多個(gè)目錄
1.配置文件修改
[root@nfs ~]# cat /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data2 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
2.重啟nfs服務(wù)
systemctl restart nfs
3.客戶(hù)端掛載測(cè)試
? ? showmount -e 172.16.1.31
? ? Export list for 172.16.1.31:
? ? /data? 172.16.1.0/24
? ? /data2 172.16.1.0/24
NFS固定端口號(hào)
1.固定NFS隨機(jī)端口
? ? [root@nfs01 ~]# vim /etc/sysconfig/nfs
? ? RQUOTAD_PORT=4001
? ? LOCKD_TCPPORT=4002
? ? LOCKD_UDPPORT=4002
? ? MOUNTD_PORT=4003
? ? STATD_PORT=4004
? ? --------------------------------------------------------
? ? [root@nfs01 ~]# tail -5 /etc/sysconfig/nfs #尾部追加
2.重啟rpcbind和nfs
systemctl restart rpcbind.service nfs-server.service
3.查看端口
[root@nfs /data]# rpcinfo -p
? program vers proto? port? service
? ? 100000? ? 4? tcp? ? 111? portmapper
? ? 100000? ? 3? tcp? ? 111? portmapper
? ? 100000? ? 2? tcp? ? 111? portmapper
? ? 100000? ? 4? udp? ? 111? portmapper
? ? 100000? ? 3? udp? ? 111? portmapper
? ? 100000? ? 2? udp? ? 111? portmapper
? ? 100005? ? 1? udp? 4003? mountd
? ? 100005? ? 1? tcp? 4003? mountd
? ? 100005? ? 2? udp? 4003? mountd
? ? 100005? ? 2? tcp? 4003? mountd
? ? 100005? ? 3? udp? 4003? mountd
? ? 100005? ? 3? tcp? 4003? mountd
? ? 100003? ? 3? tcp? 2049? nfs
? ? 100003? ? 4? tcp? 2049? nfs
? ? 100227? ? 3? tcp? 2049? nfs_acl
? ? 100003? ? 3? udp? 2049? nfs
? ? 100003? ? 4? udp? 2049? nfs
? ? 100227? ? 3? udp? 2049? nfs_acl
? ? 100021? ? 1? udp? 4002? nlockmgr
? ? 100021? ? 3? udp? 4002? nlockmgr
? ? 100021? ? 4? udp? 4002? nlockmgr
? ? 100021? ? 1? tcp? 4002? nlockmgr
? ? 100021? ? 3? tcp? 4002? nlockmgr
? ? 100021? ? 4? tcp? 4002? nlockmgr
4.firewalld開(kāi)放端口(C7)
firewall-cmd --add-service=nfs --permanent
? ? firewall-cmd --add-service=mountd --permanent
? ? firewall-cmd --add-service=rpc-bind --permanent
? ? firewall-cmd --reload
5.iptables開(kāi)放端口(C6)
vim /etc/sysconfig/iptables
? ? ? ? -A INPUT -p tcp -m tcp --dport 111 -j ACCEPT
? ? ? ? -A INPUT -p udp -m udp --dport 111 -j ACCEPT
? ? ? ? -A INPUT -p tcp -m tcp --dport 2049 -j ACCEPT
? ? ? ? -A INPUT -p udp -m udp --dport 2049 -j ACCEPT
? ? ? ? -A INPUT -p tcp -m tcp --dport 4001:4004 -j ACCEPT
? ? ? ? -A INPUT -p udp -m udp --dport 4001:4004 -j ACCEPT
? ? iptables-save #保存配置
iptables -nL
NFS小結(jié)
#NFS 存儲(chǔ)優(yōu)點(diǎn)
? ? 1.NFS 文件系統(tǒng)簡(jiǎn)單易用伯复、方便部署慨代、數(shù)據(jù)可靠、服務(wù)穩(wěn)定啸如、滿(mǎn)足中小企業(yè)需求侍匙。
? ? 2.NFS 文件系統(tǒng)內(nèi)存放的數(shù)據(jù)都在文件系統(tǒng)之上,所有數(shù)據(jù)都是能看得見(jiàn)
#NFS 存儲(chǔ)局限
? ? 1.存在單點(diǎn)故障, 如果構(gòu)建高可用維護(hù)麻煩 web->nfs()->backup
? ? 2.NFS 數(shù)據(jù)明文, 并不對(duì)數(shù)據(jù)做任何校驗(yàn)叮雳。
? ? 3.客戶(hù)端掛載 NFS 服務(wù)沒(méi)有密碼驗(yàn)證, 安全性一般(內(nèi)網(wǎng)使用)
#NFS 應(yīng)用建議
? ? 1.生產(chǎn)場(chǎng)景應(yīng)將靜態(tài)數(shù)據(jù)盡可能往前端推, 減少后端存儲(chǔ)壓力
? ? 2.必須將存儲(chǔ)里的靜態(tài)資源通過(guò) CDN 緩存 jpg\png\mp4\avi\css\js
? ? 3.如果沒(méi)有緩存或架構(gòu)本身歷史遺留問(wèn)題太大, 在多存儲(chǔ)也無(wú)用