NFS (Network File System)

NFS (Network File System)

1. Introduction About NFS

1.1NFS scenarios in the enterprise

In scenes work in the enterprise cluster architecture, NFS network file sharing systems are generally used to store video, images, attachments and other static resource files, usually the site's users to upload files will be placed in the NFS share, for example: picture BBS products, accessories when head (note that the web BBS program do not put in an NFS share), and then access these static resources all the front-end node reads the storage resources on NFS. NFS is one of the current Internet architecture is the most popular data storage service, as I said before, a higher frequency of small and medium website application company, a large company or portal in addition to using NFS, but may also use more complex distributed file system such moosefs (mfs), GlusterFS, FastDFS etc.

image_1cjiackol1lcg18241tj6dip1t079.png-165.3kB

In the production cluster architecture, NFS as a shared storage for all front-end Web services, content storage generally include site users to upload images, attachments, avatars, etc. Note that the site of the program code do not put NFS shared, because the site program is to develop operation and maintenance personnel unified release, there is no release delays, batch publish directly to the Web node provides access to NFS share higher than in access efficiency.

Why 1.2 production cluster requires shared storage role.

Here reason storage services by way of illustration to show you the following clustering architecture needs to be shared. For example: A user Web1 upload images to the server, and then let the user B to access this image, the result of user requests access B distributed to Web2, because there is no picture on this Web2, which led to it A user can not see the uploaded images, If at this time there is a shared memory, a request users to upload pictures to either distribute the Web1 or Web2, will eventually be stored on shared storage, and when the user accesses B picture, regardless of the request distributed to Web1 or Web2, eventually We will go on looking for shared storage, so you can access to the resources needed. This shared storage location through commercial open source software and hardware, Internet SME cluster architecture will configure NFS network file system implementation with ordinary PC server.


When no clusters and NFS shared storage, where access to the user's image as shown in FIG.

image_1cjiaprnp1jo3628lv48l6ns1m.png-383.3kB

The figure is a schematic diagram of the production cluster is not NFS shared storage access. The figure is the production clusters have NFS shared storage case

image_1cjiaria5emvsuk6j13u6ag13.png-483.3kB

Workflow principle of 1.3NFS

image_1cjieokpehehm4r1jhsp4mt9g9.png-381.9kB
image_1cjiepnu7d4r657k1q18h135em.png-670kB

When accessing the program to access the NFS file server via the NFS client which requests data process is as follows:

  • 首先用户访问网站程序,由程序在NFS客户端上发出存取NFS文件的请求,这时NFS客户端(即执行程序的服务器)的RPC服务(rpcbind服务)就会通过网络向NFS服务器端的RPC服务(rpcbind服务)的111端口发出NFS文件存取功能的询问请求。
  • NFS服务端的RPC服务(rpcbind服务)找到对应的已注册的NFS端口后,通知NFS客户端的RPC服务(rpcbind服务)
  • 此时NFS客户端获取到正确的端口,并与NFS daemon联机存取数据
  • NFS客户端把数据存取成功后,返回给前端访问程序,告知给用户存取结果,作为网站用户,就完成了一次存取操作。
  • 因为NFS的各项功能都需要向RPC服务(rpcbind服务)注册,所以只有RPC服务(rpcbind服务)才能获取到NFS服务的各项功能对应的端口号(port number),PID,NFS在主机所监听的IP等信息,而NFS客户端也只能通过向RPC服务(rpcbind服务)询问才能找到正确的端口。也就是说,NFS需要有RPC服务(rpcbind服务)的协助才能成功对外提供服务。从上面的描述,我们不难推断,无论是NFS客户端还是NFS服务器端,当要使用NFS时,都需要首先启动RPC服务(rpcbind服务),NFS服务必须在RPC服务启动之后启动,客户端无需启动NFS服务,但需要启动RPC服务。

注意: NFS的RPC服务,在CentOS5.X下名称为portmap,在CentOS6.x下名称为rpcbind

1.4要部署NFS服务,需要安装下面的软件包:

  • nfs-utils: NFS服务的主程序,包括rpc.nfsd,rpc.mountd这两个daemons和相关文档说明,以及执行命令文件等。
  • rpcbind: CentOS6.X下面RPC的主程序。NFS可以视为一个RPC程序,在启动任何一个RPC程序之前,需要做好端口和功能的对应映射工作,这个映射工作就是由rpcbind服务来完成的。因此,在提供NFS服务之前必须先启动rpcbind服务才行。

1.5NFS参数

image_1cjitflu9lg91qts4711s91ds29.png-61kB

1.6exports配置文件相关参数应用领域的详细解释 (NFS精华重点)

1.6.1(rw,sync) :可读可写,同步传输

