Loading

NFS网络存储

简介

NFS是Network File System的缩写及网络文件系统。NFS主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录。
NFS系统和Windows网络共享、网络驱动器类似, 只不过windows用于局域网, NFS用于企业集群架构中, 如果是大型网站, 会用到更复杂的分布式文件系统FastDFS,glusterfs,HDFS,ceph。

应用

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

NFS工作流程图

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

image
image

NFS部署

  • 服务端

    1.安装NFS和rpcbind
    [root@nfs ~]# yum install nfs-utils rpcbind -y
    
    2.创建挂载点
    # 根下创建
    [root@nfs ~]# mkdir -p /web/nfs{1..9}
    
    3.配置挂载点
    [root@nfs ~]# vim /etc/exports
    /etc/exports文件配置格式:[挂载点] [可以访问的IP]([权限])
    /web/nfs1  172.16.1.0/20(rw,sync,all_squash)
    
    4.关闭selinux和防火墙
    [root@nfs ~]# setenforce 0
    [root@nfs ~]# systemctl disable --now firewalld
    
    5.启动Nfs和rpcbind服务
    [root@nfs ~]# systemctl start nfs-server
    [root@nfs ~]# systemctl start rpcbind
    
    6.检查服务端是否正常
    # 格式: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
    
    7.给挂载点授权
    [root@nfs ~]# chown -R nfsnobody.nfsnobody /web
    
  • 客户端

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

测试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-server 和 rpcbind 服务,客户端需要卸载挂载和重新挂载

    • rw,读写权限 (常用)

      # NFS部署就是rw的案例
      [root@nfs ~]# vim /etc/exports
      /web/nfs1  172.16.1.0/20(rw,sync,all_squash)
      
      image
    • 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
      # 这样就创建不了,系统提示只读
      

    控制文件权限案例

    • 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为匿名用户
      
    • 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
      
    • 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
    
    2.使用anonuid,anongid统一用户和组id
    [root@nfs nfs1]# vim /etc/exports
    /web/nfs1  172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)
    
    3.修改挂载点
    [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
    
    4.重启服务
    # 服务端重启服务
    [root@nfs nfs1]# systemctl restart nfs-server
    [root@nfs nfs1]# systemctl restart rpcbind
    
    5.客户端验证
    # 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用户
    

搭建考试系统

统一用户的实际案例

  • 搭建步骤

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

    • 客户端安装Web软件
      # 客户端下载
      [root@web01 opt]# yum install httpd php php-devel -y
      
    • 将代码放置于网站的根目录
      [root@web01 opt]# cd /var/www/html/
      
    • 上传代码文件
      # 将文件解压
      [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
      
    • 授权
      [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
      
    • 关闭selinux和防火墙
      [root@web01 html]# setenforce 0
      setenforce: SELinux is disabled
      [root@web01 html]# systemctl disable --now firewalld
      
    • 修改web软件的用户
      [root@web01 html]# vim /etc/httpd/conf/httpd.conf
      将User apache 和 Group apache 改为 User www 和 Group www
      # 注.不然不会同步文件,出错!!
      
    • 启动web软件
      [root@web01 html]# systemctl start httpd
      
      image
    • 创建存放上传文件目录(忘记写了,可以在修改用户主和组前创建!)
      [root@web01 html]# mkdir upload
      [root@web01 html]# chown www.www upload
      # 重启服务
      [root@web01 html]# systemctl restart httpd
      
    • 测试
      image
      # 从考试系统上传图片,验证是否上传到upload目录下,并且用 http://客户端ip/upload/文件名 访问到文件
      [root@web01 upload]# ll
      total 204
      -rw-r--r-- 1 www www 205464 Dec 30 18:22 2_dog.jpg
      
      image
  • 配合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)
    
    2.创建挂载点 并且 统一用户(授权)
    [root@nfs nfs1]# mkdir /web/upload
    [root@nfs nfs1]# chown www.www /web/upload
    
    3.重启NFS和rpcbind服务
    [root@nfs nfs1]# systemctl restart nfs-server rpcbind
    
    4.所有客户端安装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
    
    5.所有客户端挂载
    [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
    
    6.测试,上传狗狗图片!用一台客户端主机能看到其他客户端主机的狗狗图片~
    

    image
    image
    image

posted @ 2022-10-07 12:20  爱learn  阅读(93)  评论(0编辑  收藏  举报