Web集群之NFS(网络文件系统)

栏目: 服务器 · 发布时间: 7年前

内容简介:所有的Web服务器会向 NFS服务器进行数据交互NFS适用于中小型企业:NFS服务器,而且默认只能有一台NFS服务器大公司或大门户(高并发):Moosefs(MFS)、GlusterFS、FastDFS等(分布式文件系统 )

1、什么是NFS

NFS(Network File System) 网络文件系统

它的主要功能是通过网络(一般是局域网)让不同主机系统之间共享文件或目录

NFS客户端(应用服务器,例如web)可以挂载(mount)的方式将NFS服务器端共享的数据目录挂载到NFS客户端本地系统中(某一个挂载点下)

从客户端本地来看,NFS服务器端共享的目录就好像是客户端自己的磁盘分区或目录一样,而实际上却是远端的NFS服务器的目录

所有的Web服务器会向 NFS服务器进行数据交互

应用场景:

NFS适用于中小型企业:NFS服务器,而且默认只能有一台NFS服务器

大公司或大门户(高并发):Moosefs(MFS)、GlusterFS、FastDFS等(分布式文件系统 )

NFS在传输数据时使用的端口会随机选择。那么就需要RPC

NFS软件列表:

nfs-utils  #NFS服务的主程序
rpcbind #RPC的主程序
安装:yum install  nfs-utils rpcbind -y  (服务端必须安装,客户端强烈建议安装)
检查:rpm -aq nfs-utils rpcbind

1.1 NFS工作原理流程图:

Web集群之NFS(网络文件系统)

NFS服务端,首先要先启动RPC服务,在启动NFS服务,NFS服务会自动向RPC服务注册启动的端口,客户端需要通过自身的RPC服务,来向NFS服务端的RPC获取NFS的端口号,然后客户端在通过得到的端口号,单独向NFS发起TCP建立。

file:///Users/xiongminghao/Library/Mobile%20Documents/iCloud~com~coderforart~iOS~MWeb/Documents/mweb_documents_library/docs/media/15413998107562/15414008422850.jpg Web集群之NFS(网络文件系统)

1.2 NFS共享目录

NFS共享的目录:必须是实际存在的目录,要用绝对路径,注意共享目录的本地权限,如果需要读写共享,要让本地目录可以被NFS客户端的用户(nfsnobody)读写

NFS客户端地址:为NFS服务端授权的可以访问共享目录的NFS客户端地址,可为单独的IP地址或主机名、域名等,也可以使整个网段地址,还可以用“*”来匹配所有客户端服务器,没有对账号密码,几乎都是用IP地址,这些所谓的客户端一般来说是前端的业务服务器,

权限参数:对授权的NFS客户端的访问权限

NFS服务端默认配置文件路径/etc/exports,并且内容默认为空

[root@web01 ~]# ls -ld /etc/exports
-rw-r--r--. 1 root root 0 Jun  7  2013 /etc/exports
[root@web01 ~]# cat /etc/exports

exports配置文件的规范(两种写法):

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

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

2、NFS搭建

查看系统版本

[root@web01 ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@web01 ~]# uname -r
3.10.0-862.el7.x86_64
[root@web01 ~]# uname -m
x86_64

检查并安装nfs和rpc服务

[root@nfs /]#yum install  nfs-utils rpcbind -y 
[root@nfs /]# rpm -aq nfs-utils rpcbind
rpcbind-0.2.0-44.el7.x86_64
nfs-utils-1.3.0-0.54.el7.x86_64

2.1 服务端配置

启动RPC

[root@nfs /]# systemctl start rpcbind
[root@nfs /]# systemctl status rpcbind #查看rpcbind是否正常运行
● rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2018-11-05 15:03:34 CST; 1min 0s ago
  Process: 52393 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 52394 (rpcbind)
   CGroup: /system.slice/rpcbind.service
           └─52394 /sbin/rpcbind -w

Nov 05 15:03:34 nfs systemd[1]: Starting RPC bind service...
Nov 05 15:03:34 nfs systemd[1]: Started RPC bind service.
[root@nfs /]# netstat -lntup |grep rpc 
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      52394/rpcbind       
tcp6       0      0 :::111                  :::*                    LISTEN      52394/rpcbind       
udp        0      0 0.0.0.0:841             0.0.0.0:*                           52394/rpcbind       
udp        0      0 0.0.0.0:111             0.0.0.0:*                           52394/rpcbind       
udp6       0      0 :::841                  :::*                                52394/rpcbind       
udp6       0      0 :::111                  :::*                                52394/rpcbind

