NFS的监听端口为 2049,可以使用如下方式查看:
[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
二.搭建一台NFS服务器共享特定资源
1.部署环境
服务器 操作系统版本 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上安装NFS
#安装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上查看是否能看到共享目录
#首先在客户端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调优完毕。