NFS网络存储,搭建考试系统,配合NFS实现文件共享


一、NFS简介

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

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

  FastDFS,glusterfs,HDFS,ceph

二、NFS应用

  1.用户访问NFS客户端,将请求转化为函数

  2.NFS通过TCP/IP连接服务端

  3.NFS服务端接收请求,会先调用portmap进程进行端口映射

  4.Rpc.nfsd进程用于判断NFS客户端能否连接服务端

  5.Rpc.mount进程用于判断客户端对服务端的操作权限

  6.如果通过权限验证,可以对服务端进行操作,修改或读取

 三、NFS实践

3.1、服务端

  1、安装NFS和rpcbind

    [root@nfs ~]# yum install nfs-utils rpcbind -y

  2、创建挂载点

    [root@nfs ~]# mkdir /web/nfs{1..9}

  3、配置挂载点

    [root@nfs ~]# vim /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、检查服务端是否正常

    [root@nfs ~]# showmount -e [服务端的地址,默认是本机地址]

[root@nfs ~]# showmount -e
Export list for nfs:
/web/nfs1 172.16.1.0/20
[root@nfs ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/web/nfs1 172.16.1.0/20

  7、验证NFS配置

[root@nfs ~]# cat /var/lib/nfs/etab
/web/nfs1    172.16.1.0/20(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)

  8、给挂载点授权

    [root@nfs ~]# chown -R nfsnobody.nfsnobody /web

[root@nfs ~]# chown -R nfsnobody.nfsnobody /web
[root@nfs ~]# ll /web/
total 0
drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 13:59 nfs1
drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 13:59 nfs2
drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 13:59 nfs3
drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 13:59 nfs4
drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 13:59 nfs5
drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 13:59 nfs6
drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 13:59 nfs7
drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 13:59 nfs8
drwxr-xr-x 2 nfsnobody nfsnobody 6 Dec 30 13:59 nfs9

3.2、客户端

  1、安装NFS

    [root@web01 opt]# yum install -y nfs-utils

    [root@web02 opt]# yum install -y nfs-utils

    [root@web03 opt]# yum install -y nfs-utils

  2、创建目录

    [root@web01 opt]# mkdir /opt/nfs/

    [root@web02 opt]# mkdir /opt/nfs/

    [root@web03 opt]# mkdir /opt/nfs/

  3、挂载NFS

    [root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/

    [root@web02 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/

    [root@web03 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/

  4、查看挂载

[root@web01 opt]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.5M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos-root   20G  2.7G   17G  14% /
/dev/sda1                509M  162M  347M  32% /boot
tmpfs                     98M     0   98M   0% /run/user/0
172.16.1.31:/web/nfs1     20G  2.7G   17G  14% /opt/nfs

  5、测试NFS文件同步功能

[root@web01 opt]# touch nfs/{1..9}.txt
[root@web01 opt]# ll nfs/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 1.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 2.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 3.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 4.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 5.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 6.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 7.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 8.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 9.txt
[root@nfs ~]# ll /web/nfs1/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 1.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 2.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 3.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 4.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 5.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 6.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 7.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 8.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 9.txt

四、NFS配置详解

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

 参数使用:

# no_root_squash参数
[root@nfs ~]# vim /etc/exports
[root@nfs ~]# cat /etc/exports
/web/nfs1  172.16.1.0/20(rw,sync,no_root_squash)
# 重新启动服务
[root@nfs ~]# systemctl restart nfs-server rpcbind
[root@web01 opt]# umount /opt/nfs/
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/
# 创建文件同步成功
[root@web01 nfs]# touch 10.txt
[root@web01 nfs]# ll
total 0
-rw-r--r-- 1 root      root      0 Dec 30 16:02 10.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 1.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 2.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 3.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 4.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 5.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 6.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 7.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 8.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:26 9.txt

  统一用户:(所有客户端web01、web02、web03,服务端nfs统一创建用户www,uid=666,gid=666)

    1、创建用户(web01、web02、web03,nfs)

      [root@web01 opt]# groupadd www -g 666

      [root@web01 opt]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin

    2、修改服务端权限

      [root@nfs ~]# vim /etc/exports

      [root@nfs nfs1]# cat /etc/exports

      /web/nfs1 172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)

      [root@nfs ~]# systemctl restart nfs-server rpcbind  # 修改完后重启服务

    3、修改挂载点权限(nfs)

      [root@nfs nfs1]# chown -R www.www /web/

    4、使用

[root@web01 opt]# umount /opt/nfs/
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/
[root@web01 opt]# touch nfs/11.txt
[root@web01 opt]# ll nfs/
total 0
-rw-r--r-- 1 www www 0 Dec 30 16:02 10.txt
-rw-r--r-- 1 www www 0 Dec 30 16:29 11.txt
-rw-r--r-- 1 www www 0 Dec 30 15:26 1.txt
-rw-r--r-- 1 www www 0 Dec 30 15:26 2.txt
-rw-r--r-- 1 www www 0 Dec 30 15:26 3.txt
-rw-r--r-- 1 www www 0 Dec 30 15:26 4.txt
-rw-r--r-- 1 www www 0 Dec 30 15:26 5.txt
-rw-r--r-- 1 www www 0 Dec 30 15:26 6.txt
-rw-r--r-- 1 www www 0 Dec 30 15:26 7.txt
-rw-r--r-- 1 www www 0 Dec 30 15:26 8.txt
-rw-r--r-- 1 www www 0 Dec 30 15:26 9.txt

五、搭建考试系统

5.1、搭建web服务

  1、安装web软件

    [root@web01 html]# yum install httpd php php-devel -y

  2、将代码放置于网站的根目录

    [root@web01 html]# cd /var/www/html/

  3、上传代码,并解压(unzip kaoshi.zip)

  4、关闭selinux和防火墙

    [root@web01 html]# setenforce 0

    [root@web01 html]# systemctl disable --now firewalld

  5、创建目录并授权

    [root@web01 html]# mkdir upload
    [root@web01 html]# chown www.www upload

  6、修改web软件的用户

    [root@web01 html]# vim /etc/httpd/conf/httpd.conf

    User www

    Group www

  7、启动web软件

    [root@web01 html]# systemctl start httpd

  8、测试

    上传

      [root@web01 html]# ll upload
      total 48
      -rw-r--r-- 1 www www 45286 Dec 30 17:21 1_linux.jpg

    访问:http://172.16.1.7/upload/1_linux.jpg

  注:查看系统日志

    [root@web01 html]# tail -f /var/log/httpd/

    [root@web01 html]# tail -f /var/log/httpd/error_log

    查看进程使用的用户

    [root@web01 html]# ps -ef | grep httpd

5.2、配合NFS实现文件共享

  1、创建挂载点

    [root@nfs web]# mkdir /web/upload

    [root@nfs web]# chown www.www /web/upload

  2、修改NFS实现实现文件共享

    [root@nfs web]# vim /etc/exports
    [root@nfs web]# cat /etc/exports
    /web/nfs1 172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)
    /web/upload 172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)

  3、重启NFS

    [root@nfs web]# systemctl restart nfs-server rpcbind

  4、查看服务端是否正常

    [root@nfs web]# showmount -e

    Export list for nfs:

    /web/upload 172.16.1.0/20

    /web/nfs1 172.16.1.0/20

  5、客户端安装NFS软件

    [root@web01 opt]# yum install -y nfs-utils

    [root@web02 opt]# yum install -y nfs-utils

    [root@web03 opt]# yum install -y nfs-utils

  6、挂载

    [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

  7、测试

    用web2上传,web3查看

 

[root@nfs web]# ll upload
total 208
-rw-r--r-- 1 www www 166491 Dec 30 19:18 2_linux.jpg
-rw-r--r-- 1 www www  41588 Dec 30 19:16 3_linux.jpg
[root@web01 html]# ll upload
total 208
-rw-r--r-- 1 www www 166491 Dec 30 19:18 2_linux.jpg
-rw-r--r-- 1 www www  41588 Dec 30 19:16 3_linux.jpg
[root@web02 html]# ll upload
total 208
-rw-r--r-- 1 www www 166491 Dec 30 19:18 2_linux.jpg
-rw-r--r-- 1 www www  41588 Dec 30 19:16 3_linux.jpg
[root@web03 html]# ll upload
total 208
-rw-r--r-- 1 www www 166491 Dec 30 19:18 2_linux.jpg
-rw-r--r-- 1 www www  41588 Dec 30 19:16 3_linux.jpg

相关