参考文档:

https://www.cnblogs.com/lfl17718347843/p/14131062.html

https://www.jianshu.com/p/b860d26f2951

https://blog.51cto.com/u_13941177/2426215

https://www.cnblogs.com/xiajq/p/11395211.html

https://www.cnblogs.com/canflyfish/p/11385613.html

安装nfs(如果rpcbind已经安装可不用安装)

1[root@k8s-master ~]# yum install nfs-utils rpcbind

注意:客户端和服务端都需要安装

——————————————————- 服务端配置 ——————————————————-

启动rpcbind、nfs服务

1[root@k8s-master nginx]# systemctl restart rpcbind && systemctl enable rpcbind
2[root@k8s-master nginx]# systemctl restart nfs && systemctl enable nfs

创建nfs储存目录(即共享目录)

1[root@k8s-master ~]# mkdir -p /data/nfs-data
2
3# 更改权限(很重要!!!)
4[root@k8s-master ~]# chown -R nfsnobody:nfsnobody /data/nfs-data

编辑export文件,这个文件就是nfs默认的配置文件

1[root@k8s-master ~]# vi /etc/exports
2/data/nfs-data *(rw,sync)

配置说明:

配置的格式:

[共享目录] [客户端地址1(权限参数)] [客户端地址2(权限参数)]

共享目录:存在于我们本机上的目录,我们想共享给网络上的其他主机使用。如我要共享/tmp/data目录,那么此选项可以就直接写/tmp/data目录,这个目录可以依照不同的权限共享给不同的主机。

客户端地址:客户端地址能够设置一个网络,也可以设置单个主机

客户端地址的设置主要有以下几种方式:

1)、 可以使用完整的IP或者是网络号,例如192.168.100.100 或 192.168.8.0/24

2)、 可以使用主机名,但这个主机名必须要在/etc/hosts内,或可以使用DNS找到该名称才行,反正重点是可找到IP就行;如果是主机名的话,还可以支持通配符,例如‘*’或‘?’均可接受;例如:host[1-8].ctos.zu,server?.test.com

权限参数可以有多个,以逗号分隔(客户端地址和权限参数中间不能有空格)

说明:

ro 只读访问

rw 读写访问

sync 所有数据在请求时写入共享

async nfs在写入数据前可以响应请求

secure nfs通过1024以下的安全TCP/IP端口发送

insecure nfs通过1024以上的端口发送

wdelay 如果多个用户要写入nfs目录,则归组写入(默认)

no_wdelay 如果多个用户要写入nfs目录,则立即写入,当使用async时,无需此设置

hide 在nfs共享目录中不共享其子目录

no_hide 共享nfs目录的子目录

subtree_check 如果共享/usr/bin之类的子目录时,强制nfs检查父目录的权限(默认)

no_subtree_check 不检查父目录权限

all_squash 共享文件的UID和GID映射匿名用户anonymous,适合公用目录

no_all_squash 保留共享文件的UID和GID(默认)

root_squash root用户的所有请求映射成如anonymous用户一样的权限(默认)

no_root_squash root用户具有根目录的完全管理访问权限(不安全)

anonuid=xxx 指定nfs服务器/etc/passwd文件中匿名用户的UID

anongid=xxx 指定nfs服务器/etc/passwd文件中匿名用户的GID

配置生效(或者重启服务亦可)

1[root@k8s-master ~]# exportfs -arv

状态查询

 1#查看生效
 2[root@k8s-master ~]# exportfs
 3/data/nfs-data  <world>
 4
 5#查看 RPC 服务的注册状况
 6[root@k8s-master ~]# rpcinfo -p localhost
 7   program vers proto   port  service
 8    100000    4   tcp    111  portmapper
 9    100000    3   tcp    111  portmapper
10    100000    2   tcp    111  portmapper
11    100000    4   udp    111  portmapper
12    100000    3   udp    111  portmapper
13    100000    2   udp    111  portmapper
14    100005    1   udp  20048  mountd
15    100005    1   tcp  20048  mountd
16    100005    2   udp  20048  mountd
17    100005    2   tcp  20048  mountd
18    100005    3   udp  20048  mountd
19    100005    3   tcp  20048  mountd
20    100003    3   tcp   2049  nfs
21    100003    4   tcp   2049  nfs
22    100227    3   tcp   2049  nfs_acl
23    100003    3   udp   2049  nfs
24    100003    4   udp   2049  nfs
25    100227    3   udp   2049  nfs_acl
26    100021    1   udp  44473  nlockmgr
27    100021    3   udp  44473  nlockmgr
28    100021    4   udp  44473  nlockmgr
29    100021    1   tcp  42496  nlockmgr
30    100021    3   tcp  42496  nlockmgr
31    100021    4   tcp  42496  nlockmgr
32
33#showmount测试
34[root@k8s-master ~]# showmount -e localhost
35Export list for k8s-master:
36/data/nfs-data *

——————————————————- 客户端配置 ——————————————————-

客户端上不需要启动nfs服务,只是为了使用showmount工具。但是客户端要确保 rpcbind 服务启动

测试nfs服务状态

1[root@k8s-node1 ~]# showmount -e 172.16.2.135
2Export list for 172.16.2.135:
3/data/nfs-data *

172.16.2.135 是服务端的ip,也可以通过hosts配置主机名

挂载至本地/mnt目录

1[root@k8s-node1 ~]# mount -t nfs 172.16.2.135:/data /mnt

此时,服务端的目录已经挂载到本地的/mnt目录下了