NFS存储
NFS即网络文件系统Network File System,它是一种分布式文件系统协议,最初是由Sun MicroSystems公司开发的类Unix操作系统之上的一款经典网络存储方案,其功能是在允许客户端主机可以像访问本地存储一样通过网络访问服务端文件。
Kubernetes的NFS存储用于将某事先存在的NFS服务器导出export的存储空间挂载到Pod中来供Pod容器使用。与emptyDir不同的是,NFS存储在Pod对象终止后仅是被卸载而非删除。另外,NFS是文件系统及共享服务,它支持同时存在多路挂载请求。定义NFS存储时,常用到以下字段。
- server:NFS服务器的IP地址或者主机名,必选字段。
- path:NFS服务器导出(共享)的文件系统路径,必选字段。
- readOnly:是否以只读挂载,默认为false。
数据卷:NFS
NFS数据卷:提供对NFS挂载支持,可以自动将NFS共享路径挂载到Pod中,在实际生产环境当中使用还是挺多的,性能还行,使用方便。如果是分布式维护成本高
NFS:是一个主流的文件共享服务器。
部署一个NFS服务
在集群之外的节点192.168.179.102
#ubuntu部署
sudo apt install nfs-kernel-server
#centos部署 注:每个Node上都要安装nfs-utils包
yum -y install rpcbind nfs-utils
[root@nfs ~]# yum install nfs-utils
[root@k8s-master ~]# yum install nfs-utils
[root@k8s-node1 ~]# yum install nfs-utils
[root@k8s-node2 ~]# yum install nfs-utils
#创建要共享的目录
[root@localhost ~]# mkdir -p /ifs/kubernetes
#编辑NFS配置并加入以下内容
[root@localhost ~]# vi /etc/exports
/ifs/kubernetes 192.168.179.0/24(rw,sync,no_all_squash,no_subtree_check)
#载入配置
exportfs -rv
•/ifs/kubernetes:NFS服务要共享的目录
•192.168.31.0/24:允许访问NFS服务器的网段,也可以写 * ,表示所有地址都可以访问NFS服务
•rw:访问到此目录的服务器都具备读写权限
•sync:数据同步写入内存和硬盘
•no_all_squash:所有用户对根目录具备完全管理访问权限
•no_subtree_check:不检查父目录的权限
启动NFS服务
#ubuntu启动
systemctl start nfs-kernel-server
#centos启动,每个节点都需要启动,k8s节点和nfs节点
systemctl start rpcbind nfs
服务检查
[root@localhost ~]# cat /var/lib/nfs/etab
/ifs/kubernetes 192.168.179.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,no_all_squash)
[root@k8s-master ~]# showmount -e 192.168.179.102
Export list for 192.168.179.102:
/ifs/kubernetes 192.168.179.0/24
创建Pod资源配置清单
Pod中使用Redis镜像来运行容器,将nginx数据持久化至NFS服务器上,下面是简单实用mginx的一个示例:
[root@k8s-master ~]# cat nginx-nfs.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-nfs
spec:
replicas: 3
selector:
matchLabels:
app: nginx-nfs
template:
metadata:
labels:
app: nginx-nfs
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: wwwroot #卷名称
mountPath: /usr/share/nginx/html #卷挂载到容器中的目录
ports:
- containerPort: 80
volumes:
- name: wwwroot #卷名称
nfs: #使用NFS网络存储卷
server: 192.168.179.102 #使用NFS网络存储卷
path: /ifs/kubernetes #NFS服务器共享的目录
readOnly: false #是否为只读
测试存储卷
[root@k8s-master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deployment-nfs-64fd86bfcc-cl8x8 1/1 Running 0 108s 10.244.0.17 k8s-master <none> <none>
deployment-nfs-64fd86bfcc-q54fk 1/1 Running 0 108s 10.244.0.18 k8s-master <none> <none>
deployment-nfs-64fd86bfcc-tq448 1/1 Running 0 108s 10.244.0.16 k8s-master <none> <none>
可以看到数据持久化了
[root@k8s-master ~]# curl 10.244.0.17
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.19.6</center>
</body>
</html>
#nfs上
[root@localhost kubernetes]# echo "hellow this is nfs" >index.html
[root@k8s-master ~]# curl 10.244.0.17
hellow this is nfs