Linux——网络文件系统之nfs


网络文件系统之nfs

nfs -------- Network File System 网络文件系统

作用:在Linux服务器间实现数据共享

在这里插入图片描述

当客户端访问,无论是左边的服务器还是右边的服务器,想要访问同样的页面,同样的内容,这就意味着一个网站做好了,一个网站上,有几百个文件,各存储一份;
问题我们的网站是刚刚搭建的,网页上的文件,以及代码经常跟新,为了保证这两个服务器上有相同的页面,把有变化的文件都要往服务器上重新放,是一个比较繁琐的过程,有什么方法可以减轻这个繁琐的过程;

解决方法:
为了解决这个问题一般的架构是,在两个web服务器上 不存储任何的页面文件,单独找一台机器,把网页文件,存储到这台机器上,对于前端的两台服务器来说,想要页面来找我要,我负责提供页面,后端的这台服务器对于前端的两台服务器来说,这台服务器是共享存储,即在跟新文件直接在后端的共享存储上跟新,web服务期想要页面,直接到后端的存储上去取,能实现共享存储的机制有nfs

nfs只适用于小型的网站架构,有10台到20台之间;
nfs是怎么实现的:
在这里插入图片描述

把网站的网页存放到nfs的目录下,如/wabdata,通过nfs机制把这个目录共享出去,允许前端的服务器使用我的网站上的页面,想要访问后端的nfs服务器,那么前端服务器怎么联系我?挂载;
对于前端的服务器来说,它想要使用我服务器上的文件,把nfs服务器上的某个目录(/webdata),挂载到前端服务器的某一个目录上(/web);
一旦挂载成功了,把nfs服务器上的目录,映射到前端服务器的目录上;
前端服务器做的事情是:
我nfs服务器的目录/webdata挂载到本地的目录上,如/web如果挂载成功,就会把nfs的也买你映射到/web目录下,相当于本地有了这个文件;
即:
在这里插入图片描述

nfs只是一个文件系统,这个文件系统仅仅是提供了存储数据和共享数据的功能;
前端服务器和nfs服务器之间要经行数据的传输,但是nfs服务器本身,它没有提供数据传输的功能;
nfs提供数据传输功能是借助linux中的一种机制,这种机制称之为rpc(远程过程调用),rpc机制是由rpcbind的软件提供的;

软件:

	nfs-utils
	rpcbind 

在Linux系统中自带了这两个软件,查看:

[root@file-server ~]# rpm -q rpcbind
rpcbind-0.2.0-32.el7.x86_64

[root@file-server ~]# rpm -q nfs-utils
nfs-utils-1.3.0-0.21.el7.x86_64
[root@file-server ~]# 

启动服务:
[root@node04 ~]# systemctl start nfs-server
[root@node04 ~]# systemctl start rpcbind

设置开机自启:
[root@node04 ~]# systemctl enable nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@node04 ~]# systemctl enable rpcbind

配置文件目录:--- /etc/exports
文件的格式:

目录名称			客户端地址(权限)
目录代表:把那个目录共享出去
把这些目录共享出去以后,允许哪些客户端使用我;即为客户端的IP地址;
使用时候的权限




1客户端地址的写法:
	IP地址	192.168.1.1      可以写成单个的IP地址;即代表当nfs共享一个目录,只有这个IP能使用我这个目录里面的文件;
	网段		192.168.1.0/24      也可以写某一个网段;只要是这个网段,都能使用我这个网段;
2权限(权限可以写多个用逗号隔开):
		ro		只读 :代表只能查看,修改的操作只能在nfs服务器上,不能在前端服务器上;
		rw		读写:可以读也可以改
		sync	同步
		async	异步  效率高
		
		以下3个是配合rw使用:
		all_squash		客户端所有用户上传的文件的所属均为nfsnobody,这些文件的属主,属组;在我nfs服务器上面
		这些文件的属主,属组分别映射成了匿名用户,nfsnobody;

		root_squash		客户端root用户上传的文件的所属会被映射为nfsnobody
		no_root_squash	客户端root用户上传的文件的所属仍为root 
		
		anonuid=<number>  跟用户的uid
		anongid=<number>  跟用户的gid

解析:
1:all_squash
首先:nfs服务器上做的是,rw读写的功能;也就意味着前端服务器能往我nfs服务器上存数据,
其中在前端服务器上可以用普通用户登录,也可以拿root用户登录;
all_squash:代表前端用户无论是以普通用户还是以root用户上传的,在nfs服务器上查看这个文件的属主属组的时候,
属主属组都是nfsnobody;
2:root_squash
代表:客户端以管理员工的身份上传的文件,这些文件的属主属组都会被映射成nfsnobody;
3:no_root_squash代表前端服务器以root管理员工上传的文件,属主属组任然是root;
4:需求假设前端服务器上传的文件不管是以普通用户u上传的还是以管理员用户上传的,其中在nfs服务器上这些文件属主属组被映射成mysql,mysql,则可以使用
anonuid= 跟用户的uid
anongid= 跟用户的gid
来完成这个功能;
在这里插入图片描述

示例: 通过nfs共享本地目录/webdata, 允许192.168.122.121以只读方式挂载 ;

在这里插入图片描述

nfs服务器:

[root@file-server ~]# mkdir /webdata
[root@file-server ~]# touch /webdata/{1..10}.html