1.6.2(ro,async):只读,异步传输。

详细说明:

  • rw或者ro,主要控制的是所有客户端用户(包含root)的读写权限。如果设置成ro,就算root也只有读权限。它是NFS权限设置的第一道总闸阀门。
  • sync:同步传输,实时进行。
  • async:异步传输:攒一会在传输。

1.6.3root_squash:将root账户在共享目录里的身份降低为匿名者(默认nfsnobody)身份

1.6.4no_root_squash:不降低root账户在共享目录的身份,身份还是root

1.6.5all_squash:将所有访问用户在共享目录里的身份都降低为匿名者(默认nfsnobody)身份

详细说明:

  • 匿名者身份默认情况下就是NFS服务器端的虚拟账户角色,也就是nfsnobody。这是最低的身份,所有NFS客户端共享目录的访问者都被附加了这个身份,这也就意味者,如果文件的属主属组是nfsnobody的话,所有访问者对该文件都拥有全部所有权。
  • 所谓身份并不是访问权限,而是用户在共享目录里创建的文件的属主和属组。
  • 一旦身份被降低那么在共享目录里创建的文件的属主和属组就是变成了默认情况下的nfsnobody。这也就意味着,权限系统对你所创建的文件不做任何保护(任何访问者都可以查看,修改,删除)

所谓root_squash:

  • 使用这个参数意味着root在共享目录里创建的任何文件都不受保护,任何人(所有用户)都可以读取,修改,删除)。
  • 而非root用户则不降低权限,在共享目录里创建的文件的属主和属组统一为nobody(身份隐藏了),这种情况下,所有普通用户之间只能互相查看文件,并不能任意修改和删除并且你还无法知道是谁创建的文件,每个普通用户只能修改或删除自己创建的文件。
  • root用户虽然被降低了身份,但是并没有降低他的管理者权限,也就是说它仍旧能对所有共享目录里的所有文件进行查看,修改,删除操作。
  • 如果这类参数默认为空的话,那么NFS将默认使用这个参数。

所谓no_root_squash:

  • 使用这个参数意味着不对root进行降低身份的操作,也就是说root在共享目录里创建的文件的属主属组仍旧为root(不能被普通用户修改和删除)。
  • 非root用户同root_squash一样,并不降低权限。

所谓all_squash:

  • 使用这个参数意味着对所有访问NFS共享目录的用户进行降低身份的操作。也就是说,所有用户只要在共享目录里创建文件,那么文件的属主属组就是默认情况下的nfsnobody。
  • 在这个模式下,任何nfs客户端的任何访问用户都可以对共享目录里的任何文件进行查看,修改,删除操作

1.6.6 anonuid和anongid:指定NFS虚拟账户的uid或gid

这两个参数主要用来修改NFS默认的虚拟账户nfsnobody。可以通过指定虚拟账户的uid和gid的方式修改默认的虚拟账户的账户名称和所属组。

2.NFS实际演练

服务端:

2.1由服务端客户端都安装nfs-utilsrpcbind两个安装包

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

2.2NFS服务端配置文件路径

NFS服务的默认配置文件路径为:/etc/exports,并且默认是空的

image_1cjijthmq17ju1dd8s1o1eg91ipa57.png-3.2kB

解析:

image_1cjik0afm1chn1qtek1ojqlqkn5k.png-6kB
(ro):只可看,不能写

2.3 启动rpcbind

image_1cjiipj7k1pigbur1cbn1m2c1qbp9.png-11.1kB

2.4查看rpcinfo信息

2.4.1NFS未启动状态下的rpcinfo

image_1cjij02ct1bk11hi8mb01nect5cm.png-15.4kB

2.4.2NFS启动状态下的rpcinfo

image_1cjij7idvffomi1om38r717l413.png-5.4kB

image_1cjij8rg19hupnp19fgjv1j621g.png-35.3kB
image_1cjijatn31v9k12i21eg4she7av1t.png-19.4kB

2.5查看网络文件是否可挂载

image_1cjikq2katvrccr1kss18ldrkc61.png-3.9kB

2.6创建共享目录/data,并更改其属主属组为nfsnobody(加R参数)

image_1cjil8kuu1s8i1vu71mi16lq14vn6e.png-5.7kB

注:nfsnobodyNFS的程序用户

image_1cjill58a124l11fusp41f6213bn78.png-2.9kB

2.7由客户端挂载

image_1cjim0bsdfi412081unu1e7tdhh7l.png-2kB

image_1cjim21k21p0a43ai292f27nv82.png-10kB

2.8如果挂载不上,就打开/etc/ssh/sshd_config

[root@yangwenbo /]# vim /etc/ssh/sshd_config

