一、NFS服务简介

NFS是Network File System的缩写,即网络文件系统。客户端通过挂载的方式将NFS服务器端共享的数据目录挂载到本地目录下。主要功能指的是共享文件。
为什么需要安装NFS服务?当服务器访问流量过大时,需要多台服务器进行分流,而这多台服务器可以使用NFS服务进行共享。(NFS服务是最基础的共享服务)
NFS应用场景,常用于高可用文件共享,多台服务器共享同样的数据,可扩展性比较差,本身高可用方案不完善,取而代之的数据量比较大的可以采用MFS、TFS、HDFS等等分布式文件系统。

二、NFS服务安装

命令如下

  1. [root@localhost ~]# yum -y install nfs-utils rpcbind //两台节点同时执行
  2. [root@localhost ~]# mkdir /mnt/test //第一台节点创建
  3. [root@localhost ~]# vi /etc/exports //第一台编辑共享参数
  4. /mnt/test 192.168.100.0/24(rw,no_root_squash,no_all_squash,sync,anonuid=501,anongid=501)
  5. 注意要在一行上

参数详解:
/mnt/test:共享目录 rw 读写权限
no_root_squash:nfs服务连接客户端时,使用root用户登录,对于服务来说存在隐患
no_all_squash:无论NFS客户端连接服务端用什么用户,对服务端来说都有匿名用户的权限
Sync:存入硬盘的同时,同时写入内存
Anonuid:匿名用户UID
Anongid:匿名用户GID

  1. [root@localhost ~]# exportfs -r //第一台生效配置文件
  2. [root@localhost ~]# systemctl start rpcbind //第一台启动服务
  3. [root@localhost ~]# systemctl start nfs //第一台启动服务
  4. [root@localhost ~]# showmount -e 192.168.100.11 //第一台列出可挂载参数
  5. Export list for 192.168.100.11:
  6. /mnt/test 192.168.100.0/24
  7. [root@localhost ~]# mount -t nfs 192.168.100.11:/mnt/test /mnt/ //第二台挂载
  8. [root@localhost ~]# df -h //查看挂载
  9. Filesystem Size Used Avail Use% Mounted on
  10. /dev/mapper/centos-root 150G 1.1G 149G 1% /
  11. devtmpfs 1.9G 0 1.9G 0% /dev
  12. tmpfs 1.9G 0 1.9G 0% /dev/shm
  13. tmpfs 1.9G 12M 1.9G 1% /run
  14. tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
  15. /dev/sda1 1014M 142M 873M 14% /boot
  16. tmpfs 378M 0 378M 0% /run/user/0
  17. 192.168.100.11:/mnt/test 150G 1.1G 149G 1% /mnt

测试

  1. [root@localhost ~]# cd /mnt/test/ //第一台
  2. [root@localhost test]# touch 123.txt //第一台
  3. [root@localhost ~]# cd /mnt/ //第二台
  4. [root@localhost mnt]# ll //第二台
  5. total 0
  6. -rw-r--r-- 1 root root 0 Oct 15 03:43 123.txt