[root@file-server ~]# cat /etc/exports
/webdata	192.168.122.121(ro)

重新加载配置文件:
exports -rav


[root@file-server ~]# systemctl start rpcbind
[root@file-server ~]# systemctl start nfs-server	
[root@file-server ~]# systemctl enable nfs-server
[root@file-server ~]# systemctl enable rpcbind

怎们验证我们搭建这个服务成功了那:
这条命令的作用是:用来查看我本机的nfs服务器共享了那个目录,以及允许那个客户端使用;
[root@file-server ~]# showmount -e localhost
Export list for localhost:
/webdata 192.168.122.121

前端服务器:

在前端服务器上,怎么使用我的nfs服务器,用挂载的方式;
创建目录
mkdir /web
把nfs的共享文件/webapp,挂载到前端服务器的/web目录上;(手动挂载)
[root@client ~]# mount 192.168.122.105:/webdata /web/


查看
[root@client ~]# ls /web
10.html  1.html  2.html  3.html  4.html  5.html  6.html  7.html  8.html  9.html

永久挂载:

 vim /etc/fstab 

192.168.122.105:/webdata        /www    nfs     defaults        0 0

mount -a

在挂载的时候报错
在这里插入图片描述

分析:
根据错误提示,查看/sbin/mount.文件,果然发现没有/sbin/mount.nfs的文件,安装nfs-utils即可
解决:
在这里插入图片描述
安装之后,/sbin/下面多了两个mount文件,分别是mount.nfs和mount.nfs4:
在这里插入图片描述
当使用mount -a时还会哦出现问题:

[root@localhost ~]# mount -a
mount.nfs: No route to host

解决办法:
查看selinux是否关闭,如果没有则关闭:

[root@node04 webdata]# getenforce 
Enforcing

关闭:
[root@node04 webdata]# setenforce 0
永久关闭
vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.

还是报错

使用 mount -t nfs 127.0.0.1:/home/lzgonline/rootfs /mnt 和 mount -t nfs 192.168.1.9:/home/lzgonline/rootfs /mnt 本机挂载nfs则没有问题,然而使用 mount -t nfs 192.168.3.12:/home/lzgonline/rootfs /mnt 时却出现了问题,导致开发板无法通过nfs挂载启动,其中192.128.3.12 和 192.128.1.9(即nfs服务器)之间建立了映射(DMZ)关系。
mount.nfs: access denied by server while mounting 192.168.3.12:/home/lzgonline/rootfs
百度、谷歌了很久,大部分都说是权限设置有问题,其实文件夹权限都设为777了,权限上都没问题,hosts.deny和hosts.allow都保留默认设置,防火墙也关了,该设置的都设置了,但还是被拒绝,很是郁闷,就在一筹莫展的时候,通过查看一些linux技术论坛后逐渐找到了问题所在。
首先使用命令查看出错日志文件
[root@lzgonline init.d]# cat /var/log/messages | grep mount
Jun 29 00:49:04 lzgonline mountd[1644]: refused mount request from 192.168.3.12 for /home/lzgonline/rootfs (/home/lzgonline/rootfs): illegal port 1689
Jun 29 00:51:02 lzgonline mountd[1644]: refused mount request from 192.168.3.12 for /home/lzgonline/rootfs (/home/lzgonline/rootfs): illegal port 1710
Jun 29 01:02:17 lzgonline mountd[1644]: refused mount request from 192.168.3.12 for /home/lzgonline/rootfs (/home/lzgonline/rootfs): illegal port 1916
Jun 29 01:09:51 lzgonline mountd[1644]: refused mount request from 192.168.3.12 for /home/lzgonline/rootfs (/home/lzgonline/rootfs): illegal port 2157
Jun 29 01:17:02 lzgonline mountd[1644]: refused mount request from 192.168.3.12 for /home/lzgonline/rootfs (/home/lzgonline/rootfs): illegal port 2318
 
从出错日志可以看出,mount.nfs: access denied by server while mounting 192.168.3.12:/home/lzgonline/rootfs 被拒绝的原因是因为使用了非法端口,功夫总没白费,终于在一个linux技术论坛上找到了答案:
I googled and found that since the port is over 1024 I needed to add the "insecure" option to the relevant line in /etc/exports on the server. Once I did that (and ran exportfs -r), the mount -a on the client worked.

//如果端口号大于1024,则需要将 insecure 选项加入到配置文件(/etc/exports)相关选项中mount客户端才能正常工作:

查看 exports 手册中关于 secure 选项说明也发现确实如此

[root@lzgonline init.d]# man exports

secure,This  option requires that requests originate on an Internet port less than IPPORT_RESERVED (1024). This option is on by default. To turn it off, specify insecure.

//secure 选项要求mount客户端请求源端口小于1024(然而在使用 NAT 网络地址转换时端口一般总是大于1024的),默认情况下是开启这个选项的,如果要禁止这个选项,则使用 insecure 标识

修改配置文件/etc/exports,加入 insecure 选项

/home/lzgonline/rootfs  *(insecure,rw,async,no_root_squash)

保存退出

然后重启nfs服务:service nfs restart

问题解决了

有志者,事竟成,破釜沉舟,百二秦关终属楚; 苦心人,天不负,卧薪尝胆,三千越甲可吞吴。 想到与得到中间还有两个字——做到。
原文地址:https://www.cnblogs.com/huoxc/p/14029905.html