NFS Mount Intro

NFS, or Network File System

一种分布式文件系统协议,允许你在服务器上挂载远程目录,你可以管理不同位置的存储空间并从多客户端写入该空间. NFS提供一种相对标准和高效的方式来通过网络访问远程系统。并且适合访问共享资源的情况.

Step 1 - Download and Install the Components

  • On the Host
    1
    2
    3
    $ sudo apt update

    $ sudo apt install nfs-kernel-server (允许你分享文件目录)
  • On the Client
    1
    2
    $ sudo apt update
    $ sudo apt install nfs-common

Step 2 - Create the Share DIrectories on the Host

  • Example 1: Exporting a General Purpose Mount
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    ubuntu in ~ at k8s-node1 via 🐍 3.8.6
    ➜ sudo mkdir /var/nfs/general -p # 创建目录

    ubuntu in ~ at k8s-node1 via 🐍 3.8.6
    ➜ ls -la /var/nfs/general
    total 8
    drwxr-xr-x 2 root root 4096 Oct 15 13:41 .
    drwxr-xr-x 3 root root 4096 Oct 15 13:41 ..

    > NFS会将客户端上的任何根操作转换为nobody:nogroup凭证作为安全措施,因此我们需要更改目录所有权匹配这些凭证.

    ubuntu in ~ at k8s-node1 via 🐍 3.8.6
    ➜ sudo chown nobody:nogroup /var/nfs/general

Step 3 - Configuring the NFS Exports on the Host Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
chyi in ~ at k8s-master
➜ sudo vim /etc/exports
# This file is auto-generated by openmediavault (https://www.openmediavault.org)
# WARNING: Do not edit this file, your changes will get lost.

# directory_to_share client(share_option1,...,share_optionN)

# /etc/exports: the access control list for filesystems which may be exported
# to NFS clients. See exports(5).
/export/K8sData 172.30.1.0/24(fsid=2,rw,subtree_check,insecure)
- rw: 此选项为客户端计算机提供对卷的读写访问权限
- sync: 此选项强制NFS在响应恢复之前将更改写入磁盘,会导致更稳定和一致的环境,因为恢复响应远程卷的世纪状态,但是他也降低了文件操作的速度.
- no_subtree_check: 此选项防止子树检查,这是一个过程,主机必须检查该文件是否在每个请求的导出树中实际上仍然可用,在客户端打开文件和重命名文件时
- no_root_squash: 默认情况下,NFS将来自root用户的远程请求转换为服务器上的非特权用户,这是为了防止客户端上的root账户以root身份使用主机的文件系统的安全功能

$ sudo systemctl restart nfs-kernel-server

Step 4 - Create Mount Points and Mount Directories on the Client

1
2
3
4
5
6
7
8
> 为了使远程共享在客户端可用,我们需要将主机上要共享的目录挂载到客户端上的空目录中
> 如果你的挂载点中有文件和目录,一旦你挂载NFS共享,他们就会被隐藏,为了避免丢失重要文件,请确保你挂载在已存在的目录中,并且该目录为空.

ubuntu in ~ at 3B
➜ sudo mkdir /k8s_data

ubuntu in ~ at 3B
➜ sudo mount 172.30.1.14:/export/K8sData /k8s_data

Step 5 - Mounting the Remote NFS Directories at Boot

1
2
3
4
5
6
> 通过将远程NFS共享添加到客户端上的/etc/fstab文件,可以在启动是自动挂载远程NFS共享

ubuntu in ~ at 3B
➜ sudo vim /etc/fstab

172.30.1.14:/export/K8sData /k8s_data nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0

Step 6 - Unmounting an NFS Remote Share

1
$ sudo umount /k8s_data