NFS 部署

部署NFS实现多主机文件共享,Web01、Web02、Web03做示例客户端,实现功能如下:


NFS简介

NFS是Network File System的缩写及网络文件系统。NFS主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录。

NFS系统和Windows网络共享、网络驱动器类似, 只不过windows用于局域网, NFS用于企业集群架构中, 如果是大型网站, 会用到更复杂的分布式文件系统FastDFS,glusterfs,HDFS,ceph。

NFS应用

  • 用户访问NFS客户端,将请求转化为函数;
  • NFS通过TCP/IP连接服务端;
  • NFS服务端接收请求,会先调用portmap进程进行端口映射
  • Rpc.nfsd进程用于判断NFS客户端能否连接服务端;
  • Rpc.mount进程用于判断客户端对服务端的操作权限;
  • 如果通过权限验证,可以对服务端进行操作,修改或读取;

NFS工作流程图

这里的服务端是存文件的,服务端里没有安全认证但是有权限认证,客户端查找文件先从本地找,如果没有去服务端,从服务端找到返回到客户端~(portmap不需要安装,NFS自带了)


NFS部署

服务端

  1. 安装NFS和rpcbind
[root@nfs ~]# yum install nfs-utils rpcbind -y
  1. 创建挂载点
# 根下创建
[root@nfs ~]# mkdir -p /web/nfs{1..9}
  1. 配置挂载点
[root@nfs ~]# vim /etc/exports

/etc/exports文件配置格式:
[挂载点] [可以访问的IP]([权限])
/web/nfs1 172.16.1.0/20(rw,sync,all_squash)
  1. 关闭selinux和防火墙
[root@nfs ~]# setenforce 0
[root@nfs ~]# systemctl disable --now firewalld
  1. 启动Nfs和rpcbind服务
[root@nfs ~]# systemctl start nfs-server
[root@nfs ~]# systemctl start rpcbind
  1. 检查服务端是否正常
