Centos7下创建NFS存储
一. 开篇三问~
Q1: 这次为什么要尝试NFS?
A1: 因为kubernets的PV打算使用nfs作为存储路径,并且支持 ReadWriteMany 模式被多个node同时读写。
Q2: NFS是什么?
A2: Network File System的缩写,即网络文件系统。客户端通过挂载的方式将NFS服务器端共享的数据目录挂载到本地目录下。
Q3. NFS为什么需要RPC?
因为NFS支持的功能很多,不同功能会使用不同程序来启动,因此,NFS对应的功能所对应的端口无法固定。
端口不固定造成客户端与服务端之间的通信障碍,所以需要RPC来从中帮忙。
NFS启动时会随机取用若干端口,然后主动向RPC服务注册取用相关端口和功能信息,RPC使用固定端口111来监听来自NFS客户端的请求,
并将正确的NFS服务端口信息返回给客户端,这样客户端与服务端就可以进行数据传输了。
二、NFS的工作流程
1、由程序在NFS客户端发起存取文件的请求,客户端本地的RPC(rpcbind)服务会通过网络向NFS服务端的RPC的111端口发出文件存取功能的请求。
2、NFS服务端的RPC找到对应已注册的NFS端口,通知客户端RPC服务。
3、客户端获取正确的端口,并与NFS daemon联机存取数据。
4、存取数据成功后,返回前端访问程序,完成一次存取操作。
所以无论客户端,服务端,需要使用NFS,必须安装RPC服务。
NFS的RPC服务,在Centos5下名为portmap,Centos6下名称为rpcbind。
关于更多概念信息,请移步:http://linux.vbird.org/linux_server/0330nfs.php
三、NFS组件安装,包括服务端与客户机
主机1:k8s01 10.0.2.15 nfs服务器将位于k8s01
主机2:k8s02 10.0.2.8
## 安装 nfs-utils rpcbind
[root@k8s01 ~]# yum install nfs-utils rpcbind
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirrors.huaweicloud.com
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.tuna.tsinghua.edu.cn
base | 3.6 kB 00:00:00
docker-ce-stable | 2.9 kB 00:00:00
extras | 3.4 kB 00:00:00
kubernetes | 1.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/3): extras/7/x86_64/primary_db | 204 kB 00:00:00
(2/3): updates/7/x86_64/primary_db | 6.0 MB 00:00:01
(3/3): kubernetes/primary | 37 kB 00:00:03
kubernetes 272/272
Resolving Dependencies
--> Running transaction check
---> Package nfs-utils.x86_64 1:1.3.0-0.54.el7 will be installed
--> Processing Dependency: libtirpc >= 0.2.4-0.7 for package: 1:nfs-utils-1.3.0-0.54.el7.x86_64
...................
...................
Installed:
nfs-utils.x86_64 1:1.3.0-0.54.el7 rpcbind.x86_64 0:0.2.0-44.el7
Dependency Installed:
gssproxy.x86_64 0:0.7.0-17.el7 keyutils.x86_64 0:1.5.8-3.el7 libbasicobjects.x86_64 0:0.1.1-29.el7
libcollection.x86_64 0:0.7.0-29.el7 libevent.x86_64 0:2.0.21-4.el7 libini_config.x86_64 0:1.3.1-29.el7
libnfsidmap.x86_64 0:0.25-19.el7 libpath_utils.x86_64 0:0.2.1-29.el7 libref_array.x86_64 0:0.1.5-29.el7
libtirpc.x86_64 0:0.2.4-0.10.el7 libverto-libevent.x86_64 0:0.2.5-4.el7 quota.x86_64 1:4.01-17.el7
quota-nls.noarch 1:4.01-17.el7 tcp_wrappers.x86_64 0:7.6-77.el7
Complete!
[root@k8s01 ~]#
四、启动 nfs rpcbind 服务,并自动启动
[root@k8s01 ~]# systemctl start rpcbind && systemctl enable rpcbind
[root@k8s01 ~]# systemctl start nfs && systemctl enable nfs
[root@k8s01 ~]# systemctl status rpcbind
● rpcbind.service - RPC bind service
Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2018-11-07 14:20:07 CST; 40s ago
Main PID: 11098 (rpcbind)
CGroup: /system.slice/rpcbind.service
└─11098 /sbin/rpcbind -w
Nov 07 14:20:07 k8s01 systemd[1]: Starting RPC bind service...
Nov 07 14:20:07 k8s01 systemd[1]: Started RPC bind service.
[root@k8s01 ~]# systemctl status nfs
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
Active: active (exited) since Wed 2018-11-07 14:20:19 CST; 33s ago
Main PID: 11254 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/nfs-server.service
Nov 07 14:20:19 k8s01 systemd[1]: Starting NFS server and services...
Nov 07 14:20:19 k8s01 systemd[1]: Started NFS server and services.
## 查看rpc,可以看见rpcbind已经在111端口监听
[root@k8s01 ~]# lsof -i :111
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 11098 rpc 6u IPv4 168477 0t0 UDP *:sunrpc
rpcbind 11098 rpc 8u IPv4 168479 0t0 TCP *:sunrpc (LISTEN)
rpcbind 11098 rpc 9u IPv6 168480 0t0 UDP *:sunrpc
rpcbind 11098 rpc 11u IPv6 168482 0t0 TCP *:sunrpc (LISTEN)
[root@k8s01 ~]# netstat -lntup | grep rpcbind
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 11098/rpcbind
tcp6 0 0 :::111 :::* LISTEN 11098/rpcbind
udp 0 0 0.0.0.0:673 0.0.0.0:* 11098/rpcbind
udp 0 0 0.0.0.0:111 0.0.0.0:* 11098/rpcbind
udp6 0 0 :::673 :::* 11098/rpcbind
udp6 0 0 :::111 :::* 11098/rpcbind
## 查看nfs服务向rpc注册的端口信息
[root@k8s01 ~]# rpcinfo -p localhost
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
100024 1 udp 40704 status
100024 1 tcp 60118 status
100005 1 udp 20048 mountd
100005 1 tcp 20048 mountd
100005 2 udp 20048 mountd
100005 2 tcp 20048 mountd
100005 3 udp 20048 mountd
100005 3 tcp 20048 mountd
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 3 tcp 2049 nfs_acl
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100227 3 udp 2049 nfs_acl
100021 1 udp 50438 nlockmgr
100021 3 udp 50438 nlockmgr
100021 4 udp 50438 nlockmgr
100021 1 tcp 38820 nlockmgr
100021 3 tcp 38820 nlockmgr
100021 4 tcp 38820 nlockmgr
## NFS常见进程详解
[root@k8s01 ~]# ps -ef|egrep "rpc|nfs"
rpc 11098 1 0 14:20 ? 00:00:00 /sbin/rpcbind -w
rpcuser 11231 1 0 14:20 ? 00:00:00 /usr/sbin/rpc.statd
root 11232 2 0 14:20 ? 00:00:00 [rpciod]
root 11238 1 0 14:20 ? 00:00:00 /usr/sbin/rpc.idmapd
root 11246 1 0 14:20 ? 00:00:00 /usr/sbin/rpc.mountd
root 11256 2 0 14:20 ? 00:00:00 [nfsd4_callbacks]
root 11262 2 0 14:20 ? 00:00:00 [nfsd]
root 11263 2 0 14:20 ? 00:00:00 [nfsd]
root 11264 2 0 14:20 ? 00:00:00 [nfsd]
root 11265 2 0 14:20 ? 00:00:00 [nfsd]
root 11266 2 0 14:20 ? 00:00:00 [nfsd]
root 11267 2 0 14:20 ? 00:00:00 [nfsd]
root 11268 2 0 14:20 ? 00:00:00 [nfsd]
root 11269 2 0 14:20 ? 00:00:00 [nfsd]
root 12622 1168 0 14:24 pts/0 00:00:00 grep -E --color=auto rpc|nfs
nfsd(rpc.nfsd)主进程,主要是管理客户端能否登入服务端,登入者ID判别。
mountd(rpc.mountd)管理NFS文件系统,登入者的权限管理
rpc.lockd(非必要)用来锁定文件,用于客户端同时写入
rpc.statd(非必要)检查文件一致性
rpc.idmapd 名字映射后台进程
五、NFS服务端配置
# 在server机创建即将挂载的文件目录,并且更改用户及用户组为 nfsnobody.nfsnobody
[root@k8s01 ~]# mkdir -p /NFS/dir1
[root@k8s01 ~]# chown -R nfsnobody.nfsnobody /NFS/
[root@k8s01 ~]# ls -lrt /NFS/
total 0
drwxr-xr-x 2 nfsnobody nfsnobody 6 Nov 7 14:28 dir1
[root@k8s01 ~]# echo "/NFS 10.0.2.15/24(rw,sync)" > /etc/exports
[root@k8s01 ~]# cat /etc/exports
/NFS 10.0.2.15/24(rw,sync)
# 参数说明:
rw 在NFS卷上同时允许读取和写入请求。
ro 在NFS卷上只允许读取请求。
sync 只有在更改已提交到稳定存储后才会对请求进行回复。 (默认)
async 此选项允许NFS服务器违反NFS协议并在该请求所做的任何更改已提交到稳定存储器之前对请求进行回复。
secure 此选项要求请求源自小于IPPORT_RESERVED(1024)的Internet端口。 (默认)
insecure 此选项接受所有端口。
wdelay 如果其怀疑另一个相关的写入请求可能正在进行或可能很快到达,则延迟向磁盘提交写入请求。 (默认)
no_wdelay 如果同时设置了异步,此选项不起作用。如果NFS服务器怀疑另一个相关的写入请求可能正在进行或可能很快到达,则NFS服务器通常会将写入请求延迟提交给磁盘。这允许多个写入请求提交到磁盘,其中一个操作可以提高性能。如果NFS服务器主要接收小的无关请求,这种行为实际上可能会降低性能,因此no_wdelay可用于关闭它。
no_subtree_check 此选项禁用子树检查,它具有温和的安全影响,但可以提高某些情况下的可靠性
root_squash 将请求从uid / gid 0映射到匿名uid / gid。请注意,这不适用于可能同样敏感的任何其他uid或gids,例如用户bin或组工作人员。
all_squash 将所有uids和gids映射到匿名用户。用于NFS导出的公共FTP目录,新闻spool目录等。
no_all_squash Turn off all squashing. (Default)
anonuid=UID 这些选项显式设置匿名帐户的uid和gid。此选项主要适用于PC / NFS客户端,您可能希望所有请求显示为来自一个用户。
# 重新加载nfs配置
[root@k8s01 ~]# exportfs -rv
exporting 10.0.2.15/24:/NFS
[root@k8s01 ~]#
# 查看nfs服务器挂载情况
[root@k8s01 ~]# showmount -e 127.0.0.1
Export list for 127.0.0.1:
/NFS 10.0.2.15/24
六、挂载测试
# 注意挂载后设备可用空间实际上是该目录在NFS server上所在分区的可用空间
# 在k8s01本地挂载:
[root@k8s01 ~]# mount -t nfs 10.0.2.15:/NFS /mnt/
[root@k8s01 ~]# ls -lrt /mnt/
total 0
drwxr-xr-x 2 nfsnobody nfsnobody 6 Nov 7 14:28 dir1
[root@k8s01 ~]# df -h | grep NFS
10.0.2.15:/NFS 38G 2.8G 36G 8% /mnt
# 在k8s02上挂载:
[root@k8s02 ~]# mount -t nfs 10.0.2.15:/NFS /mnt/
[root@k8s02 ~]# ls -lrt /mnt/
total 0
drwxr-xr-x 2 nfsnobody nfsnobody 6 Nov 7 14:28 dir1
[root@k8s02 ~]# df -h | grep NFS
10.0.2.15:/NFS 38G 2.8G 36G 8% /mnt
# 尝试在两端向同一文件写入数据
[root@k8s01 ~]# echo 123 > /mnt/dir1/1.txt
[root@k8s02 ~]# echo 456 >> /mnt/dir1/1.txt
[root@k8s01 ~]# cat /mnt/dir1/1.txt
123
456
# 卸载挂载
[root@k8s02 ~]# umount /mnt/