[root@nfs /]#  lsof -i :111       #111为RPC程序端口号
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 52394  rpc    6u  IPv4 100989      0t0  UDP *:sunrpc 
rpcbind 52394  rpc    8u  IPv4 100991      0t0  TCP *:sunrpc (LISTEN)
rpcbind 52394  rpc    9u  IPv6 100992      0t0  UDP *:sunrpc 
rpcbind 52394  rpc   11u  IPv6 100994      0t0  TCP *:sunrpc (LISTEN)

启动NFS

[root@nfs /]# systemctl start nfs
[root@nfs /]# systemctl status nfs
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
   Active: active (exited) since Mon 2018-11-05 15:08:02 CST; 3s ago
  Process: 54371 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 54366 ExecStartPre=/bin/sh -c /bin/kill -HUP `cat /run/gssproxy.pid` (code=exited, status=0/SUCCESS)
  Process: 54365 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 54371 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service

Nov 05 15:08:02 nfs systemd[1]: Starting NFS server and services...
Nov 05 15:08:02 nfs systemd[1]: Started NFS server and services.

再次查看RPC端口

#当NFS启动后,NFS向rpc注册端口
[root@nfs /]# netstat -lntup |grep rpc
tcp        0      0 0.0.0.0:37998           0.0.0.0:*               LISTEN      54333/rpc.statd     
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      52394/rpcbind       
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      54363/rpc.mountd    
tcp6       0      0 :::33985                :::*                    LISTEN      54333/rpc.statd     
tcp6       0      0 :::111                  :::*                    LISTEN      52394/rpcbind       
tcp6       0      0 :::20048                :::*                    LISTEN      54363/rpc.mountd    
udp        0      0 0.0.0.0:841             0.0.0.0:*                           52394/rpcbind       
udp        0      0 0.0.0.0:20048           0.0.0.0:*                           54363/rpc.mountd    
udp        0      0 0.0.0.0:111             0.0.0.0:*                           52394/rpcbind       
udp        0      0 0.0.0.0:45174           0.0.0.0:*                           54333/rpc.statd     
udp        0      0 127.0.0.1:703           0.0.0.0:*                           54333/rpc.statd     
udp6       0      0 :::45876                :::*                                54333/rpc.statd     
udp6       0      0 :::841                  :::*                                52394/rpcbind       
udp6       0      0 :::20048                :::*                                54363/rpc.mountd    
udp6       0      0 :::111                  :::*                                52394/rpcbind

提示:

查看nfs服务向rpc注册的端口信息

rpcinfo -p localhost

NFS常见进程讲解

[root@nfs /]#  ps -ef|egrep "rpc|nfs"
rpc       52394      1  0 15:03 ?        00:00:00 /sbin/rpcbind -w
rpcuser   54333      1  0 15:08 ?        00:00:00 /usr/sbin/rpc.statd
root      54341      2  0 15:08 ?        00:00:00 [rpciod]
root      54363      1  0 15:08 ?        00:00:00 /usr/sbin/rpc.mountd
root      54364      1  0 15:08 ?        00:00:00 /usr/sbin/rpc.idmapd
root      54373      2  0 15:08 ?        00:00:00 [nfsd4_callbacks]
root      54379      2  0 15:08 ?        00:00:00 [nfsd]
root      54380      2  0 15:08 ?        00:00:00 [nfsd]
root      54381      2  0 15:08 ?        00:00:00 [nfsd]
root      54382      2  0 15:08 ?        00:00:00 [nfsd]
root      54383      2  0 15:08 ?        00:00:00 [nfsd]
root      54384      2  0 15:08 ?        00:00:00 [nfsd]
root      54385      2  0 15:08 ?        00:00:00 [nfsd]
root      54386      2  0 15:08 ?        00:00:00 [nfsd]

nfsd(rpc.nfsd) #主进程,主要是管理客户端能否登入服务端,登入者ID判别。

mountd(rpc.mountd) #管理NFS文件系统,登入者的权限管理

rpc.lockd(非必要) #用来锁定文件,用于客户端同时写入

rpc.statd(非必要) #检查文件一致性

rpc.idmapd #名字映射后台进程

将rpcbind和nfs加入开机自启动

systemctl enable rpcbind.service
systemctl enable nfs-server.service

配置共享/data目录(编辑/etc/exports配置文件)

[root@nfs ~]# cat >>/etc/exports<<EOF
> #shared /data by jason for bingbing at 2018-08-25
> /data 172.16.1.0/24(rw,sync)
> EOF

[root@nfs ~]# cat /etc/exports 
#shared /data by oldboy for bingbing at 2018-08-25
/data 172.16.1.0/24(rw,sync)

图解上述操作的意义:

Web集群之NFS(网络文件系统)

提示:

必须要在NFS和RPC启动之后修改/etx/export文件