# 格式:showmount -e [服务端的地址,默认是本机地址]
[root@nfs ~]# showmount -e
Export list for nfs:
/web/nfs1 172.16.1.0/20 # 可以跟着ip地址
[root@nfs ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/web/nfs1 172.16.1.0/20 [root@nfs ~]# cat /var/lib/nfs/etab
  1. 给挂载点授权
[root@nfs ~]# chown -R nfsnobody.nfsnobody /web

客户端

  1. 安装NFS
[root@web01 opt]# yum install -y nfs-utils
  1. 创建目录
[root@web01 opt]# mkdir /opt/nfs/
  1. 挂载NFS
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1  /opt/nfs/
  1. 测试
# 在web01中、在/opt/nfs/目录下创建文件,到NFS服务端/web/nfs1/目录下查看是否同步
[root@web01 opt]# touch /opt/nfs/test{1..9}.txt
  1. 最终实现网络同步存储!

web02和web03客户端同样的操作,使用同样的挂载点,最终实现网络同步存储

测试NFS文件同步功能

NFS配置详解

nfs共享参数 参数作用
rw 读写权限 (常用)
ro 只读权限 (不常用)
root_squash 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户 (不常用)
no_root_squash 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员 (不常用)
all_squash 无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户 (常用)
no_all_squash 无论NFS客户端使用什么账户访问,都不进行压缩 (不常用)
sync 同时将数据写入到内存与硬盘中,保证不丢失数据 (常用)
async 优先将数据保存到内存,然后再写入硬盘;这样效率更高,但可能会丢失数据 (不常用)
anonuid 配置all_squash使用,指定NFS的用户UID,必须存在系统 (常用)
anongid 配置all_squash使用,指定NFS的用户GID,必须存在系统 (常用)

NFS部分参数案例

所有的参数都是在NFS服务端中的文件/etc/exports中修改,修改完成服务端(NFS)需要重启nfs-serverrpcbind 服务,客户端需要卸载挂载重新挂载

  1. rw,读写权限 (常用)
# NFS部署就是rw的案例
[root@nfs ~]# vim /etc/exports
/web/nfs1 172.16.1.0/20(rw,sync,all_squash)

  1. ro,只读权限 (不常用)
# NFS服务端的操作
# 修改配置文件
[root@nfs nfs1]# vim /etc/exports
/web/nfs1 172.16.1.0/20(ro,sync,all_squash)
# 重启服务
[root@nfs nfs1]# systemctl restart nfs-server
[root@nfs nfs1]# systemctl restart rpcbind # web01客户端的操作
# 查看挂载
[root@web01 nfs]# df -h
172.16.1.31:/web/nfs1 20G 3.1G 17G 16% /opt/nfs
# 卸载挂载,如果在nfs目录下卸载会报错:umount.nfs4: /opt/nfs: device is busy
[root@web01 /]# umount /opt/nfs/ # 再次挂载
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/
[root@web01 opt]# touch /opt/nfs/test.txt
touch: cannot touch ‘/opt/nfs/test.txt’: Read-only file system
# 这样就创建不了,系统提示只读

控制文件权限案例

  1. root_squash,当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户 (不常用)
# 修改服务端配置文件参数
[root@nfs nfs1]# vim /etc/exports
/web/nfs1 172.16.1.0/20(rw,sync,root_squash)
# 服务端重启服务
[root@nfs nfs1]# systemctl restart nfs-server
[root@nfs nfs1]# systemctl restart rpcbind # 客户端卸载和挂载
[root@web01 /]# umount /opt/nfs/
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/
# 创建文件查看是否为匿名用户
[root@web01 opt]# touch nfs/test.txt
[root@web01 nfs]# ll /opt/nfs/
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 17:01 test.txt
# 验证成功nfsnobody为匿名用户
  1. no_root_squash,当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员 (不常用)
# 修改服务端配置文件参数
[root@nfs nfs1]# vim /etc/exports
/web/nfs1 172.16.1.0/20(rw,sync,no_root_squash)
# 服务端重启服务
[root@nfs nfs1]# systemctl restart nfs-server
[root@nfs nfs1]# systemctl restart rpcbind # 客户端卸载和挂载
[root@web01 /]# umount /opt/nfs/
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/
# 创建文件查看是否为root用户
[root@web01 opt]# touch nfs/10.txt
[root@web01 nfs]# ll /opt/nfs/
-rw-r--r-- 1 root root 0 Dec 30 17:07 10.txt
# 验证成功用户为root
  1. all_squash,无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户 (常用)
# 修改服务端配置文件参数
[root@nfs nfs1]# vim /etc/exports
/web/nfs1 172.16.1.0/20(rw,sync,all_squash)
# 服务端重启服务
[root@nfs nfs1]# systemctl restart nfs-server
[root@nfs nfs1]# systemctl restart rpcbind # 客户端使用普通用户验证
[root@web01 /]# useradd hammer
[root@web01 nfs]# cat /etc/passwd
hammer:x:1000:1000::/home/hammer:/bin/bash
# 客户端卸载和挂载
[root@web01 /]# umount /opt/nfs/
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/
# 普通用户创建文件查看是否为匿名用户
[hammer@web01 nfs]$ touch 11.txt
[hammer@web01 nfs]$ ll
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Dec 30 17:18 11.txt
# 验证成功,普通用户创建文件也是匿名用户

统一用户

解决NFS如果取消文件权限,客户端用户不能操作的问题,使用统一用户(其实是固定统一用户id)解决

  1. 所有服务端和客户端都添加用户和用户组
# NFS和web01-03中创建www用户和用户组
[root@nfs nfs1]# groupadd www -g 666
[root@nfs nfs1]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin
  1. 使用anonuid,anongid统一用户和组id
[root@nfs nfs1]# vim /etc/exports
/web/nfs1 172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)
  1. 修改挂载点
[root@nfs nfs1]# chown -R www.www /web/

# 查看
[root@nfs web]# ll
total 0
drwxr-xr-x 2 www www 320 Dec 30 17:18 nfs1
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs2
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs3
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs4
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs5
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs6
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs7
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs8
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs9
  1. 重启服务
# 服务端重启服务
[root@nfs nfs1]# systemctl restart nfs-server
[root@nfs nfs1]# systemctl restart rpcbind
  1. 客户端验证
# web01中,分别用root用户和hammer用户验证创建文件所属用户和用户组是谁
# 普通用户验证
[hammer@web01 nfs]$ touch 13.txt
[hammer@web01 nfs]$ ll
-rw-rw-r-- 1 www www 0 Dec 30 17:37 13.txt # root用户验证
[root@web01 nfs]# touch 12.txt
[root@web01 nfs]# ll
-rw-r--r-- 1 www www 0 Dec 30 17:36 12.txt # 验证成功,结果都为www用户

