yum install nfs-utils rpcbind -y
systemctl enable nfs.service
systemctl start nfs.service
cat > /etc/exports <<EOF
#rync,write in diisk,user
/data/v1 10.104.0.0/8(rw,sync,no_root_squash)
/data/v2 10.104.0.0/8(rw,sync,no_root_squash)
/data/v3 10.104.0.0/8(rw,sync,no_root_squash)
EOF
exportfs –arv # 当服务端修改了配置文件以后可以使用
showmount -e 127.0.0.1 #查看共享点(可挂载)
mount -t nfs 10.10.10.31:/data /backup/ #挂载到本地backup下并且查看
2、NFS客户端和NFS服务端通讯过程6、应用建议
7、NFS软件安装(服务端)
yum install nfs-utils rpcbind -y #安装
rpm -ql nfs-utils #查看安装了什么内容,了解即可,用处不大
systemctl status rpcbind.service #安装完成后启动rpcbind ,centos7默认安装完成后启动
systemctl status nfs #nfs安装完成后,系统并没有加入开机自启动,而且默认也没有启动
systemctl enable nfs.service
systemctl start nfs #启动nfs
netstat -lntup|grep 2049 #检查启动状态
rpcinfo -p localhost #当启动nfs.service后,再查看rpcbind启动信息,可以看到nfs已经向rpcbind注册启动端口
showmount -a # 查看谁挂载了
当服务端修改了配置文件以后可以使用
systemctl reload nfs
exportfs –rv
即可使修改的/etc/exports生效
#编辑配置文件(即设置共享目录)
[root@nfs01 ~]# cat /etc/exports
#rync,write in diisk,user
/data 10.10.10.0/24(rw,sync,all_squash, anonuid=1001,anongid=1001)
showmount -e 127.0.0.1 #查看共享点(可挂载)
mount -t nfs 10.10.10.31:/data /backup/ #挂载到本地backup下并且查看
mount
cat /proc/mounts
df -h
#这3个命令都可以查看挂载,如果nfs服务终止,则不能使用df -h会卡主
udi和gid web和nfs最好相同
若服务器不正常关机,会丢数据(解决办法:服务器主板电池加UPS不间断电源)
注意:不能有空格、生产环境中常用的一种配置,适合多客户端共享一个NFS目录。All_squash 也就是说不管客户端是以什么样的身份来进行访问的,都会被压缩成为all_squash后面所接的用户和群组身份。这边用anonuid、anongid编号来标示。
那么客户端以什么身份来访问?
客户端访问服务端默认是使用nfsnobody这个用户来进行访问的。uid和gid为65534。服务器默认共享时,也是加上了all_squash这个参数。并制定anonuid为65534(也就是nfsnobayd用户)。当然如果系统中nfsnobody是其他的uid,那么就有可能造成访问权限出现问题。所以最好我们可以通过一设置一个用户来访问,统一UID、GID。
挂载情况怎样呢?
有两个重要的文件,能够解决这个疑问。
/var/lib/nfs/etab
/var/lib/nfs/rmtab
这两个文件就能够查看服务器上共享了什么目录,到底有多少客户端挂载了共享,能查看到客户端挂载的具体信息。
etab这个文件能看到服务器上共享了哪些目录,执行哪些人可以使用,并且设定的参数为何。
rmtab这个文件就是能够查看到共享目录被挂载的情况。
NFS配置权限设置,即/etc/exports文件配置格式中小括号()里的参数集;
参数命令 |
参数用途 |
rw |
表示可读写 |
ro |
Read-only表示只能读权限 |
Sync |
请求或者写入数据时,数据同步写入到NFS server的硬盘中后才会返回 |
no_root_squas |
访问nfs server共享目录的用户如果是root的话,它对该目录具有root权限。 这个配置原本为无盘用户准备的。用户应避免使用! |
root_squash |
对于访问NFS server共享目录的用户,如果是root的话会被压缩成为nobody用户身份。 |
all_squash |
不管访问nfs server共享目录的用户身份如何包括root,它的权限都将被压缩成为匿名用户, 同时他们的udi和gid都会变成nobody或nfsnobody账户的uid,gid。 在多个nfs客户端同时读写nfs server数据时,这个参数很有用可以确保大家写入的数据的权限是一样的。 但不同系统有可能匿名用户的uid,gid不同。因为此处我们需要服务端和客户端之间的用户是一样的。 比如说:服务端指定匿名用户的UID为2000,那么客户端也一定要存在2000这个账号才可以 |
anonuid |
anonuid就是匿名的uid和gid。说明客户端以什么权限来访问服务端, 在默认情况下是nfsnobody。Uid65534. |
anongid |
同anongid,就是把uid换成gid而已 |
nfs共享目录如果不是nfsnobody则客户端没有写入权限,如果想让客户端也可以写,需要修改权限grep nfsnobody /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
查看nfs默认使用的用户以及共享的参数
[root@nfs01 data]# cat /var/lib/nfs/etab
/data 10.10.10.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)
[root@nfs01 ~]# mount|grep nfs
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
nfsd on /proc/fs/nfsd type nfsd (rw,relatime)
10.10.10.40:/data on /mnt type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=10.10.10.40,local_lock=none,addr=10.10.10.40)
扩展知识:
1)NFS客户端挂载后,往共享目录写入数据时卡住
答:大体nfs服务端重启之后,共享文件夹进入grace time(无敌时间)
2)NFS服务端,重启restart服务,客户端如果写入数据卡住了。
答:客户端在服务端重启后写入数据大概要等90秒
有关配置文件:
/proc/fs/nfsd/nfsv4gracetime
/proc/fs/nfsd/nfsv4leasetime
/proc/fs/nfsd/nfsv4recoverydir
#showmount 使用帮助
-a or --all
List both the client hostname or IP address and mounted directory in host:dir format. This info should
not be considered reliable. See the notes on rmtab in rpc.mountd(8).
-d or --directories
List only the directories mounted by some client.
-e or --exports
Show the NFS server's export list.
-h or --help
Provide a short help summary.
-v or --version
Report the current version number of the program.
--no-headers
Suppress the descriptive headings from the output.
服务端不写配置文件通过命令来实现:了解一下 临时提供共享目录
exportfs -o rw,sync 10.10.10.0/24:/data
8、NFS客户端、安装、挂载、优化
yum install nfs-utils rpcbind -y #客户端直接yum安装即可
systemctl list-unit-files|grep rpcbind.service #Cenots7 默认也是开机自启动的
#如果是centos6 开机启动自动挂载放在rc.local里面,因为fstab开机加载顺序,在网卡之前,没网络,无法挂载远端NFS
客户端挂载和优化
mount -t nfs -o noatime,nodiratime,nosuid,noexec,nodev 10.10.10.31:/data /backup/
补充:data下可以有有多个目录分别挂到不同的服务器
/data/blog/uploads
/data/bbs/uploads
挂载方式:
[root@moban /]# mount -t nfs -o noatime,nodiratime,nosuid,noexec,nodev 10.10.10.31:/data/blog/uploads/ /backup/
noatime nodiratime 代表不更新文件和目录的访问时间戳,减少磁盘IO (优化)
nosuid noexec nodev 取消suid功能,是否保留装置文件的特殊功能,选择nodev (安全)
noexec 一些二进制命令不可以执行,但是shell脚本可以执行,可以起到一定的安全效果!(安全)
如果挂载比较慢,可以修改hosts,这里可以用单个IP 或者域名,NDS解析(hosts)
注意事项:一定要事先退出挂载卸载目录!在进行 挂载/卸载目录
企业生产环境下,NFS性能优化挂载的例子
#禁止更新目录及文件时间戳挂载,命令如下:
mount -t nfs -o noatime,nodiratime 172.16.1.31:/data /mnt
#安全加优化的挂载方式如下:
mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,intr,rsize=131072,wsize=131072 172.16.1.31:/data /mnt
#默认的挂载方式如下:
mount -t nfs 172.16.1.31:/data /mnt
#如果是本地文件系统,使用如下命令:
mount /dev/sdb1 /mnt -o defaults,async,noatime,data=writeback,barrier=0
[root@template /]# mount|grep nfs
10.10.10.40:/data on /mnt type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=10.10.10.20,local_lock=none,addr=10.10.10.40)
rsize=131072,wsize=131072 #读写的大小
proto=tcp #协议
hard #客户端是以hard方式挂载,如果网络问题,会一直挂。和soft相反,
mountcat
cat /proc/mounts
df -h
#这3个命令都可以查看挂载,如果nfs服务端,服务终止,则不能使用df -h会卡主
客户端,ping nfs服务端,查看网络是否通telnet ip 111 查看端口是否通,是否防火墙原因showmount -e ip检查服务端 rpcbind和nfs服务是否启动检查客户端rpcbin是否启动如果服务端NFS挂掉以后,客户端有挂载的 此时df –h 命令不可用(特别注意,以后监控的时候,就不能使用df -h来监控)
如果卸载的时候提示 umount:/backup:device is busy 需要强制卸载,umonut lf /backup
cat >>/etc/sysctl.conf<<EOF
net.core.rmem_default = 8388608
net.core.wmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
EOF
sysctl -p
11、NFS简单高可用(还可以再中间加一台Haproxy服务器做负载均衡)
https://blog.51cto.com/13555423/2082796
[ mfs工作原理 ]
- 分布式原理
分布式文件系统就是把一些分散在多台计算机上的共享文件夹,集合到一个共享文件夹内,用户要访问这些文件夹的时候,只要打开一个文件夹,就可以的看到所有链接到此文件夹内的共享文件夹。
MFS是一个具有容错性的网络分布式文件系统,它把数据分散存放在多个物理服务器上,而呈现给用户的则是一个统一的资源。