linux NFS共享存儲(chǔ)
一、NFS的簡介
1.1.1 什么NFS
NFS 是(Network Flie System)網(wǎng)絡(luò)文件系統(tǒng)的縮寫,通過網(wǎng)絡(luò)存儲(chǔ)和組織文件的一種方法或機(jī)制。
1.1.2 企業(yè)為什么用NFS共享存儲(chǔ)
在企業(yè)中前端所有的應(yīng)用服務(wù)器接收到用戶上傳的圖片、文件、視頻,都會(huì)統(tǒng)一存放到后端的存儲(chǔ)服務(wù)器上,方便前端應(yīng)用服務(wù)器的統(tǒng)一存取。
1.1.3 共享存儲(chǔ)的種類
- 單點(diǎn)存儲(chǔ):NFS (中小型企業(yè)使用),阿里云服務(wù)的NAS服務(wù),OSS對象服務(wù)。
- 分布式存儲(chǔ):FastDFS、Ceph、GlusterFS、Mfs(大型企業(yè)會(huì)用)。
- NFS的工作原理
在c7系統(tǒng)中rpcbind和NFS是并行啟動(dòng)的所以和c6不同,c6要想啟動(dòng)rpcbind然后在啟動(dòng)NFS。我用的是c7,所以NFS工作的原理就,rpcbind和NFS同時(shí)啟動(dòng)之后NFS會(huì)將自己的所有服務(wù)端口注冊到rpcbind上,如果客戶端訪問NFS會(huì)先請求rpcbind,rpcbind會(huì)把NFS的端口返回給客戶端,然后客戶端就會(huì)連接NFS的端口,NFS驗(yàn)證端口正確之后就和連接客戶端。
二、NFS共享存儲(chǔ)環(huán)境搭建
2.1.1 NFS服務(wù)端安裝
- 安裝服務(wù)端
[root@nfs01 ~]# yum install nfs-utils rpcbind -y
[root@nfs01 ~]# rpm -qa nfs-utils rpcbind
rpcbind-0.2.0-49.el7.x86_64
nfs-utils-1.3.0-0.66.el7.x86_64
- 啟動(dòng)rpcbind和NFS
[root@nfs01 ~]# systemctl enable rpcbind.service
[root@nfs01 ~]# systemctl enable nfs.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@nfs01 ~]# systemctl start nfs.service
- 查看NFS在rpcbind上注冊的端口服務(wù)
[root@nfs01 ~]# rpcinfo -p 127.0.0.1
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
100024 1 udp 7194 status
100024 1 tcp 20528 status
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 56231 nlockmgr
100021 3 udp 56231 nlockmgr
100021 4 udp 56231 nlockmgr
100021 1 tcp 14003 nlockmgr
100021 3 tcp 14003 nlockmgr
100021 4 tcp 14003 nlockmgr
- 查看NFS和rpcbind的端口
[root@nfs01 ~]# netstat -luntp |grep -E "rpc|nfs"
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1772/rpcbind
tcp 0 0 0.0.0.0:20048 0.0.0.0:* LISTEN 1870/rpc.mountd
tcp 0 0 0.0.0.0:20528 0.0.0.0:* LISTEN 1848/rpc.statd
tcp6 0 0 :::39368 :::* LISTEN 1848/rpc.statd
tcp6 0 0 :::111 :::* LISTEN 1772/rpcbind
tcp6 0 0 :::20048 :::* LISTEN 1870/rpc.mountd
udp 0 0 0.0.0.0:7194 0.0.0.0:* 1848/rpc.statd
udp 0 0 0.0.0.0:111 0.0.0.0:* 1772/rpcbind
udp 0 0 0.0.0.0:20048 0.0.0.0:* 1870/rpc.mountd
udp 0 0 0.0.0.0:675 0.0.0.0:* 1772/rpcbind
udp 0 0 127.0.0.1:755 0.0.0.0:* 1848/rpc.statd
udp6 0 0 :::111 :::* 1772/rpcbind
udp6 0 0 :::48616 :::* 1848/rpc.statd
udp6 0 0 :::20048 :::* 1870/rpc.mountd
udp6 0 0 :::675 :::* 1772/rpcbind
- 如何配置NFS共享存儲(chǔ)
[root@nfs01 ~]# ll /etc/exports
-rw-r--r--. 1 root root 0 Jun 7 2013 /etc/exports
查看如何配置NFS
[root@nfs01 ~]# man exports
EXAMPLE
# sample /etc/exports file
/ master(rw) trusty(rw,no_root_squash)
/projects proj*.local.domain(rw)
/usr *.local.domain(ro) @trusted(rw)
/home/joe pc001(rw,all_squash,anonuid=150,anongid=100)
/pub *(ro,insecure,all_squash)
/srv/www -sync,rw server @trusted @external(ro)
/foo 2001:db8:9:e54::/64(rw) 192.0.2.0/24(rw)
/build buildhost[0-9].local.domain(rw)
NFS共享存儲(chǔ)的配置方法
第一列:是共享的目錄
第二例:允許訪問的主機(jī)(IP 域名 主機(jī)名)
第三列:權(quán)限 (rw讀寫 ro只讀)可選參數(shù)配置NFS共享存儲(chǔ)
[root@nfs01 ~]# cat /etc/exports
#this public file
#/data 172.16.1.0/24(rw,sync) 10.0.0.0/24(ro,sync)
/data 172.16.1.0/24(rw,sync)
8.創(chuàng)建共享目錄并授權(quán)
[root@nfs01 ~]# mkdir /data
[root@nfs01 ~]# id nfsnobody
uid=65534(nfsnobody) gid=65534(nfsnobody) groups=65534(nfsnobody)
[root@nfs01 ~]# chown -R nfsnobody:nfsnobody /data
[root@nfs01 ~]# ll -d /data
drwxr-xr-x 2 nfsnobody nfsnobody 6 Apr 29 04:23 /data
- 重啟NFS服務(wù)
[root@nfs01 ~]# systemctl restart nfs.service
[root@nfs01 ~]# systemctl reload nfs
[root@nfs01 ~]# exportfs -r
#上述三個(gè)命令是等價(jià)的,用其中一個(gè)即可。
- 檢查是否成功
[root@nfs01 ~]# showmount -e
Export list for nfs01:
/data 172.16.1.0/24
- 本地掛載測試
[root@nfs01 ~]# mount -t nfs 172.16.1.31:/data /mnt
mount.nfs: access denied by server while mounting 172.16.1.31:/data
#出現(xiàn)權(quán)限被拒絕的報(bào)錯(cuò):是配置文件里ip沒有加掩碼,加上掩碼即可解決
[root@nfs01 ~]# mount -t nfs 172.16.1.31:/data /mnt
[root@nfs01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 979M 0 979M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.5M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/mapper/centos_node-root 19G 2.1G 17G 12% /
/dev/sda1 497M 156M 342M 32% /boot
tmpfs 199M 0 199M 0% /run/user/0
172.16.1.31:/data 19G 2.1G 17G 12% /mnt
- 創(chuàng)建文件測試
[root@nfs01 ~]# cd /mnt/
[root@nfs01 /mnt]# ls -l
total 0
[root@nfs01 /mnt]# touch cs.txt
[root@nfs01 /mnt]# ls -l
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Apr 30 22:49 cs.txt
[root@nfs01 /mnt]# ll /data/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Apr 30 22:49 cs.txt
2.1.2 客戶端配置
- 客戶端安裝
[root@web01 ~]# yum install nfs-utils rpcbind -y
#原則上客戶端只安裝rpcbind就可以的,但是nfs-utils安裝包里有個(gè)showmount命令我們要使用所以安裝上但是不要啟動(dòng)就可以了。
- 啟動(dòng)rpcbind
[root@web01 ~]# systemctl start rpcbind
[root@web01 ~]# systemctl enable rpcbind
[root@web01 ~]# systemctl status rpcbind
● rpcbind.service - RPC bind service
Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2020-04-30 22:57:31 CST; 24s ago
Main PID: 1883 (rpcbind)
CGroup: /system.slice/rpcbind.service
└─1883 /sbin/rpcbind -w
Apr 30 22:57:31 web01 systemd[1]: Starting RPC bind service...
Apr 30 22:57:31 web01 systemd[1]: Started RPC bind service.
- 端口檢查
[root@web01 ~]# ss -luntp|grep rpc
udp UNCONN 0 0 *:111 *:* users:(("rpcbind",pid=1883,fd=6))
udp UNCONN 0 0 *:786 *:* users:(("rpcbind",pid=1883,fd=7))
udp UNCONN 0 0 [::]:111 [::]:* users:(("rpcbind",pid=1883,fd=9))
udp UNCONN 0 0 [::]:786 [::]:* users:(("rpcbind",pid=1883,fd=10))
tcp LISTEN 0 128 *:111 *:* users:(("rpcbind",pid=1883,fd=8))
tcp LISTEN 0 128 [::]:111 [::]:* users:(("rpcbind",pid=1883,fd=11))
- 查看并掛載NFS共享存儲(chǔ)
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /mnt
[root@web01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 979M 0 979M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.5M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/mapper/centos_node-root 19G 2.1G 17G 11% /
/dev/sda1 497M 156M 342M 32% /boot
tmpfs 199M 0 199M 0% /run/user/0
172.16.1.31:/data 19G 2.1G 17G 12% /mnt
- 創(chuàng)建文件進(jìn)行測試
[root@web01 ~]# touch /mnt/web01.txt
[root@web01 ~]# ll /mnt/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Apr 30 22:49 cs.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Apr 30 23:20 web01.txt
- 設(shè)置開機(jī)自啟動(dòng)
[root@web01 ~]# echo '172.16.1.31:/data /mnt nfs defaults 0 0' >>/etc/fstab
[root@web01 ~]# tail -1 /etc/fstab
172.16.1.31:/data /mnt nfs defaults 0 0
7.因?yàn)樵谇懊鎯?yōu)化的時(shí)候把開機(jī)自動(dòng)掛載的服務(wù)關(guān)了,所以沒有掛載上,
[root@web01 ~]# systemctl start remote-fs.target
[root@web01 ~]# systemctl enable remote-fs.target
Created symlink from /etc/systemd/system/multi-user.target.wants/remote-fs.target to /usr/lib/systemd/system/remote-fs.target.
[root@web01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 979M 0 979M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.5M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/mapper/centos_node-root 19G 2.1G 17G 11% /
/dev/sda1 497M 156M 342M 32% /boot
172.16.1.31:/data 19G 2.1G 17G 11% /data
tmpfs 199M 0 199M 0% /run/user/0
掛載擴(kuò)展
172.16.1.31:/data /data nfs defaults,_netdev 0 0
#這里的_netdev是防止nfs服務(wù)器掛掉,客戶端不能啟動(dòng)的參數(shù)。
- autofs 服務(wù)
這也是一個(gè)NFS的自動(dòng)掛載工具,但是這個(gè)工具有很大的缺點(diǎn),就是我往NFS共享存儲(chǔ)里面放東西的時(shí)候,它才自動(dòng)掛載NFS,平時(shí)都是卸載狀態(tài),如果并發(fā)很大的話,這樣做就會(huì)降低效率。https://blog.csdn.net/weixin_42179528/article/details/90379307?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1
2.1.3 NFS exports 重要配置參數(shù)說明
- rw允許在這個(gè)NFS卷上讀取和寫入請求。
- async 異步寫到遠(yuǎn)程緩沖區(qū)參數(shù)
- sync 數(shù)據(jù)寫到磁盤
- root_squash 如果訪問NFS server共享目錄的root用戶,則它的權(quán)限將被壓縮成匿名用戶,同時(shí)它的UID和GID通常會(huì)變成nfsnobody的身份。
- no_root_squash 訪問NFSserver共享目錄的用戶如果是root,它對該共享目錄具有root權(quán)限
- all_squash 不過什么用戶訪問NFS server 共享目錄的用戶是什么身份,則它的權(quán)限將被壓縮成匿名用戶同時(shí)它的UID和GID通常會(huì)變成nfsnobody的身。
- anonuid 指定匿名用戶的UID
- anongid 指定匿名用戶的GID
- 這個(gè)文件中有NFS服務(wù)端的掛載參數(shù)
2.1.4 NFS服務(wù)端參數(shù)實(shí)戰(zhàn)
服務(wù)端參數(shù)查看
[root@nfs01 ~]# cat /var/lib/nfs/etab
/data 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,no_all_squash)
- NFS默認(rèn)用戶改變
創(chuàng)建WWW用戶并指定UID和GID(在客戶端也要?jiǎng)?chuàng)建相同的用戶UID和GID一樣)
[root@nfs01 ~]# useradd -u 888 -M -s /sbin/nologin www
[root@nfs01 ~]# groupmod -g 888 www
[root@nfs01 ~]# id www
uid=888(www) gid=888(www) groups=888(www)
- 添加參數(shù)
[root@nfs01 ~]# tail -1 /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=888,anougid=888)
- 把data共享目錄的用戶換成www
[root@nfs01 ~]# chown -R www.www /data
- 重啟NFS服務(wù)
[root@nfs01 ~]# systemctl reload nfs
- 本地掛載成功
[root@nfs01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 979M 0 979M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.5M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/mapper/centos_node-root 19G 2.1G 17G 11% /
/dev/sda1 497M 156M 342M 32% /boot
tmpfs 199M 0 199M 0% /run/user/0
172.16.1.31:/data 19G 2.1G 17G 11% /mnt
- 客戶頓掛載 web01
[root@web01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 979M 0 979M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.5M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/mapper/centos_node-root 19G 2.1G 17G 11% /
/dev/sda1 497M 156M 342M 32% /boot
172.16.1.31:/data 19G 2.1G 17G 11% /data
tmpfs 199M 0 199M 0% /run/user/0
- 客戶端掛載 web02
[root@web02 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 979M 0 979M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.5M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/mapper/centos_node-root 19G 2.1G 17G 11% /
/dev/sda1 497M 156M 342M 32% /boot
172.16.1.31:/data 19G 2.1G 17G 11% /data
tmpfs 199M 0 199M 0% /run/user/0
- 創(chuàng)建文件測試
[root@web02 ~]# touch /data/web02.txt
[root@web01 ~]# touch /data/web01.txt
[root@web01 ~]# ll /data/
total 0
-rw-r--r-- 1 www www 0 May 1 12:21 web01.txt
-rw-r--r-- 1 www www 0 May 1 12:20 web02.txt
2.1.5 NFS客戶端參數(shù)說明
- /proc/mounts 查看掛載參數(shù)
[root@web01 ~]# cat /proc/mounts
172.16.1.31:/data /data nfs4 rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=172.16.1.7,local_lock=none,addr=172.16.1.31 0 0
- 三種方法解決NFS服務(wù)端宕機(jī),客戶端無法啟動(dòng)的原因。
[root@web01 ~]# tail -3 /etc/fstab
#172.16.1.31:/data /data nfs defaults,_netdev 0 0
#172.16.1.31:/data /data nfs defaults,soft 0 0
172.16.1.31:/data /data nfs defaults,hard,intr 0 0
- 指定inode和block的大小
[root@web01 ~]# mount -t nfs -o hard,intr,rsize=131072,wsize=131072 172.16.1.31:/data/ /mnt
mount -0 本地參數(shù)優(yōu)化
- noexec 不允許在掛載文件中執(zhí)行二進(jìn)制命令,這樣做可以提高安全
- noatime 訪問文件時(shí)不更新時(shí)間戳,高并發(fā)的情況下可以提高I/O性能。
- nodiratime 訪問目錄時(shí)不更新時(shí)間戳,高并發(fā)的情況下可以提高I/O性能。
- nosuid 不允許別人在這個(gè)分區(qū)上使用suid
- 文件系統(tǒng)只讀故障
https://blog.csdn.net/daydayup_gzm/article/details/52744540
- 生產(chǎn)中安全優(yōu)化掛載(可選)
[root@web02 ~]# mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,hard,intr,rsize=131072,wsize=131072 172.16.1.31:/data /mnt
#測試發(fā)現(xiàn)默認(rèn)掛載就好
- nfs 內(nèi)核優(yōu)化
[root@nfs01 ~]# cat >>/etc/sysctl.conf<<EOF
> net.core.wmem_default = 8388608
> net.core.rmem_default = 8388608
> net.core.wmem_max = 16777216
> net.core.rmem_max = 16777216
> EOF
[root@nfs01 ~]# sysctl -p
net.core.wmem_default = 8388608
指定發(fā)送套接字緩沖區(qū)大小的默認(rèn)值
net.core.rmem_default = 8388608
指定接收套接字緩沖區(qū)大小的默認(rèn)值
net.core.wmem_max = 16777216
指定發(fā)送套接字緩沖區(qū)大小的最大值
net.core.rmem_max = 16777216
指定接收套接字緩沖區(qū)大小的最大值
三、NFS生產(chǎn)優(yōu)化小結(jié)
3.1.1 磁盤優(yōu)化
- 多塊磁盤做raid5或raid10(最佳)。
- 網(wǎng)卡吞吐量要大,至少千兆(多塊網(wǎng)卡可以做bond)http://www.itdecent.cn/p/e31b2b5b2f22
3.1.2 服務(wù)端和客戶端優(yōu)化
1,請看上述筆記
3.1.3 內(nèi)核優(yōu)化
- 內(nèi)核優(yōu)化
[root@web02 ~]# cat >>/etc/sysctl.conf<<EOF
> net.core.wmem_default = 8388608
> net.core.rmem_default = 8388608
> net.core.wmem_max = 16777216
> net.core.rmem_max = 16777216
> EOF
[root@web02 ~]# sysctl -p
3.1.4 切割NFS
- 把多個(gè)目錄分配到不同的NFS服務(wù)上
3.1.5 優(yōu)化思想
- 能在前端訪問解決的問題,就不要留到后端解決。