搭建考试系统

统一用户的实际案例

搭建步骤

所有客户端都操作如下步骤

  1. 客户端安装Web软件
# 客户端下载
[root@web01 opt]# yum install httpd php php-devel -y
  1. 将代码放置于网站的根目录
[root@web01 opt]# cd /var/www/html/
  1. 上传代码文件

    代码文件,想练习的留言发给你,文件我是用xftp传输

# 将文件解压
[root@web01 html]# ll
total 28
-rw-r--r-- 1 root root 26995 Dec 30 17:47 kaoshi.zip
[root@web01 html]# unzip kaoshi.zip
Archive: kaoshi.zip
inflating: info.php
inflating: bg.jpg
inflating: index.html
inflating: upload_file.php
[root@web01 html]# ll
total 80
-rw-r--r-- 1 root root 38772 Apr 27 2018 bg.jpg
-rw-r--r-- 1 root root 2633 May 4 2018 index.html
-rw-r--r-- 1 root root 52 May 10 2018 info.php
-rw-r--r-- 1 root root 26995 Dec 30 17:47 kaoshi.zip
-rw-r--r-- 1 root root 1192 Jan 10 2020 upload_file.php
  1. 授权
[root@web01 html]# chown -R www.www /var/www/html
[root@web01 html]# ll
total 80
-rw-r--r-- 1 www www 38772 Apr 27 2018 bg.jpg
-rw-r--r-- 1 www www 2633 May 4 2018 index.html
-rw-r--r-- 1 www www 52 May 10 2018 info.php
-rw-r--r-- 1 www www 26995 Dec 30 17:47 kaoshi.zip
-rw-r--r-- 1 www www 1192 Jan 10 2020 upload_file.php
  1. 关闭selinux和防火墙
[root@web01 html]# setenforce 0
setenforce: SELinux is disabled
[root@web01 html]# systemctl disable --now firewalld
  1. 修改web软件的用户
[root@web01 html]# vim /etc/httpd/conf/httpd.conf
将User apache 和 Group apache 改为 User www 和 Group www
# 注.不然不会同步文件,出错!!
  1. 启动web软件
[root@web01 html]# systemctl start httpd

访问成功!


  1. 创建存放上传文件目录(忘记写了,可以在修改用户主和组前创建!)
[root@web01 html]# mkdir upload
[root@web01 html]# chown www.www upload
# 重启服务
[root@web01 html]# systemctl restart httpd
  1. 测试

下载二哈图片

# 从考试系统上传图片,验证是否上传到upload目录下,并且用 http://客户端ip/upload/文件名 访问到文件
[root@web01 upload]# ll
total 204
-rw-r--r-- 1 www www 205464 Dec 30 18:22 2_dog.jpg

验证成功!!!

配合NFS实现文件共享

所有客户端搭建完NFS,可以在自己的所有客户端上传验证文件,我分别在web01,web02和web03上传了二哈,吉娃娃和杜宾图片,用来验证

在服务端搭建NFS,实现多主机文件共享,通过一台客户端就能看到所有的狗狗帅照!!!

  1. 修改NFS配置文件
[root@nfs nfs1]# vim /etc/exports
/web/upload 172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)
  1. 创建挂载点 并且 统一用户(授权)
[root@nfs nfs1]# mkdir /web/upload
[root@nfs nfs1]# chown www.www /web/upload
  1. 重启NFS和rpcbind服务
[root@nfs nfs1]# systemctl restart nfs-server rpcbind
  1. 所有客户端安装NFS软件
[root@web01 html]# yum install nfs-utils -y
[root@web02 html]# yum install nfs-utils -y
[root@web03 html]# yum install nfs-utils -y
  1. 所有客户端挂载