创建共享目录(客户端访问)

#创建/data目录
[root@nfs /]# mkdir /data -p
[root@nfs /]# ls -ld /data/
drwxr-xr-x 2 root root 6 Nov  5 14:31 /data/
将共享目录加入到nfs的虚拟用户和用户组
[root@nfs /]# chown -R nfsnobody.nfsnobody /data/  

#nfsnobody是nfs的虚拟用户
[root@nfs /]# id nfsnobody
uid=65534(nfsnobody) gid=65534(nfsnobody) groups=65534(nfsnobody)

上述操作完毕后,平滑重启nfs

[root@nfs /]# systemctl reload nfs

使用nfs自带的showmount,测试nfs是否共享成功

#测试NFS目录是否共享成功
#在nfs端本地测试
[root@nfs /]# showmount -e localhost
Export list for localhost:
/data 172.16.1.0/24
#可以-e指定nfs端地址
[root@nfs /]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24

nfs端自己挂载自己的/data目录(可忽略)

[root@nfs /]# mount -t nfs 172.16.1.31:/data /mnt/
mount.nfs: /mnt is busy or already mounted
[root@nfs /]# df -h |grep mnt                     
172.16.1.31:/data         22G  1.7G   21G   8% /mnt #挂摘成功
[root@nfs /]# umount /mnt/ #测试完成后,记得卸载

提示:

测试nfs共享目录,使用showmount命令即可

umount /mnt -lf 可强制卸载

2.2 客户端配置

安装nfs和rpc(rpc必须安装,nfs需要使用到自带的一些工具,所以两者都装)

[root@web01 ~]# yum isntall nfs-utils rpcbind -y
[root@web01 ~]# rpm -qa nfs-utils rpcbind
nfs-utils-1.3.0-0.54.el7.x86_64
rpcbind-0.2.0-44.el7.x86_64

启动rpc,并加入开机自启动

[root@web01 ~]# systemctl start rpcbind #启动rpc
[root@web01 ~]# systemctl status rpcbind #查看rpc状态
● rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2018-11-05 15:39:55 CST; 4s ago
  Process: 17432 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 17433 (rpcbind)
   CGroup: /system.slice/rpcbind.service
           └─17433 /sbin/rpcbind -w

Nov 05 15:39:55 web01 systemd[1]: Starting RPC bind service...
Nov 05 15:39:55 web01 systemd[1]: Started RPC bind service.
[root@web01 ~]# systemctl enable rpcbind.service #开机自启动

测试挂载

[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24  #共享目录没问题
[root@web01 ~]# telnet 172.16.1.31 111 
Trying 172.16.1.31...
Connected to 172.16.1.31. #连接到对端rpc也没问题
Escape character is '^]'.

将客户端的/mnt目录挂载到nfs服务端

[root@web01 ~]# mount -t nfs 172.16.1.31:/data /mnt/ 
[root@web01 ~]# df -h |grep mnt
172.16.1.31:/data         22G  1.7G   21G   8% /mnt 
#挂载完成

在挂载目录/mnt下创建文件,然后在nfs服务端查看

#客户端创建文件
[root@web01 ~]# cd /mnt/
[root@web01 mnt]# touch test.txt
[root@web01 mnt]# ll
-rw-r--r-- 1 nfsnobody nfsnobody 0 Nov  5 16:05 test.txt
#服务端查看
[root@nfs /]# cd /data/
[root@nfs data]# ll
-rw-r--r-- 1 nfsnobody nfsnobody 0 Nov  5 16:05 test.txt
注:当客户端访问/nmt的时候,相当于是在访问172.16.1.31这个NFS服务端的/data目录

实现开机自动挂载

[root@nfs /]# echo "mount -t nfs 172.16.1.31:/data /mnt/" >>/etc/rc.local 
[root@nfs /]# tail -1 /etc/rc.local 
mount -t nfs 172.16.1.31:/data /mnt/

提示:

也可以放入/etc/fatab,需要注意一些情况

NFS网络文件系统最好不要放到fstab里实现开机挂载,

但如果在开机自启动服务里设置并启动了netfs服务,放入fastb里也是可

以开机挂载的


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

PHP for the World Wide Web, Second Edition (Visual QuickStart Gu

PHP for the World Wide Web, Second Edition (Visual QuickStart Gu

Larry Ullman / Peachpit Press / 2004-02-02 / USD 29.99

So you know HTML, even JavaScript, but the idea of learning an actual programming language like PHP terrifies you? Well, stop quaking and get going with this easy task-based guide! Aimed at beginning ......一起来看看 《PHP for the World Wide Web, Second Edition (Visual QuickStart Gu》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具