首页 存档 技术 查看内容

Linux网络文件系统NFS NFS介绍 NFS服务端部署环境准备 NFS SERVER端设置 客户端配置 ...

2018-3-30 13:00 |来自: 互联网 347 0

摘要: NFS介绍 什么是NFS? NFS是Network File System的缩写,即网络文件系统。它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录。NFS客户端(一般为应用服务器,例如web)可以通过挂载(mo ...


NFS介绍

什么是NFS?

NFS是Network File System的缩写,即网络文件系统。它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录。NFS客户端(一般为应用服务器,例如web)可以通过挂载(mount)的方式将NFS服务端共享的数据目录挂载到NFS客户端本地系统中(就是某一个挂载点下)。从NFS客户端的机器本地看,NFS服务端共享的目录就好像是客户自己的磁盘分区或者目录一样,而实际上确是远端的NFS服务端的目录。

NFS网络文件系统类似windows系统的网络共享、安全功能、网络驱动器映射,这也和linux系统里的samba服务类似。应用于互联网中小型集群架构后端作为数据共享,如果是大型网站,那么有可能还会用到更复杂的分布式文件系统,例如Moosefs(mfs)、glusterfs、FastDFS。

NFS在企业中的应用场景

在企业集群架构的工作场景中,NFS网络文件系统一般被用来存储共享视频、图片、附件等静态资源文件。一般是把网站用户上传的文件都放在NFS共享里,例如,BBS产品的图片、附件、头像,注意网站BBS程序不要放在NFS共享里,然后前端所有的节点访问存储服务之一,特别是中小网站公司应用频率更高。

NFS流程图

NFS原理图

NFS的RPC服务,在CentOS5.x下名称为portmap,在CentOS6.x下名称为rpcbind。

NFS服务端部署环境准备

NFS服务部署服务器准备

服务器系统

角色

ip

CentOS 6.6 x86_64

NFS服务端(nfs-server)

自定义

CentOS 6.6 x86_64

NFS客户端(nfs-client1)

自定义

CentOS 6.6 x86_64

NFS客户端(nfs-client2)

自定义

NFS SERVER端设置

NFS软件列表

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

nfs-utils:这个NFS服务主程序包括rpc.nfsd、rpc.mountd两个daemons和相关文档说明及执行命令文件等。

rpcbind:CentOS 6.x下面RPC的主程序(CentOS 5.x下面的portmap)

NFS可以被视为一个RPC程序,在启动任何一个RPC程序之前,需要做好端口映射工作,这个映射工作就是由rpcbind服务来完成的。因此,必须先启动rpcbind服务。

CentOS 6.6默认没有安装NFS软件包(CentOS 5默认已经安装)。

  1. [root@web-lnmp01 ~]# yum install nfs-utils rpcbind -y

  2. [root@web-lamp01 ~]# rpm -qa nfs-utils rpcbind

  3. [root@web-lnmp01 ~]# rpm -qa nfs-utils rpcbind

  4. nfs-utils-1.2.3-70.el6_8.2.x86_64 #这个包在5.8为portmap

  5. rpcbind-0.2.0-12.el6.x86_64

出现两个软件包,表示NFS服务端软件安装完毕。

启动NFS相关服务

  1. [root@nfs-server ~]# /etc/init.d/rpcbind status #查看rpcbind服务

  2. rpcbindisstopped

  3. [root@nfs-server ~]# /etc/init.d/rpcbind start #启动rpcbind服务

  4. Starting rpcbind: [ OK ]

  5. [root@nfs-server ~]# /etc/init.d/rpcbind status

  6. rpcbind (pid 26101)isrunning...

  7. [root@nfs-server ~]# lsof -i:111 #rpcbind服务端口111

  8. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

  9. rpcbind 26101 rpc 6u IPv4 28829 0t0 UDP *:sunrpc

  10. rpcbind 26101 rpc 8u IPv4 28832 0t0 TCP *:sunrpc (LISTEN)

  11. rpcbind 26101 rpc 9u IPv6 28834 0t0 UDP *:sunrpc

  12. rpcbind 26101 rpc 11u IPv6 28837 0t0 TCP *:sunrpc (LISTEN)

  13. [root@nfs-server ~]# netstat -lntup|grep rpcbind

  14. tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 26101/rpcbind

  15. tcp 0 0 :::111 :::* LISTEN 26101/rpcbind

  16. udp 0 0 0.0.0.0:836 0.0.0.0:* 26101/rpcbind

  17. udp 0 0 0.0.0.0:111 0.0.0.0:* 26101/rpcbind

  18. udp 0 0 :::836 :::* 26101/rpcbind

  19. udp 0 0 :::111 :::* 26101/rpcbind

  20. [root@nfs-server ~]# chkconfig --list|grep rpcbind

  21. rpcbind 0:off 1:off 2:on 3:on 4:on 5:on 6:off

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

  23. program vers proto port service

  24. 100000 4 tcp 111 portmapper

  25. 100000 3 tcp 111 portmapper

  26. 100000 2 tcp 111 portmapper

  27. 100000 4 udp 111 portmapper

  28. 100000 3 udp 111 portmapper

  29. 100000 2 udp 111 portmapper

  30. [root@nfs-server ~]# /etc/init.d/nfs status #查看nfs服务

  31. rpc.svcgssdisstopped

  32. rpc.mountdisstopped

  33. nfsdisstopped

  34. rpc.rquotadisstopped

  35. [root@nfs-server ~]# /etc/init.d/nfs start #启动nfs服务

  36. Starting NFS services: [ OK ]

  37. Starting NFS quotas: [ OK ]

  38. Starting NFS mountd: [ OK ]

  39. Starting NFS daemon: [ OK ]

  40. Starting RPC idmapd: [ OK ]

  41. [root@nfs-server ~]# /etc/init.d/nfs status

  42. rpc.svcgssdisstopped

  43. rpc.mountd (pid 26225)isrunning...

  44. nfsd (pid 26241 26240 26239 26238 26237 26236 26235 26234)isrunning...

  45. rpc.rquotad (pid 26220)isrunning...

  46. [root@nfs-server ~]# netstat -lntup|grep 2049 #nfs服务端口2049

  47. tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN -

  48. tcp 0 0 :::2049 :::* LISTEN -

  49. udp 0 0 0.0.0.0:2049 0.0.0.0:* -

  50. udp 0 0 :::2049 :::* -

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

  52. program vers proto port service

  53. 100000 4 tcp 111 portmapper

  54. 100000 3 tcp 111 portmapper

  55. 100000 2 tcp 111 portmapper

  56. 100000 4 udp 111 portmapper

  57. 100000 3 udp 111 portmapper

  58. 100000 2 udp 111 portmapper

  59. 100011 1 udp 875 rquotad

  60. 100011 2 udp 875 rquotad

  61. 100011 1 tcp 875 rquotad

  62. 100011 2 tcp 875 rquotad

  63. 100005 1 udp 56844 mountd

  64. 100005 1 tcp 49326 mountd

  65. 100005 2 udp 47287 mountd

  66. 100005 2 tcp 47817 mountd

  67. 100005 3 udp 56317 mountd

  68. 100005 3 tcp 47741 mountd

  69. 100003 2 tcp 2049 nfs

  70. 100003 3 tcp 2049 nfs

  71. 100003 4 tcp 2049 nfs

  72. 100227 2 tcp 2049 nfs_acl

  73. 100227 3 tcp 2049 nfs_acl

  74. 100003 2 udp 2049 nfs

  75. 100003 3 udp 2049 nfs

  76. 100003 4 udp 2049 nfs

  77. 100227 2 udp 2049 nfs_acl

  78. 100227 3 udp 2049 nfs_acl

  79. 100021 1 udp 45085 nlockmgr

  80. 100021 3 udp 45085 nlockmgr

  81. 100021 4 udp 45085 nlockmgr

  82. 100021 1 tcp 42582 nlockmgr

  83. 100021 3 tcp 42582 nlockmgr

  84. 100021 4 tcp 42582 nlockmgr

  85. [root@nfs-server ~]# chkconfig --list nfs

  86. nfs 0:off 1:off 2:off 3:off 4:off 5:off 6:off

  87. [root@nfs-server ~]# chkconfig nfs on #nfs添加开机自启动

  88. [root@nfs-server ~]# chkconfig --list nfs

  89. nfs 0:off 1:off 2:on 3:on 4:on 5:on 6:off

  90. [root@nfs-server ~]# less /etc/init.d/rpcbind

  91. #! /bin/sh

  92. #

  93. # rpcbind Start/Stop RPCbind

  94. #

  95. # chkconfig: 2345 13 87 #开机启动顺序

  96. [root@nfs-server ~]# less /etc/init.d/nfs

  97. #!/bin/sh

  98. #

  99. # nfs This shell script takes care of starting and stopping

  100. # the NFS services.

  101. #

  102. # chkconfig: - 30 60 #开机启动顺序

工作中推荐使用/etc/rc.local进行服务的启动,不推荐使用chkconfig,目的是为了方便管理。

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

  2. rpc 26101 1 0 09:45 ? 00:00:00 rpcbind

  3. root 26211 2 0 09:52 ? 00:00:00 [rpciod/0]

  4. root 26220 1 0 09:52 ? 00:00:00 rpc.rquotad #磁盘配额进程

  5. root 26225 1 0 09:52 ? 00:00:00 rpc.mountd #权限管理验证

  6. root 26232 2 0 09:52 ? 00:00:00 [nfsd4]

  7. root 26233 2 0 09:52 ? 00:00:00 [nfsd4_callbacks]

  8. root 26234 2 0 09:52 ? 00:00:00 [nfsd] #NFS主进程

  9. root 26235 2 0 09:52 ? 00:00:00 [nfsd]

  10. root 26236 2 0 09:52 ? 00:00:00 [nfsd]

  11. root 26237 2 0 09:52 ? 00:00:00 [nfsd]

  12. root 26238 2 0 09:52 ? 00:00:00 [nfsd]

  13. root 26239 2 0 09:52 ? 00:00:00 [nfsd]

  14. root 26240 2 0 09:52 ? 00:00:00 [nfsd]

  15. root 26241 2 0 09:52 ? 00:00:00 [nfsd]

  16. root 26268 1 0 09:52 ? 00:00:00 rpc.idmapd

  17. root 26418 1023 0 10:41 pts/0 00:00:00 egrep rpc|nfs

配置NFS服务端

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

  1. [root@nfs-server ~]# ls -l /etc/exports

  2. -rw-r--r--. 1 root root 0 Jan 12 2010 /etc/exports

exports配置文件格式

/etc/exports文件配置格式

NFS 共享目录NFS客户端地址1(参数1,参数2,参数3…) 客户端地址1(参数1,参数2,参数3…)

NFS 共享目录NFS客户端地址(参数1,参数2…)

  1. [root@nfs-server ~]# cat /etc/exports

  2. /data 192.168.31.*(rw,sync)

  3. [root@nfs-server ~]# /etc/init.d/nfs reload

  4. exportfs: Failed to stat /data: No such file or directory #/data目录不存在

  5. [root@nfs-server ~]# mkdir /data

  6. [root@nfs-server ~]# /etc/init.d/nfs reload #exports -r

  1. [root@nfs-server ~]# showmount -e 127.0.0.1

  2. Export listfor127.0.0.1:

  3. /data 192.168.31.* #共享目录

  1. [root@nfs-server ~]# mount -t nfs 192.168.31.129:/data /mnt #挂载

  2. [root@nfs-server ~]# df -h

  3. Filesystem Size Used Avail Use% Mounted on

  4. /dev/sda3 7.1G 1.5G 5.3G 21% /

  5. tmpfs 497M 0 497M 0% /dev/shm

  6. /dev/sda1 190M 27M 153M 15% /boot

  7. 192.168.31.129:/data 7.1G 1.5G 5.3G 21% /mnt #挂载成功

  1. [root@nfs-server ~]# touch /data/oldboy.txt

  2. [root@nfs-server ~]# ls /mnt

  3. oldboy.txt

  4. [root@nfs-server ~]# touch /mnt/test.txt

  5. touch: cannot touch `/mnt/test.txt': Permission denied #无权限

客户端配置

启动rpcbind

  1. [root@web-lamp01 ~]# /etc/init.d/rpcbind start

  2. Starting rpcbind: [ OK ]

  3. [root@web-lamp01 ~]# /etc/init.d/rpcbind status

  4. rpcbind (pid 26272)isrunning...

  5. [root@web-lamp01 ~]# cat /etc/rc.local

  6. #启动rpcbind

  7. /etc/init.d/rpcbind start

  1. [root@web-lamp01 ~]# showmount -e 192.168.31.129

  2. Export listfor192.168.31.129:

  3. /data 192.168.31.*

  4. [root@web-lamp01 ~]# mount -t nfs 192.168.31.129:/data /mnt

  5. [root@web-lamp01 ~]# df -h

  6. Filesystem Size Used Avail Use% Mounted on

  7. /dev/sda3 7.1G 1.5G 5.3G 21% /

  8. tmpfs 497M 0 497M 0% /dev/shm

  9. /dev/sda1 190M 27M 153M 16% /boot

  10. 192.168.31.129:/data 7.1G 1.5G 5.3G 21% /mnt

  11. [root@web-lamp01 ~]# ls /mnt

  12. oldboy.txt

在服务端删除/data中的文件/oldboy.txt,然后在客户端进行查看,文件已经不存在。

无法写入是因为权限的问题,解决办法如下。

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

  2. /data 192.168.31.*(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,rw,root_squash,no_all_squash) #nfs的由65534进行管理

  3. [root@nfs-server ~]# cat /etc/exports

  4. /data 192.168.31.*(rw,sync)

  5. [root@nfs-server ~]# ls -ld /data

  6. drwxr-xr-x 2 root root 4096 Jan 19 11:36 /data #/data的属主是root

  7. [root@nfs-server ~]# grep 65534 /etc/passwd #查看65534的用户名

  8. nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

  9. [root@nfs-server ~]# chown -R nfsnobody /data #更改/data属主

  10. [root@nfs-server ~]# ls -ld /data

  11. drwxr-xr-x 2 nfsnobody root 4096 Jan 19 11:36 /data

  1. [root@nfs-server ~]# cd /mnt

  2. [root@nfs-server mnt]# pwd

  3. /mnt

  4. [root@nfs-server mnt]# ls

  5. [root@nfs-server mnt]# touch test.txt #服务端创建

  6. [root@nfs-server mnt]# ls

  7. test.txt

  8. [root@web-lamp01 mnt]# touch test2.txt #客户端创建

  9. [root@web-lamp01 mnt]# ls

  10. test2.txt test.txt

客户端设置开机自动挂载

  1. [root@web-lamp01 mnt]# cat /etc/rc.local

  2. #!/bin/sh

  3. #

  4. # This script will be executed *after* all the other init scripts.

  5. # You can put your own initialization stuffinhereifyou don't

  6. # want todothe full Sys V style init stuff.

  7. touch /var/lock/subsys/local

  8. #启动rpcbind

  9. /etc/init.d/rpcbind start

  10. #挂载nfs /data

  11. /bin/mount -t nfs 192.168.31.129:/data /mnt

同样配置另外一台客户端。

故障排查

1、首先确认NFS服务端配置和服务是否正常。

  1. [root@nfs-server ~]# showmount -e localhost

  2. Export listforlocalhost:

  3. /data 192.168.31.*

最好服务端自己挂载自己看看是否成功。

  1. [root@nfs-server ~]# mount -t nfs 192.168.31.129:/data /mnt

  2. [root@nfs-server ~]# df -h

  3. Filesystem Size Used Avail Use% Mounted on

  4. /dev/sda3 7.1G 1.4G 5.4G 21% /

  5. tmpfs 497M 0 497M 0% /dev/shm

  6. /dev/sda1 190M 27M 153M 15% /boot

  7. 192.168.31.129:/data 7.1G 1.4G 5.4G 21% /mnt

这一步主要是检查服务端的NFS服务是不是正常的。

2、确认NFS客户端showmount是否正常。

  1. [root@web-lamp01 ~]# showmount -e 192.168.31.129

  2. Export listfor192.168.31.129:

  3. /data 192.168.31.*

如果有问题进行下面排查。

1)ping NFS服务端IP检查

  1. [root@web-lamp01 ~]# ping 192.168.31.129

  2. PING 192.168.31.129 (192.168.31.129) 56(84) bytes of data.

  3. 64 bytes from 192.168.31.129: icmp_seq=1 ttl=64 time=1.40 ms

  4. 64 bytes from 192.168.31.129: icmp_seq=2 ttl=64 time=0.392 ms

2)telnet NFS服务端IP端口检查

  1. [root@web-lamp01 ~]# telnet 192.168.31.129 111

  2. Trying 192.168.31.129...

  3. Connected to 192.168.31.129.

  4. Escape characteris'^]'.

如果出现No route to host很有可能是服务端防火墙引起。

此外,还需要注意服务端RPC服务启动顺序问题。

/etc/init.d/rpcbind start

rpcinfo -p localhost

/etc/init.d/nfs start

rpcinfo -p localhost

NFS配置参数权限

NFS配置文件权限参数说明

NFS服务器端的权限设置,即/etc/exports文件配置格式中小括号的参数。

  1. [root@nfs-server ~]# cat /etc/exports

  2. /data 192.168.31.*(rw,sync)

参数名称

参数用途

rw

read-write,表示可读写权限*

ro

read-only,表示只读权限

sync

请求或写入数据时,数据同步写入到NFS Server的硬盘后才返回。数据安全不会丢,缺点,性能下降。

async

请求或写入数据是,先返回请求,再将数据写入到内存缓存和硬盘中,即异步写入数据。此参数可以提升NFS性能,但是会降低数据的安全。因此,一般情况下建议不用,如果NFS处于瓶颈状态,并且运行数据丢失的话可以打开此参数提升NFS性能。写入时数据会先写到内存缓冲区,等硬盘有空档再写入磁盘,这样可以提升写入效率,风险若服务器宕机或不正常关机,会损失缓冲区中未写入磁盘的数据(解决办法:服务器主板电池或加UPS不间断电源)。(电商秒杀是异步)

no_root_squash

访问NFS Server共享目录的用户如果是root的话,它对该共享目录具有root权限。这个配置原本为无盘客户端准备的。用户应避免使用。

root_squash

对于访问NFS Server共享目录的用户如果是root的话,则它的权限将被压缩成匿名用户,同时它的UID和GID通常会变成nfsnobody账号身份

all_squash

不管访问NFS Server共享目录的用户身份如何,它的权限都被压缩成匿名用户,同时它的UID和GID都会变成nfsnobody账号身份。在早期多个NFS客户端同时读写NFS Server数据时,这个参数很有用。*

anonuid

声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

相关分类