[root@web01 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web02 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web03 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
  1. 测试,上传狗狗图片!用一台客户端主机能看到其他客户端主机的狗狗图片~


三台客户端主机上传文件成功,客户端之间实现同步共享!

网页验证结果如下

NFS 部署的更多相关文章

  1. nfs部署和优化

    nfs--网络文件系统 1.说明:允许一个系统在网络上与他人共享目录和文件 2.好处:通过nfs服务,就可以让这个机器访问远程的文件,像访问自己的文件一样,属于cs通信   3.原理说明:假设有A,B ...

  2. kubernetes(14):k8s基于NFS部署storageclass实现pv自动供给

    k8s基于NFS部署storageclass实现pv自动供给 https://www.cnblogs.com/Smbands/p/11059843.html https://www.jianshu.c ...

  3. NFS部署文件共享

    本章解了如何配置网络文件系统(Network File System,NFS)服务来简化Linux系统之间的文件共享工作,以及通过部署NFS服务在多台Linux系统之间挂载并使用资源.在管理设备挂载信 ...

  4. Linux下 nfs部署

    一. 挂载一个硬盘来分享 二. 更改配置文件 三. 在配置文件中设置属性 四.     另一台机器   配置的虚拟机,将nfs关闭  配置文件也删除内容       挂载  挂载到部署nfs的极其 之 ...

  5. k8s 基于NFS部署storageclass pv自动供给

    在k8s中部署有状态应用时,通常需要做数据持久化存储. 后端存储的方式有以下几种: 1.基于宿主机本地的存储方式: (重启pod时,若pod被调度到其他节点上,尽管原来节点上的数据不会丢失,但是其他节 ...

  6. NFS部署教程

    NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源.在NFS的应用中,本地NFS的客户端应用可 ...

  7. 部署和调优 1.1 nfs部署和优化-2

    更改共享目录文件默认的所有者和所属组 已知道客户端有个user11用户 cat /etc/passwd user11:x:501:501::/home/user11:/bin/bash 服务端打开 v ...

  8. 部署和调优 1.1 nfs部署和优化-1

    NFS服务会经常用到,用于在网络上共享存储.举一个例子来说明一下 NFS .假如有三台机器 A.B.C,它们需要访问同一个目录,目录中都是图片,传统的做法是把这些图片分别放到 A.B.C.但是,若使用 ...

  9. nfs部署和优化 -2

    客户端: cat /etc/passwd 显示用户 weifeng 500   服务端: vim /etc/exports /mnt 192.168.1.105(rw,sync,all_squash, ...

随机推荐

  1. 安全相关,CSRF

    先说下CSRF的定义 跨站请求伪造(英语:Cross-site request forgery),也被称为 one-click attack 或者 session riding,通常缩写为 CSRF ...

  2. ORACLE中dual用法详解

    基本上oracle引入dual为的就是符合语法1. 我们先从名称来说,dual不是缩写词,本身就是完整的单词.dual名词意思是对数,做形容词时是指二重的,二元的.2. Oracle中的dual表是一 ...

  3. 随录、EJB和JTA

    说道JTA(Java Transction Api),即事务的一种. 事务:说白了就是一组原子操作,是为了保证数据的安全性. 它,分为三类:JDBC事务,JTA事务,还有容器事务. JDBC是由Con ...

  4. RecyclerView实现侧滑删除、置顶、滑动

    1.首先在build.gradle里添加 compile 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.2.1' 2.设置recyclerView的item布 ...

  5. 一、手把手教你docker搭建fastDFS文件上传下载服务器

    在搭建fastDFS文件上传下载服务器之前,你需要准备的有一个可连接的linux服务器,并且该linux服务器上已经安装了docker,若还有没安装docker的,先百度自行安装docker. 1.执 ...

  6. MySQL 用户权限相关命令

    ##1.创建用户: create user test identified by '123456';##identified后面跟密码 ##2.查询所有用户: select user from mys ...

  7. 关于ssh-keygen 生成的key以“BEGIN OPENSSH PRIVATE KEY”开头

    现在使用命令 ssh-keygen -t rsa  生成ssh,默认是以新的格式生成,id_rsa的第一行变成了"BEGIN OPENSSH PRIVATE KEY" 而不在是&q ...

  8. linux基础-TCP/IP协议篇

    一.网络TCP/IP层次模型 1.网络层次模型概念介绍:TCP/IP协议就是用于简化OSI层次,以及相关的标准.传输控制协议(tcp/ip)族是相关国防部(DoD)所创建的,主要用来确保数据的完整性及 ...

  9. spring的注解AOP配置

    package com.hope.service.impl;import com.hope.service.IAccountService;import org.aspectj.lang.annota ...

  10. odoo views中html的奇怪问题

    在我创建了字段类型为 fields.Html 以后,确出现了两种不同的情况 下图中,content是此类型的,可以正常显示不需要加widget(小部件)="html" <fo ...