1.NFS是Network File System的簡寫,即網絡文件系統。網絡文件系統是FreeBSD支持的文件系統中的一種,也被稱爲NFS,NFS容許一個系統在網絡上與他人共享目錄和文件。經過使用NFS,用戶和程序能夠像訪問本地文件同樣訪問遠端系統上的文件。node
2.在項目中,若是A,B,C服務器都須要D服務器上的某個目錄下的文件,就能夠把D服務器上的目錄使用NFS的方式共享給A,B,C服務器,既方便又節省磁盤空間,D服務器還能夠設置權限限制A,B,C的訪問。web
3.NFS的軟件架構是C/S架構(即客戶端/服務器端架構)。shell
4.NFS的監聽端口爲 2049,可使用以下方式查看:apache
[root@node5 ~]# cat /etc/services | grep "^nfs" nfs 2049/tcp nfsd shilp # Network File System nfs 2049/udp nfsd shilp # Network File System nfs 2049/sctp nfsd shilp # Network File System nfsd-keepalive 1110/udp # Client status info nfsrdma 20049/tcp # Network File System (NFS) over RDMA nfsrdma 20049/udp # Network File System (NFS) over RDMA nfsrdma 20049/sctp # Network File System (NFS) over RDMA
1.部署環境vim
服務器 | 操做系統版本 | CPU架構 | 進程 | 功能描述 |
---|---|---|---|---|
node5/192.168.110.184 | CentOS Linux release 7.9.2009 (Core) | x86_64 | rpcbind,nfs-server | NFS服務器端 |
node8/192.168.110.186 | CentOS Linux release 7.9.2009 (Core) | x86_64 | 須要安裝nfs-server,可是不須要啓動 | NFS客戶端 |
2.在node5上安裝NFScentos
#安裝nfs [root@node5 ~]# yum -y install rpcbind nfs-utils [root@node5 ~]# rpm -qa | grep rpcbind rpcbind-0.2.0-49.el7.x86_64 [root@node5 ~]# rpm -qa | grep nfs libnfsidmap-0.25-19.el7.x86_64 nfs-utils-1.3.0-0.68.el7.x86_64 #查看已安裝的nfs-utils命令,生成了哪些文件 [root@node5 ~]# rpm -ql nfs-utils /etc/exports.d /etc/gssproxy/24-nfs-server.conf /etc/modprobe.d/lockd.conf /etc/nfs.conf /etc/nfsmount.conf /etc/request-key.d/id_resolver.conf /etc/sysconfig/nfs ...... /var/lib/nfs/state /var/lib/nfs/v4recovery /var/lib/nfs/xtab #NFS的核心配置文件 [root@node5 ~]# ls /etc/exports /etc/exports [root@node5 ~]# systemctl start rpcbind #啓動NFS [root@node5 ~]# systemctl start nfs [root@node5 ~]# systemctl status rpcbind ● rpcbind.service - RPC bind service Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2020-12-21 23:38:40 CST; 31s ago Process: 75239 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS) Main PID: 75240 (rpcbind) CGroup: /system.slice/rpcbind.service └─75240 /sbin/rpcbind -w Dec 21 23:38:40 node5 systemd[1]: Starting RPC bind service... Dec 21 23:38:40 node5 systemd[1]: Started RPC bind service. #查看NFS啓動狀態 [root@node5 ~]# systemctl status nfs ● nfs-server.service - NFS server and services Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled) Drop-In: /run/systemd/generator/nfs-server.service.d └─order-with-mounts.conf Active: active (exited) since Mon 2020-12-21 23:38:55 CST; 21s ago Process: 75283 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS) Process: 75266 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS) Process: 75263 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS) Main PID: 75266 (code=exited, status=0/SUCCESS) CGroup: /system.slice/nfs-server.service Dec 21 23:38:55 node5 systemd[1]: Starting NFS server and services... Dec 21 23:38:55 node5 systemd[1]: Started NFS server and services. #查看NFS端口有沒有監聽 [root@node5 ~]# netstat -antup | grep 2049 tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN - tcp6 0 0 :::2049 :::* LISTEN - udp 0 0 0.0.0.0:2049 0.0.0.0:* - udp6 0 0 :::2049 :::* - #設置NFS開機自啓動 [root@node5 ~]# systemctl enable nfs Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service. #查看NFS是否開機自啓動 [root@node5 ~]# systemctl is-enabled nfs enabled
3.修改配置文件,把node5的Tomcat發佈目錄分享給node8緩存
#修改配置文件 [root@node5 ~]# vim /etc/exports #/usr/local/apache-tomcat-8.0.51/webapps表示要共享的目錄 #192.168.110.186表示只對192.168.110.186這臺服務器共享 #rw,no_root_squash表示訪問權限,rw表示有讀寫權限,no_root_squash表示root用戶具備根目錄的徹底管理訪問權限 #其餘可用參數說明(看狀況配置便可): #ro #只讀訪問。 #rw #讀寫訪問。 #sync #資料同步寫入到內存與硬盤當中。 #async #資料會先暫存於內存當中,而非直接寫入硬盤。 #secure #NFS經過1024如下的安全TCP/IP端口發送。 #insecure #NFS經過1024以上的端口發送。 #wdelay #若是多個用戶要寫入NFS目錄,則歸組寫入(默認)。 #no_wdelay #若是多個用戶要寫入NFS目錄,則當即寫入,當使用async時,無需此設置。 #hide #在NFS共享目錄中不共享其子目錄。 #no_hide #共享NFS目錄的子目錄。 #subtree_check #若是共享/usr/bin之類的子目錄時,強制NFS檢查父目錄的權限(默認)。 #no_subtree_check #和上面相對,不檢查父目錄權限。 #all_squash #共享文件的UID和GID映射匿名用戶anonymous,適合公用目錄。 #no_all_squash #保留共享文件的UID和GID(默認)。 #root_squash #root用戶的全部請求映射成如anonymous用戶同樣的權限(默認)。 #no_root_squash #root用戶具備根目錄的徹底管理訪問權限。 [root@node5 ~]# cat /etc/exports /usr/local/apache-tomcat-8.0.51/webapps 192.168.110.186(rw,no_root_squash) #動態加載配置文件 [root@node5 ~]# exportfs -rv exporting 192.168.110.186:/usr/local/apache-tomcat-8.0.51/webapps #nfs默認使用nfsnobody用戶,因此修改共享目錄擁有者 [root@node5 ~]# chown nfsnobody:nfsnobody -R /usr/local/apache-tomcat-8.0.51/webapps
4.在客戶端node8上查看是否能看到共享目錄tomcat
#首先在客戶端node8安裝nfs,不須要啓動,可是須要nfs裏包含的命令showmount [root@node8 ~]# yum -y install nfs-utils [root@node8 ~]# rpm -qa | grep nfs nfs-utils-1.3.0-0.48.el7.x86_64 libnfsidmap-0.25-17.el7.x86_64 #查看服務器端的共享目錄,showmount語法:showmount -e NFS服務器端IP #在客戶端node8上能夠看到服務器端的共享目錄,則服務器端的/etc/exports配置正確 [root@node8 ~]# showmount -e 192.168.110.184 Export list for 192.168.110.184: /usr/local/apache-tomcat-8.0.51/webapps 192.168.110.186
5.掛載共享目錄安全
#建立掛載目錄 [root@node8 ~]# mkdir /tomcatapp #掛載共享目錄 [root@node8 ~]# mount -t nfs 192.168.110.184:/usr/local/apache-tomcat-8.0.51/webapps /tomcatapp #查看掛載狀況 [root@node8 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 17G 14G 3.6G 80% / devtmpfs 478M 0 478M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 13M 476M 3% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/loop0 4.3G 4.3G 0 100% /mnt/cdrom /dev/sda1 1014M 125M 890M 13% /boot tmpfs 98M 0 98M 0% /run/user/0 192.168.110.184:/usr/local/apache-tomcat-8.0.51/webapps 17G 14G 4.0G 77% /tomcatapp #此時在/tomcatapp就能夠看到共享文件了 [root@node8 ~]# cd /tomcatapp/ [root@node8 tomcatapp]# pwd /tomcatapp #具備讀的權限 [root@node8 tomcatapp]# ls docs examples host-manager index.jsp manager ROOT test #具備寫的權限 [root@node8 tomcatapp]# mkdir 2358 [root@node8 tomcatapp]# ls 2358 docs examples host-manager index.jsp manager ROOT test
6.配置開機自動掛載共享目錄服務器
[root@node8 tomcatapp]# vim /etc/fstab #在/etc/fstab的末尾添加「192.168.110.184:/usr/local/apache-tomcat-8.0.51/webapps /tomcatapp nfs defaults 0 0」,使其能夠開機自動掛載 [root@node8 tomcatapp]# cat /etc/fstab /dev/mapper/centos-root / xfs defaults 0 0 UUID=db58ac18-ddda-44e9-bbf5-2584fac03ed6 /boot xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0 /mnt/yum-iso/CentOS-7-x86_64-DVD-1708.iso /mnt/cdrom iso9660 loop 0 0 192.168.110.184:/usr/local/apache-tomcat-8.0.51/webapps /tomcatapp nfs defaults 0 0
自此,NFS服務器搭建完畢。
1.調優思路:先調節系統內核參數,使操做系統性能最優,而後再調節NFS客戶端掛載參數。
2.系統內核優化
[root@node8 ~]# vim /etc/sysctl.conf [root@node8 ~]# cat /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). #內核默認讀緩存 net.core.wmem_default = 8388608 #內核默認寫緩存 net.core.rmem_default = 8388608 #內核最大讀緩存 net.core.rmem_max = 16777216 #內核最大寫緩存 net.core.wmem_max = 16777216 #使配置文件生效 [root@node8 ~]# sysctl -p net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216
3.NFS客戶端掛載參數優化
[root@node8 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 17G 14G 3.6G 80% / devtmpfs 478M 0 478M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 13M 476M 3% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/loop0 4.3G 4.3G 0 100% /mnt/cdrom /dev/sda1 1014M 125M 890M 13% /boot tmpfs 98M 0 98M 0% /run/user/0 192.168.110.184:/usr/local/apache-tomcat-8.0.51/webapps 17G 14G 4.0G 77% /tomcatapp #先卸載共享目錄 [root@node8 ~]# umount 192.168.110.184:/usr/local/apache-tomcat-8.0.51/webapps [root@node8 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 17G 14G 3.6G 80% / devtmpfs 478M 0 478M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 13M 476M 3% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/loop0 4.3G 4.3G 0 100% /mnt/cdrom /dev/sda1 1014M 125M 890M 13% /boot tmpfs 98M 0 98M 0% /run/user/0 #優化NFS客戶端掛載參數 #-o後面跟掛載參數 #noatime:取消更新文件系統上的inode訪問時間,提高I/O性能,優化I/O目的,推薦使用。 #nodiratime:取消更新文件系統上的directory inode訪問時間,高併發環境,推薦顯式應用該選項,提升系統性能。 #intr:能夠中斷不成功的掛載。 #rsize/wsize:讀取(rsize)/寫入(wsize)的區塊大小(block size),這個設置值能夠影響客戶端與服務端傳輸數據的緩衝存儲量。通常來講,若是在局域網內,而且客戶端與服務端都具備足夠的內存,這個值能夠設置大一點,好比說32768(bytes),提高緩衝區塊將可提高NFS文件系統的傳輸能力。但設置的值也不要太大,最好是實現網絡可以傳輸的最大值爲限。 [root@node8 ~]# mount -t nfs -o noatime,nodiratime,rsize=131072,wsize=131072,intr 192.168.110.184:/usr/local/apache-tomcat-8.0.51/webapps /tomcatapp [root@node8 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 17G 14G 3.6G 80% / devtmpfs 478M 0 478M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 13M 476M 3% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/loop0 4.3G 4.3G 0 100% /mnt/cdrom /dev/sda1 1014M 125M 890M 13% /boot tmpfs 98M 0 98M 0% /run/user/0 192.168.110.184:/usr/local/apache-tomcat-8.0.51/webapps 17G 14G 4.0G 77% /tomcatapp [root@node8 ~]# vim /etc/fstab #把優化後的掛載參數也加入到配置文件中 [root@node8 ~]# cat /etc/fstab /dev/mapper/centos-root / xfs defaults 0 0 UUID=db58ac18-ddda-44e9-bbf5-2584fac03ed6 /boot xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0 /mnt/yum-iso/CentOS-7-x86_64-DVD-1708.iso /mnt/cdrom iso9660 loop 0 0 192.168.110.184:/usr/local/apache-tomcat-8.0.51/webapps /tomcatapp nfs noatime,nodiratime,rsize=131072,wsize=131072,intr 0 0 [root@node8 ~]# mount -a [root@node8 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 17G 14G 3.6G 80% / devtmpfs 478M 0 478M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 13M 476M 3% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/loop0 4.3G 4.3G 0 100% /mnt/cdrom /dev/sda1 1014M 125M 890M 13% /boot tmpfs 98M 0 98M 0% /run/user/0 192.168.110.184:/usr/local/apache-tomcat-8.0.51/webapps 17G 14G 4.0G 77% /tomcatapp
自此,NFS調優完畢。