NFS(Network File System)即网络文件系统,最先是由Sun Microsystems在内部做为实验完成开发,是第一个构建于IP协议之上的网络文件系统,主要功能是经过TCP/IP在不一样主机系统之间共享资源(文件或目录)。
NFS客户端能够经过挂载的方式将NFS服务器共享的数据目录挂载到本地,本地NFS的客户端应用能够透明地读写位于远端NFS服务器上的文件,就像访问本地文件同样。
NFS网络文件系统有点相似Samba服务。samba主要用于在windows和unix之间共享资源,资源包括文件、打印机等等。而NFS通常用于Unix系统之间的资源共享,固然也能够共享到Windows。css
硬件存储自己就很贵的,而随着公司业务的不断发展,网站并发继续加大时,硬件存储的扩展升级成本高,成几何倍的增加,这对于中小型企业来讲是一笔不菲的开销。
因此通常中小型网站就会选择NFS进行数据共享,若是大型网站颇有可能会用到更复杂的分布式文件系统,例如:MFS(Moosefs)、GlusterFS、FastDFS 等
NFS系统经历了近30年的发展, 已是一个很是稳定、可移植、可扩展、高性能 的企业级应用,因此在中小型企业中使用的比较普遍。html
在NFS服务器上设置好一个共享目录/data/server后,具备访问权限的NFS客户端均可以将/data/server这个共享目录挂载到本地的某个挂载点,这个挂载点能够本身随意指定。
客户端正确挂载完成后,就能够经过NFS客户端挂载点所在的目录查看NFS服务器共享出来的/data/server目录下的全部数据。在客户端查看时,这个共享目录就至关于客户端本地磁盘上的目录,几乎感受不到有何区别。
根据NFS服务器端设置的权限以及/data/server共享目录的本地系统权限,只要在指定的NFS客户端挂载共享目录, 就能够将数据轻松的存取到NFS服务器上的/data/server共享目录中。node
NFS是经过TCP/IP来进行服务端和客户端之间的数据传输,二者之间要传输数据就要有相对应的端口来进行传输。
可是这里有一个问题,就是NFS的端口是不固定的,由于NFS有不少的功能,而不一样的功能都会使用不一样的端口,NFS服务在启动时会随机选择端口,因此NFS服务器没法固定端口。linux
既然NFS服务器的端口不固定,NFS客户端又怎么知道服务器端使用的是那个端口呢 ?
因此就须要经过RPC服务来帮忙(Remote Procedure Call,远程过程调用)简称RPC
当NFS启动后,就会随机的使用一些端口,而后NFS就会向RPC去注册这些端口,RPC就会记录下这些端口,而且RPC会开起111端口,若是客户端有请求,那服务端的RPC就会将记录的NFS端口信息告知客户端。
也就是说RPC的主要功能就是记录NFS功能所对应的端口号,而且在NFS客户端请求时将对应的端口信息返给NFS客户端,从而确保客户端能够链接到正确的NFS端口,才能实现数据传输/数据交互的目的。vim
在启动NFS以前,首先要启动RPC服务,不然NFS就没法向RPC注册了。另外,若是RPC服务从新其从,原来注册好的NFS端口就会丢失,所以NFS服务也须要重启以从新向RPC注册端口信息。
要特别注意的是,修改了NFS的配置文件以后是不须要重启的,执行命令 /etc/init.d/nfs reload 或者 exportfs -rv 便可使NFS服务的配置文件/etc/exportfs生效。
不管是NFS客户端仍是NFS服务器端,都须要先启动RPC服务。
NFS客户端无需启动NFS,可是须要启动RPC服务。windows
1)首先服务器端启动RPC服务并开启111端口
2)启动NFS服务并向RPC注册端口信息
3)客户端向服务端的RPC请求服务端的NFS端口
4)服务端的RPC服务反馈NFS端口信息给客户端。
5)客户端经过获取到的NFS端口来创建和服务端的NFS链接并进行数据的传输。centos
我这里服务器端系统是 CentOS7,客户端是CentOS6。安全
关闭防火墙
[root@localhost ~]# systemctl stop firewalld.service #关闭防火墙
[root@localhost ~]# systemctl disable firewalld.service #禁止防火墙开机启动性能优化
关闭SELinuxbash
[root@localhost ~]# setenforce 0 #临时关闭
#修改配置文件 vim /etc/selinux/config
SELINUX=disabled
服务端须要的软件包 nfs-utils rpcbind
nfs-utils 是NFS服务的主程序包
rpcbind 是RPC服务的程序包
centos6和centos7默认没有安装nfs(centos5已经默认安装了), 因此须要咱们手动安装。可使用 yum -y install nfs-utils rpcbind 命令安装。
采用yum方式安装, 只须要安装 nfs-utils,yum会自动安装rpcbind。
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# rpm -qa nfs-utils rpcbind
[root@localhost ~]# yum -y install nfs-utils rpcbind
[root@localhost ~]# rpm -qa nfs-utils rpcbind #若是出现nfs和rpc开头的两个软件包,表示nfs已经安装好了
rpcbind-0.2.0-47.el7.x86_64
nfs-utils-1.3.0-0.61.el7.x86_64
在启动nfs服务前, 要先启动rpc
[root@localhost ~]# systemctl start rpcbind #启动rpc
[root@localhost ~]# systemctl enable rpcbind #将加入开机启动那个
查看是否启动成功
[root@localhost ~]# systemctl status rpcbind #查看rpc运行状态
[root@localhost ~]# systemctl list-unit-files | grep rpcbind # 查看rpc是否加入开机启动
[root@localhost ~]# rpcinfo -p localhost #查看nfs服务向rpc注册的端口信息,由于nfs还没启动,因此没有太多的端口。
[root@localhost ~]# rpcinfo -p localhost 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
[root@localhost ~]# systemctl start nfs #启动nfs
[root@localhost ~]# systemctl enable nfs #将nfs加入开机启动
查看是否启动成功
[root@localhost ~]# systemctl status nfs #查看服务运行状态
[root@localhost ~]# systemctl list-unit-files | grep nfs.service #查看是否将nfs服务加入开机运行
[root@localhost ~]# rpcinfo -p localhost #查看nfs服务向rpc注册的端口信息,和刚才没启动nfs的时相比较,如今监听了更多的端口。
[root@localhost ~]# rpcinfo -p localhost 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 37652 status 100024 1 tcp 45595 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 58685 nlockmgr 100021 3 udp 58685 nlockmgr 100021 4 udp 58685 nlockmgr 100021 1 tcp 41537 nlockmgr 100021 3 tcp 41537 nlockmgr 100021 4 tcp 41537 nlockmgr
NFS服务的主要任务是共享文件系统数据,而文件系统数据的共享离不开权限问题。
因此NFS服务器启动时最少须要两个不一样的进程,一个是管理nfs客户端是否可以登入的rpc.nfsd主进程,另外一个用于管理nfs客户端是否可以取得对应权限的rpc.mountd进程。
若是还须要管理磁盘配额,则nfs还须要加载rpc.rquotad进程。
[root@localhost ~]# ps -ef | grep rpc rpc 11095 1 0 02:26 ? 00:00:00 /sbin/rpcbind -w rpcuser 11160 1 0 02:59 ? 00:00:00 /usr/sbin/rpc.statd root 11170 2 0 02:59 ? 00:00:00 [rpciod] root 11174 1 0 02:59 ? 00:00:00 /usr/sbin/rpc.idmapd root 11180 1 0 02:59 ? 00:00:00 /usr/sbin/rpc.mountd root 11812 10857 0 03:28 pts/2 00:00:00 grep --color=auto rpc
[root@localhost ~]# ps -ef | grep nfs root 11185 2 0 02:59 ? 00:00:00 [nfsd4_callbacks] root 11191 2 0 02:59 ? 00:00:00 [nfsd] root 11192 2 0 02:59 ? 00:00:00 [nfsd] root 11193 2 0 02:59 ? 00:00:00 [nfsd] root 11194 2 0 02:59 ? 00:00:00 [nfsd] root 11195 2 0 02:59 ? 00:00:00 [nfsd] root 11196 2 0 02:59 ? 00:00:00 [nfsd] root 11197 2 0 02:59 ? 00:00:00 [nfsd] root 11198 2 0 02:59 ? 00:00:00 [nfsd] root 11814 10857 0 03:30 pts/2 00:00:00 grep --color=auto nfs
NFS 服务的配置文件
NFS服务的默认配置文件路径 /etc/exportfs 而且默认是空的,须要用户自行配置。
/etc/exportfs 文件配置的格式:
NFS共享目录 NFS客户端地址1 (参数1,参数2,只读仍是可写) NFS客户端地址2 (参数1,参数2,......)
参数说明:
NFS共享目录:是服务器端的本地目录,是咱们想要共享给网络上其余主机使用的目录。假如我要共享的是/data/server目录,那么此选项能够就直接写/data/server
NFS客户端地址:客户端地址可以设置一个网段,也能够设置为单个主机,
参数:如读写权限(rw,同步更新sync,压缩来访帐户all_squash,压缩后的匿名帐号anonuid=uid,anongid=gid等等..)
配置实例1: /data/server 192.168.1.11(rw,sync)
###括号前不能有空格 rw表示可读写,sync表示同步更新到磁盘,同步将内存内的文件写入到磁盘空间,保证数据不丢失,但会影响性能。
配置实例2:/data/server 192.168.1.11/24(rw,sync,all_squash,anonuid=65534,anongid=65534)
###生产环境中经常使用的一种配置,适合多客户端共享一个NFS目录。all_squash 也就是说无论客户端是以什么样的身份来进行访问的,都会被压缩成为all_squash后面所接的用户和群组身份。这边用anonuid、anongid编号来表示。
配置实例3:/data/server 192.168.1.11/24(ro)
###表示只能读权限
NFS配置权限设置,即/etc/exports文件配置格式中小括号()里的参数集。
rw 表示可读写
ro Read-only表示只能读权限
sync 写入数据时数据同步写入到NFS server的硬盘中后才会返回,数据安全不会丢失。
async 写入数据时先写到内存缓冲区,直到硬盘有空档时才会再写到磁盘,这样能够提高写入效率,可是若是服务器宕机,缓冲区的数据没来得及写入到磁盘,这些数据将会丢失。
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为65534,那么客户端也必定要存在65534这个帐号才能够
anonuid anonuid就是匿名的uid和gid。说明客户端以什么权限来访问服务端,在默认状况下是nfsnobody。uid65534.
anongid 同anongid,就是把uid换成gid而已。
客户端来访问的用户
客户端访问服务端默认是使用nfsnobody这个用户来进行访问的。uid和gid为65534。服务器默认共享时,也是加上了all_squash这个参数。
并制定anonuid为65534(也就是nfsnobayd用户)。固然若是系统中nfsnobody是其余的uid,那么就有可能形成访问权限出现问题。
因此最好咱们能够经过一设置一个用户来访问,统一UID、GID。
查看客户端挂载状况
有两个重要的文件/var/lib/nfs/etab、/var/lib/nfs/rmtab这两个文件就可以查看服务器上共享了什么目录,到底有多少客户端挂载了共享,能查看到客户端挂载的具体信息。
一、/var/lib/nfs/etab这个文件能看到服务器上共享了哪些目录,执行哪些人可使用,而且设定的参数为什么。
二、/var/lib/nfs/rmtab这个文件就是可以查看到共享目录被挂载的状况。
实例:共享/data/server目录给192.168.1.11
[root@localhost ~]# mkdir -p /data/server #建立共享目录 [root@localhost ~]# ll -d /data/server drwxr-xr-x 2 root root 6 Mar 13 15:12 /data/server/ #注意如今共享的目录的权限为只有root才有写权限。 [root@localhost ~]# chown nfsnobody.nfsnobody /data/server/ #更改目录所属主和所属组。 [root@localhost ~]# ll -d /data/server drwxr-xr-x 2 nfsnobody nfsnobody 6 Mar 13 15:12 /data/server/ #如今nfsnobody用户有读写权限了。
[root@localhost ~]# cat /etc/exports # Shared /data/server directory to 192.168.1.11 /data/server 192.168.1.11(rw,sync) [root@localhost ~]# systemctl reload nfs #从新加载nfs [root@localhost ~]# showmount -e localhost #服务器本地确认是否共享正常 Export list for localhost: /data/server 192.168.1.11
权限说明:在配置文件中设置了权限rw。只代表了网络端的主机可以有权限去服务器端去写文件,但还须要经过服务器端的本地目录的权限。
而且客户端往服务端去写文件的用户身份是nfsnobody、nfsnobody UID=65534。那么也就是说客户端须要经过两层的权限来控制的。NFS配置文件—>共享目录文件的权限。
服务器端 nfsstat -s | grep Server
[root@localhost server]# nfsstat -s | grep Server Server rpc stats: Server nfs v4: Server nfs v4 operations:
如今就是让客户端来进行挂载就能够了。
--------------------------------------------------------
我这里客户端是CentOS6
NFS客户端只须要启动rpc服务便可。
关闭防火墙
[root@localhost ~]# /etc/init.d/iptables stop #关闭防火墙
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
[root@localhost ~]# chkconfig iptables off #禁止防火墙开机启动
关闭SELinu
[root@localhost ~]# setenforce 0 #临时关闭
#修改配置文件 vim /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@localhost ~]# uname -a
Linux localhost.localdomain 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# rpm -qa nfs-utils rpcbind
[root@localhost ~]# yum -y install nfs-utils rpcbind [root@localhost ~]# rpm -qa nfs-utils rpcbind #若是出现nfs和rpc开头的两个软件包,表示nfs已经安装好了 nfs-utils-1.2.3-78.el6_10.1.x86_64 rpcbind-0.2.0-16.el6.x86_64
在启动nfs服务前, 要先启动rpc
[root@localhost ~]# /etc/init.d/rpcbind start #启动rpc Starting rpcbind: [ OK ] [root@localhost ~]# chkconfig rpcbind on #将rpc加入开机启动那个 [root@localhost ~]# chkconfig --list | grep rpcbind #查看rpc是否加入开机启动 rpcbind 0:off 1:off 2:on 3:on 4:on 5:on 6:off
查看是否启动成功
[root@localhost ~]# /etc/init.d/rpcbind status rpcbind (pid 2310) is running... [root@localhost ~]# netstat -anput | grep rpc tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 2310/rpcbind tcp 0 0 :::111 :::* LISTEN 2310/rpcbind udp 0 0 0.0.0.0:111 0.0.0.0:* 2310/rpcbind udp 0 0 0.0.0.0:789 0.0.0.0:* 2310/rpcbind udp 0 0 :::111 :::* 2310/rpcbind udp 0 0 :::789 :::* 2310/rpcbind
[root@localhost ~]# rpcinfo -p localhost #查看nfs服务想rpc注册的端口信息,客户端只启动rpc服务便可。
[root@localhost ~]# rpcinfo -p localhost 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
查看服务器端的共享信息
[root@localhost ~]# showmount -e 192.168.1.21 Export list for 192.168.1.21: /data/server 192.168.1.11
挂载服务器共享出来的目录
[root@localhost ~]# mkdir /data/client # 要先建立挂载点 [root@localhost ~]# mount -t nfs 192.168.1.21:/data/server /data/client #把共享目录挂载到本地
#mount 命令格式以下:
mount -t 类型 device localedir(本地目录)
以上这个命令device=192.168.1.21:/data/server 类型为nfs
查看磁盘状况
[root@localhost ~]# df -TH Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root ext4 19G 831M 17G 5% / tmpfs tmpfs 523M 0 523M 0% /dev/shm /dev/sda1 ext4 500M 29M 445M 7% /boot 192.168.1.21:/data/server nfs 19G 1.2G 18G 7% /data/client
查看挂载状况
[root@localhost client]# mount -l /dev/mapper/VolGroup-lv_root on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw) /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) 192.168.1.21:/data/server on /data/client type nfs (rw,vers=4,addr=192.168.1.21,clientaddr=192.168.1.11)
查看更详细的挂载信息 cat /proc/mounts
[root@localhost client]# cat /proc/mounts rootfs / rootfs rw 0 0 proc /proc proc rw,relatime 0 0 sysfs /sys sysfs rw,relatime 0 0 devtmpfs /dev devtmpfs rw,relatime,size=499224k,nr_inodes=124806,mode=755 0 0 devpts /dev/pts devpts rw,relatime,gid=5,mode=620,ptmxmode=000 0 0 tmpfs /dev/shm tmpfs rw,relatime 0 0 /dev/mapper/VolGroup-lv_root / ext4 rw,relatime,barrier=1,data=ordered 0 0 /proc/bus/usb /proc/bus/usb usbfs rw,relatime 0 0 /dev/sda1 /boot ext4 rw,relatime,barrier=1,data=ordered 0 0 none /proc/sys/fs/binfmt_misc binfmt_misc rw,relatime 0 0 sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0 192.168.1.21:/data/server /data/client nfs4 rw,relatime,vers=4,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.11,minorversion=0,local_lock=none,addr=192.168.1.21 0 0
测试
1.在nfs服务器端, 进入/data/server共享目录, 建立test-nfs.txt文件
[root@localhost server]# pwd /data/server [root@localhost server]# touch test-nfs.txt
2.在nfs客户端,进入/data/client挂载目录,建立test-nfs222.txt文件
[root@localhost client]# pwd /data/client [root@localhost client]# touch test-nfs222.txt [root@localhost client]# ls test-nfs222.txt test-nfs.txt [root@localhost client]# ll total 0 -rw-r--r-- 1 nfsnobody nfsnobody 0 Mar 14 2019 test-nfs222.txt -rw-r--r-- 1 root root 0 Mar 14 2019 test-nfs.txt
test-nfs.txt 是在服务器端以root建立的,因此权限是root。
test-nfs222.txt 是在客户端以root建立的,可是文件的权限被压缩成nfsnobody用户了。
[root@localhost server]# cat /etc/exports # Shared /data/server directory to 192.168.1.11 /data/server 192.168.1.11(rw,sync) [root@localhost server]# showmount -e localhost Export list for localhost: /data/server 192.168.1.11 [root@localhost server]# cat /var/lib/nfs/etab /data/server 192.168.1.11(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)
从/var/lib/nfs/etab 这个文件中咱们能够看到, 在共享的时候即便咱们只指定了rw和rsync这两参数, nfs也会默认有不少参数。其中就有all_squash,这个参数在客户端的做用就是无论你用什么用户建立的文件,都会被强制压缩成nfsnobody。
咱们客户端挂载NFS也能够设置不少参数的,-o 后面的参数。
客户端挂载能够设置:不可执行、读写权限、断开后RPC呼叫方式、读写区块大小等。
通常来讲当nfs服务器提供的只是普通数据(图片html,css,jss,视频等)应该不须要执行suid,exec等权限,
因为是共享目录不存在设备因此也不存在挂载设备dev,所以在客户端挂载的时候,能够加上给你这几个命令挂载。
mount -t nfs -o nosuid,noexec,nodev,rw 192.168.1.21:/data/server /data/client
suid 容许设置suid
nosuid 不容许在共享文件系统中设置suid ,默认是suid
rw 读写权限
ro 只读
dev 保留设备文件的特殊功能
nodev 不保留(通常来讲只有/dev才会有特殊的设备文件,所以能够选在nodev),默认是dev
exec 容许执行任何二进制文件
noexec 不容许在共享文件系统中直接执行任何二进制文件,默认是exec
user 容许用户拥有文件的挂载与卸载的功能
nouser 不容许用户拥有文件的挂载与卸载功能(若是要保护文件系统,最好不要为用户提供挂载与卸载的功能),默认是nouser
auto 这个auto指的是"mount -a"时会不会自动挂载的项目,
noauto 不自动挂载,默认是auto
fg
bg
当执行挂载时,该挂载行为是在前台(fg)仍是在后台(bg)执行。若在前台执行,则mount会持续尝试连接,直到成功或time out为止。
若为在后台执行,则mount会在后台持续屡次进行mount,而不会影响到前台的程序操做。
若是网络联机不稳定,或是服务器经常须要开关机。建议使用bg比较稳当。默认为fg
soft
hard
使用挂载时会使用RPC呼叫。若是是hard的状况,那么当二者之间有任何一台主机离线,那RPC会持续呼叫,直到对方恢复联机为止。
而soft,只是在RPC time out后重复呼叫。而非持续呼叫。所以系统的延迟会不这么明显,若是服务器常常开开关关的话,建议使用soft。
在生产环境中推荐使用hard,intr这样的方式来挂载。默认为hard
intr
当使用hard方式挂载时,若加上intr参数,则RPC的持续呼叫是能够被中断的
rsize
wsize
读出(rsize)和写入(wsize)的区块大小。这个设置值能够影响客户端与服务器端传输数据的缓冲存储容量。
通常来讲,若是在局域网内(LAN),而且客户端与服务器都具备足够的内存,这个值能够设置大一点,好比说32768,提高缓冲区块将可提高NFS文件系统的传输能力。
但设置的值也不要太大,最好是实现网络可以传输的最大值为限。默认rsize=1024,wsize=1024
proto=udp
使用UDP协议来传输数据,在内网中会有较好的性能,可是在Internet的话,使用proto=tpc传输的数据会有较好的纠错能力。默认是proto=tcp
命令格式以下:
mount –t nfs –o nosuid,noexec,nodev,rw,hard,intr,rsize=32768,wsize=32768 192.168.1.21:/data/server /data/client
1) noexec,nosuid,nodev,由于共享存放的是简单资料,不须要suid位不须要执行,没有设备文件。
2) hard,intr,bg 当NFS连接断了以后会一直去监测服务端的NFS服务直到恢复以后从新链接。
3) rsize=32768 wsize=32768 调优NFS传输的区块大小。
4) 基本参数:rw 读写权限。
1.经过把 mount –t nfs 192.168.1.21:/data/server /data/client 这条命令写到/etc/rc.local中,让它开机就执行。
2.在/etc/fstab(系统开机启动分区加载项)添加咱们的NFS配置:
192.168.1.21:/data/serve /data/client nfs defaults 1 1
但在生产环境中,对于共享的NFS目录,通常不会配置到/etc/fstab里。
由于在客户端主机重启时若是因为网络等缘由链接不上nfs server时,就会致使客户机没法启动的厄运发生。
有两种方式实现开机自动挂载,但这里建议采用第一种,若是由于网络缘由没有链接到NFSserver那么第二中有可能会致使系统没法启动的故障。
若是卸载时提示:"umount.nfs: /data/client: device is busy",须要退出挂载目录再进行卸载,若是NFS 服务器宕机了,则须要强制卸载,可执行命令:umount -lf /data/client
强制卸载:umount –lf /data/client
在企业工做场景,通常来讲,NFS服务器共享的只是普通静态数据(图片、附件、视烦),不须要执行suid、exec等权限,
挂载的这个文件系统只能做为数据存取之用,没法执行程序,对于客户端来说增长了安全性,例如:不少***篡改站点文件都是由上传入口上传的程序到存储目录,而后执行的。
所以在挂载的时候,用下面的命令颇有必要
mount -t nfs -o nosuid, noexec ,node, rw 10.0.0.7:/data /mnt
经过 mount -o 指定挂载参数与在 /etc/fstab里指定挂载参数的效果是同样的。网络文件系统和本地的文件系统效果也是同样的。
下面介绍几个在企业生产环境下,NFS性能优化挂载的例子
1)禁止更新目录及文件时间戳挂载,命令以下:
mount -t nfs -o noatime,nodiratime 10.0.0.7:/data /mnt
2)安全加优化的挂载方式以下
mount -t nfs -o nosud,noexec,node,noatime,noduratime,intr,rsize=131072,wsize=131072 10.0.0.7:/data /mnt
3)默认的挂载方式以下
mount -t nfs 10.0.0.7:/data /mnt
WSIZE和RSIZE的大小最好是1024的倍数,对于NFSv2来讲,8192是RSIZE和 WSIZE的最大数值,若是使用的是NFSv3,则能够尝试设置32768,若是是NFSv4能够到65536或更大。
若是在客户端挂载时使用了这两个参数,可让客户端在读取和写入数据时,一次性读写更多的数据包,这能够提高访问性能和效率。
除此以外,还有 noatime,mxtiratimc 性能优化选项,
这两个选项是说在读写磁盘的时候不更新文件和目录的时间戮(即不更新文件系统中文件对应 inode 信息),这样就能够减小和磁盘系统的交互,提高读取和写入磁盘的效率,
由于磁盘是机械的,每次读写都会消耗磁盘I/O,而更新文件时间戳对于工做数据必要性不大,最大问题是增长了访问磁I/O的次数,拖慢系统性能。
如下是NFS网络文件系统优化挂载的参数建议:
在CentOS6 x86_64服努器端和客户端环境下,可以使用以下命令参数:
m
mount-t nf -o noatimc,nodiratime,nosuid,noexec,nodev,rsize=131072,wsize=131072 10.0.0.7:/data /mnt
通过实际测试,CentOS6 x86_64默认的挂载参数性能仍是不错的。
mount -t nfs 10.0.0.7:/data /mnt
注意:非性能的参数越多,速度可能会越慢。根据具体的业务须要以及实际测试效果选择挂载参数。
/proc/sys/net/core/rmem_default:该文件指定了接收套接宇缓冲区大小的默认值(以宇节为单位),默认设置:124928,
/proc/sys/net/core/rmem_max:该文件指定了接收套接字缓冲区大小的最大值(以宇节为单位),默认设置:124928,
/proc/sys/net/core/wmem_default:该文件指定了发送套接字绶冲区大小的默认值(以字节单位),默认设置:124928
/proc/sys/net/core/wmem_max:该文件指定了发送套接字簧沖区大小的最大值(以字节单位),默认设置:124928
硬件:sas/ssd磁盘,买多块, raid0/raid10。网卡吞吐量要大,至少干兆(多块bond)。
NFS服务端配置:/data 10.0.0.7(rw,sync,all_squash,anonuid=65534,anongid=65534)
NFS客户端挂载优化配置命令:
mount -t nfs -o nosuid,noexec,nodev,noatmme,nodiratime,rsize=131072,wsize=131072 10.0.0.7:/data /mnt #兼顾安全性能
一、简单容易掌握
二、方便快速部署简单维护容易
三、从软件层面上看,数据可靠性高,稳定性强。
一、局限性是存在单点故障,若是NFSserver宕机了全部客户端都不能访问共享目录,#####咱们能够经过rsync来进行数据的同步。或者是经过负载均衡的高可用方案。######
二、在高并发的场合,NFS效率性能有限(通常几千万如下pv的网站不是瓶颈,除非网站架构太差。)
三、服务器共享文件的客户端认证是基于IP和主机名的安全性通常(但用于内网则问题不大)
四、NFS数据是明文的,对数据完整性不作验证(通常是存放于内网,提供内网的服务器使用。因此安全性相对不是一个问题)
五、多机器挂载服务器时,链接管理维护麻烦。尤为NFS服务端出问题后,全部客户端都挂掉状态(可以使用autofs自动挂载解决。)
中小型网站(2000万pv如下)线上应用,都有用武之地。门户网站也会有其余方面的应用,
由于门户网站的并发量是超级的大。因此有更加会用专业的存储来作这件事情。
-------------------------------完结-----------------------------------