1. NFS介绍

     NFS 是 Network File System 的缩写,即网络文件系统。一种使用于分散式文件系统的协定,由Sun 公司开发,于 1984 年向外公布。功能是通过网络让不同的机器、不同的操作系统能够彼此分享个别的数据,让应用程序在客户端通过网络访问位于服务器磁盘中的数据,是在类 Unix 系统间实现磁盘文件共享的一种方法。

     NFS 在文件传送或信息传送过程中依赖于 RPC 协议。RPC,远程过程调用 (Remote Procedure Call)是能使客户端执行其他系统中程序的一种机制。 NFS 本身是没有提供信息传输的协议和功能的。

     NFS 应用场景,常用于高可用文件共享,多台服务器共享同样的数据,可扩展性比较差,本身高可用方案不完善,取而代之的数据量比较大的可以采用 MFS、 TFS、 HDFS 等等分布式文件系统。

2. NFS配置参数

1.NFS常见配置参数:

参数名称参数用途
ro只读权限
rw可读可写权限
sync同步写入数据,性能可能会降低
async异步写入数据,性能高,数据易丢失
all_squash无论身份如何压缩用户身份为匿名用户
anonuid配置all_squash使用,指定NFS的用户UID,必须存在系统
anongid配置all_squash使用,指定NFS的用户UID,必须存在系统

 

2. NFS重要配置文件:

NFS常用路径说明
/etc/exportsNFS主配置文件,配置NFS共享目录
/usr/sbin/exportfsexportfs –rv nfs服务的管理命令,加载配置生效
/usr/sbin/showmountshowmount –e remoteip 查看NFS配置及挂载结果的命令
/var/lib/nfs/etabnfs配置文件的完整参数,有很多都是默认参数
/proc/mountsgrep mnt /proc/mounts 查看客户端挂载参数

3. NFS部署

3.1  NFS部署环境准备

OS版本:centos 7.5 64bit

[root@nfs-server ~]# cat /etc/redhat-release #查看系统版本

image  

[root@nfs-server ~]# uname -r #查看内核版本

image  

[root@nfs-server ~]# systemctl stop ebtables firewalld #关闭ebtables firewall防火墙

[root@nfs-server ~]# systemctl disable ebtables firewalld #重启不启动ebtables firewall防火墙

image  

[root@nfs-server ~]# vim /etc/sysconfig/selinux         #设置selinux 状态为disabled

image  

[root@nfs-server ~]# setenforce 0                     # 临时将selinux设置为disabled

[root@nfs-server ~]# getenforce                       #检测selinux是否关闭

image  

[root@nfs-server ~]# ifconfig ens33|awk -F '[ :]+'   'NR==2{print $3}'                    #查看IP地址

image  

[root@nfs-server ~]# hostname                               #查看主机名

image  

3.2 部署配置NFS服务

1. 安装NFS服务

[root@nfs-server ~]# yum -y install nfs-utils rpcbind

image  

2. 启动服务

[root@nfs-server ~]# systemctl start  nfs-server  rpcbind

[root@nfs-server ~]# systemctl enable nfs-server  rpcbind

image  

3. 查看rpcbind的注册信息

[root@nfs-server ~]# rpcinfo -p localhost

image  

4. 查看rpc进程

[root@nfs-server ~]# ps -ef |egrep "rpc|nfs"

image  

5. 修改NFS配置文件共享目录share至192.168.1.0/24网段,可读,可写,统一账户

[root@nfs-server ~]# vim /etc/exports

/share 192.168.1.0/24(rw,sync,all_squash)

image  

解释:

/share         192.168.1.0/24 (rw,sync,all_squash)

共享目录    可访问放段(读写权限,实时同步,压缩匿名权限)

6. 创建共享目录

[root@nfs-server ~]# mkdir /share

image  

7. 重启NFS服务

[root@nfs-server ~]# systemctl restart nfs-server

[root@nfs-server ~]# systemctl status nfs-server

image  

8. 查看NFS共享的文件信息 ,NFS统一给客户端的65534用户权限

[root@nfs-server ~]# cat /var/lib/nfs/etab

image  

9.  查看UID为65534的用户名

[root@nfs-server ~]# grep "65534" /etc/passwd

image  

10. 修改共享目录的所有者,所属组

[root@nfs-server ~]# chown -R nfsnobody:nfsnobody /share

[root@nfs-server ~]# ll / |grep share

image  

12. 查看server是否配置成功

[root@nfs-server ~]# showmount -e 192.168.1.234

image  

3.3 客户端测试

1. 安装软件包

[root@nfs-client ~]# yum -y install nfs-utils rpcbind

image  

2. 启动服务

[root@nfs-client ~]# systemctl start nfs

[root@nfs-client ~]# systemctl enable nfs

image  

3. 创建挂载点

[root@nfs-client ~]# mkdir -p /nfs/share

image  

4. 临时挂载访问

[root@nfs-client ~]# mount -t nfs 192.168.1.234:/share /nfs/share

[root@nfs-client ~]# df –h

image  

5. 切换目录到挂载点下,创建一个chenjf的文件

[root@NFS-client ~]# cd /nfs/share/
[root@NFS-client share]# touch chenjf
[root@NFS-client share]# ls

image  

6. 永久挂载

[root@nfs-client ~]# vim /etc/fstab

192.168.1.234:/share  /nfs/share  nfs defaults 0 0

image  

7. 挂载全部

[root@nfs-client ~]# mount -a

[root@nfs-client ~]# df –h

image  

8. 卸载设备时请不要在挂载点下面

[root@nfs-client ~]# umount /nfs/share

image  

9. 当NFS-Server宕机,强制卸载

[root@nfs-client ~]# umount -lf /nfs/share

[root@nfs-client ~]# df -h 

image  

3.4 NFS优化

3.4.1 服务器优化

1、 硬件优化

NFS服务器硬件的选择

SAS/SSD磁盘,多买几块硬盘做raid0/raid10。网卡吞吐量要大,至少千兆网卡。

2、 内核优化

cat >>/etc/sysctl.conf<<EOF

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

EOF

1. 加载内核生效

sysctl -p

2. 该文件指定了接收套接字缓冲区大小的缺省值(以字节为单位),缺省设置为124928

/proc/sys/net/core/rmem_default

3. 该文件指定了接收套接字缓冲区大小的最大值(以字节为单位),缺省设置为124928

/proc/sys/net/core/rmem_max

4. 该文件指定了发送套接字缓冲区大小的缺省值(以字节为单位),缺省设置为124928

/proc/sys/net/core/wmem_default

5. 该文件指定了发送套接字缓冲区大小的最大值(以字节为单位),缺省设置为124928

/proc/sys/net/core/wmem_max

3.4.2 客户端优化

企业生产环境NFS性能挂载参数

1. 安全挂载

mount -t nfs -o nosuid,noexec,nodev,rw 192.168.1.234:/share /nfs/share

2. 禁止更新时间戳挂载

mount -t nfs -o noatime,nodiratime 192.168.1.234:/share /nfs/share

3. 追求极致

mount -t nfs -o rsize=131072,wrsize=131072 192.168.1.234:/share /nfs/share

4. 安全加优化的挂载方式(推荐)

mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,intr,rsize=131072,wrsize=131072 192.168.1.234:/share /nfs/share

5. 默认的挂载方式(默认参数满足企业日常需求)

mount -t nfs 192.168.1.234:/share /nfs/share