image_1cjin7bk9ekmlsf1icf4vm3p88s.png-9.9kB

image_1cjinahci14qa1ru5ksotlb6699.png-7.2kB

2.9服务端本地实验

image_1cjim7v3e1dot1sondlg1ok01a3o8f.png-5.5kB

客户端:

3.1首先必须装nfs-utilsrpcbind两个安装包

3.2挂载对方共享目录

image_1cjisi1a61kmlnhmu9l1b5g1er59m.png-2.4kB

3.3查看自家目录/tmp

image_1cjisjrkqathbed1i0qqhj1inma3.png-2.1kB

3.3本地实验上传数据

image_1cjisloplal61r1k241bja1rv2ag.png-3.5kB

服务端:

由服务端检查本地的两个目录

image_1cjisoi8nnun3er1s3n9qv9nqbd.png-3.5kB

3.NFS企业级优化

3.1 NFS配置文件优化

问题:
(1)NFS客户端挂载后,往共享目录写入数据时卡住了
(2)NFS服务端,重启restart服务,客户端如果写入数据卡住了。
解答:
1.nfs服务端重启之后,共享文件夹进入grace time(无敌时间)
2.客户端在服务端重启后写入数据大概要等90秒
3.nfs配置文件:/etc/sysconfig/nfs

image_1cjl7b3p01ecrqttb26htob9t4t.png-37.9kB

3.2NFS客户端mount挂载深入

在NFS服务端可以通过cat /var/lib/nfs/etab 查看服务端配置参数的细节。在NFS客户端可以通过cat /proc/mounts查看mount的挂载参数细节。

3.2.1 mount挂载说明

通过如下命令在NFS客户端测试挂载获取的默认挂载参数:

>

NFS Client mount 挂载参数列表

image_1cjl7fdbj1num1862h9158026b6n.png-46.5kB

3.2.2mount -o参数对应的选项:

|参数|参数意义|系统默认值|
image_1cjl7h4tj225196d1o2s1f6t68t7k.png-52kB

3.3NFS 客户端mount挂载优化

提问:在企业生产环境中,NFS客户端挂载有没有必须要加的参数,比如,加noexec,nosuid,nodev,bg,soft,rsize,wsize等参数。
解答:这个问题属于mount挂载优化内容(有些参数也适合其他文件系统),一般来说要适当加挂载参数,但是,最好是先做好测试,用数据来说话,才能更好的确定到底是挂载还是不挂载。

3.3.1有关系统安全挂载参数选项

在企业工作场景,一般来说,NFS服务器共享的只是普通静态数据(图片,附件,视频),不需要执行suid,exec等权限,挂载的这个文件系统只能作为数据存取之用,无法执行程序,对于客户端来讲增加了安全性,例如:很多木马篡改站点文件都是由上传入口上传的程序到存储目录,然后执行的。
因此在挂载的时候,用下面的命令很有必要:mount -t nfs -o nosuid,noexec,nodev,rw 172.16.1.31:/data /mnt

3.3.2mount挂载性能优化参数选项

下面介绍几个在企业生产环境下,NFS性能优化挂载的例子

3.3.2.1禁止更新目录及文件时间戳挂载,命令如下:
mount -t nfs -o noatime,nodiratime 172.16.1.31:/data /mnt
3.3.2.2安全加优化的挂载方式如下:
mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,intr,rsize=131072,wsize=131072 172.16.1.31:/data /mnt
3.3.2.3默认的挂载方式如下:
mount -t nfs 172.16.1.31:/data /mnt
3.3.2.4如果是本地文件系统,使用如下命令:
mount /dev/sdb1 /mnt -o defaults,async,noatime,data=writeback,barrier=0

注意:如果本地文件系统挂载时,如果加入nodiratime会报错

3.3.3NFS网络文件系统优化挂载的参数建议

3.3.3.1在CentOS6.5 6.6 6.8等服务器端和客户端环境下,可使用如下命令参数:
mount -t nfs -o noatime,nodiratime,nosuid,noexec,nodev,rsize=131072 172.16.1.31:/data /mnt
3.3.3.2经过实际测试,CentOS6.6 6.8默认的挂载参数性能还是不错的。
mount -t nfs 172.16.1.31:/data /mnt

注意:非性能的参数越多,速度可能会变慢

3.3.4 NFS内核优化建议

下面是优化选项说明:

  • /proc/sys/net/core/rmem_default:该文件指定了接收套接字缓冲区大小的默认值(以字节为单位),默认设置:124928 建议:8388608
  • /proc/sys/net/core/rmem_max:该文件指定了接收套接字缓冲区大小的最大值(以字节为单位) 建议:16777216
  • /proc/sys/net/core/wmem_default:该文件指定了发送套接字缓冲区大小的默认值(以字节为单位),默认设置:124928 建议:8388608
  • /proc/sys/net/core/wmem_max:该文件指定了发送套接字缓冲区大小的最大值(以字节为单位)。默认设置:124928. 建议:16777216

3.4 NFS系统应用的优缺点说明

NFS服务可以让不同的客户端挂载使用同一个共享目录,也就是将其作为共享存储使用,这样可以保证不同节点客户端数据的一致性,在集群架构环境中经常会用到。如果是windows和Linux混合环境的集群系统,可以用samba来实现。


优点:

  • 简单,容易上手,容易掌握
  • NFS 文件系统内数据是在文件系统之上的,即数据是能看得见的。
  • 部署快速,维护简单方便,且可控,满足需求的就是最好的。
  • 可靠,从软件层面上看,数据可靠性高,经久耐用。数据是在文件系统之上的。
  • 服务非常稳定

局限:

  • 存在单点故障,如果NFS Server宕机了,所有客户端都不能访问共享目录。这个需要负载均衡及高可用来弥补
  • 在大数据高并发的场合,NFS效率,性能有限(2千万/日以下PV(page view)的网站不是瓶颈,除非网站架构设计太差。)
  • 客户端认证是基于IP和主机名的,权限要根据ID识别,安全性一般(用于内网则问题不大)。
  • NFS数据是明文的,NFS本身不对数据完整性做验证。
  • 多台客户机器挂载一个NFS服务器时,连接管理维护麻烦(耦合度高)。尤其NFS服务端出问题后,所有NFS客户端都处于挂掉状态(测试环境可使用autofs自动挂载解决,正式环境可修复NFS服务或强制卸载)
  • 涉及了同步(实时等待)和异步(解耦)的概念,NFS服务端和客户端相对来说就是耦合度有些高。网站程序也是一样,尽量不要耦合度太高,系统及程序架构师的重要职责就是为程序及架构解耦,让网站的扩展性变得更好。

应用建议:大中小型网站(参考点2000万/日PV以下)线上应用,都有用武之地。门户站也会有应用,生产场景应该多把数据的访问往前推,即尽量把静态存储里的资源通过CDN或缓存服务器提供服务,如果没有缓存服务或架构不好,存储服务器数量再多也是扛不住压力的,而且用户体验会很差。

附录1 【nfs挂载加入fstab案例】

NFS客户端实现fstab开机自启动挂载

现象:在/etc/fstab中配置了nfs开机自动挂载,结果无法开机自动挂载nfs

Answer:
1.nfs client mount command put 在/etc/rc.localautomatically mount
2. boot from the start netfs service before you can achieve startup automatically mount nfs fstab file system (before loading the network will be loaded at boot linux / etc / fstab )

Appendix 2 fstab misuse leads not boot

2.1fstab file was incorrectly modified, resulting in an error in the boot linux when prompted to restore your system settings.

Error message occurs when the boot 2.1.1

image_1cjlac1h9a7giuum561ll0lsh9.png-55.6kB

2.1.2 root password is entered into the user interface

image_1cjladhllk4215et1q8r1uq1gjtm.png-14.2kB

2.1.3 Open vim / etc / fstab file, we found fstab file is read-only. That can only see can not be changed.

image_1cjlaeu0k10ed575bo2duacc13.png-61.5kB

2.1.4 Exit / etc / fstab. At the command line, type the command

mount -o remount,rw/ It means that the entire root directory is readable and writable rw way to re-mount sideremount

2.1.5 We then open the / etc / fstab will find not a read-only mode

image_1cjlai6f7r2ipgb1u461k0j97n1g.png-53.9kB

2.1.6 quickly modify fstab and then reboot the server.

2.2 CD rescue mode recovery (with a linux disc repair system)

2.2.1 adjust the boot bios set the compact disc, and then mount the CD

image_1cjlakmq2usu11tv1tk1179k2181t.png-12.2kB

2.2.2 reboot the system, enter the CD rescue mode

image_1cjlalh6o1i5c1iolvl91mjsckd2a.png-415.3kB

2.2.3 all the way round, without in network mode

image_1cjlamhhtb5urvj1np61asd1i372n.png-9.1kB

2.2.4 the way to enter, select the first

image_1cjlanl7r1r7o1v0gdjuqfa1kvn44.png-12.1kB

2.2.5 Entering this page

image_1cjlaoklm1u4qj6314he1fqff4n4h.png-6.5kB

2.2.6 enter the command

image_1cjlapbpk7tpsjv1tabjh610hu4u.png-64.4kB

Guess you like

Origin www.cnblogs.com/ywb123/p/11128977.html
Recommended