kubernetes的存储机制NFS管理
网络共享存储
虽然hostpath可以根据pod的配置自动创建关联的宿主机目录,但是因为pod通常会运行在不同的宿主机上,因此每个宿主机目录下面可能没有想需要的相同的数据文件
在集群场景中,想要实现比基于hostpath方式实现更强大所有的数据文件进行同步功能。
基于此,需要一种可以实现网络共享的存储方式。
Kubernetes 支持多种网络共享的存储方法
实现NFS的网络共享存储

NFS的网络共享存储配置说明
https://kubernetes.io/zh-cn/docs/concepts/storage/volumes/#nfs
https://github.com/kubernetes/examples/tree/master/staging/volumes/nfs
和传统的方式一样, 通过 NFS 网络文件系统可以实现Kubernetes数据的网络存储共享
使用NFS提供的共享目录存储数据时,需要在系统中部署一个NFS环境,通过volume的配置,实现pod内的容器间共享NFS目录。
#NFS服务器软件安装
# 安装 nfs-utils(包含 nfs-server 服务)
dnf install -y nfs-utils
# 启动并设置开机自启
systemctl enable --now nfs-server
# 验证状态
systemctl status nfs-server
创建共享目录
mkdir -p /data/nfsshare
编辑 /etc/exports
echo "/data/nfsshare 192.168.3.0/24(rw,sync,no_root_squash)" >> /etc/exports
192.168.3.0/24 改成你的客户端网段,no_root_squash 允许 root 用户保持 root 权限(按需使用)。
生效配置
#重启服务
exportfs -r
-v 查看
exportfs -v
[root@master1 storage]# exportfs -v /data/nfsshare 192.168.3.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
#说明:
*:表示允许所有连接,该值可以是一个网段|IP|域名的形式
rw 表示读写共享权限
no_root_squash 表示root登录nfs资源的时候,不压缩其权限
sync 所有操作数据会同时写入硬盘和内存
验证
#测试访问
[root@master1 storage]# showmount -e 192.168.3.60 Export list for 192.168.3.60: /data/nfsshare 192.168.3.0/24
属性解析
#配置属性:
kubectl explain pod.spec.volumes.nfs
server #指定nfs服务器的地址
path #指定nfs服务器暴露的共享地址
readOnly #是否只能读,默认false
#配置格式: volumes: - name: <卷名称> nfs: server: nfs_server_address #指定NFS服务器地址,如果是域名,需要宿主机节点能解析此域名 path: "共享目录" #指定NFS共享目录 readOnly: false #指定权限
#注意事项:要求Kubernetes所有集群都必须支持nfs客户端命令,即所有节点都必须执行
dnf install -y nfs-utils
#配置共享目录
mkdir /nfsdata
[root@master1 ~]# cat /etc/exports #/data/nfsshare 192.168.3.0/24(rw,sync,no_root_squash) /nfsdata * [root@master1 ~]#
[root@master1 ~]# exportfs -r
exportfs: No options for /nfsdata *: suggest *(sync) to avoid warning
[root@master1 ~]# exportfs -v
/nfsdata <world>(sync,wdelay,hide,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash)
#注意:如果所有客户端想要使用nfs的功能,必须提前安装软件,否则会发生找不到资源的报错。
#编写资源配置文件
apiVersion: apps/v1 kind: Deployment metadata: name: volumes-nfs spec: replicas: 1 selector: matchLabels: app: volumes-nfs template: metadata: labels: app: volumes-nfs spec: nodeName: node1.org #指定在node1节点运行pod,节点的/etc/hosts文件或DNS解析此域名 containers: - name: hostpath-redis image: registry.cn-beijing.aliyuncs.com/wangxiaochun/redis:6.2.5 volumeMounts: - name: redisdatapath mountPath: /data volumes: - name: redisdatapath nfs: server: nfs.org #注意:需要宿主机节点/etc/hosts文件或DNS解析此域名,而不是由Pod通过coreDNS完成解析 path: /nfsdata
kubectl apply -f storage-nfs-1.yaml
nfs服务器通过域名访问,每台宿主机节点做/etc/hosts文件域名解析
[root@node3 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.3.60 kubeapi.org kubeapi 192.168.3.60 master1.org master1 192.168.3.60 nfs.org master1 192.168.3.61 master2.org master2 192.168.3.62 master3.org master3 192.168.3.63 node1.org node1 192.168.3.64 node2.org node2 192.168.3.65 node3.org node3 192.168.3.66 ha1.org ha1 192.168.3.67 ha2.org ha2
#注意:需要宿主机节点/etc/hosts文件或DNS解析此域名
只读
[root@master1 storage]# kubectl logs volumes-nfs-69fd8c8c84-fgw4q chown: changing ownership of '.': Read-only file system
nfs配置默认是只读
[root@master1 storage]# cat /etc/exports #/data/nfsshare 192.168.3.0/24(rw,sync,no_root_squash) /nfsdata *(rw)
[root@master1 storage]# exportfs -r
[root@master1 storage]# exportfs -v
/nfsdata <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
没有权限
root_squash,表示root登录nfs资源的时候,,压缩root权限了
[root@master1 storage]# kubectl logs volumes-nfs-69fd8c8c84-kwtkf chown: changing ownership of '.': Operation not permitted
[root@master1 storage]# cat /etc/exports #/data/nfsshare 192.168.3.0/24(rw,sync,no_root_squash) /nfsdata *(rw,no_root_squash) [root@master1 storage]# exportfs -r [root@master1 storage]# exportfs -v /nfsdata <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
权限问题解决pod状态正常
[root@master1 storage]# kubectl get pod NAME READY STATUS RESTARTS AGE controller-deployment-demo-5f884bb8-2ppvf 1/1 Running 1 (84m ago) 14h controller-deployment-demo-5f884bb8-fscrl 1/1 Running 10 (84m ago) 6d17h controller-deployment-demo-5f884bb8-rfsbv 1/1 Running 9 (84m ago) 6d14h volumes-nfs-69fd8c8c84-kwtkf 1/1 Running 6 (10m ago) 13m
#写入数据
root@master1 storage]# kubectl exec -it volumes-nfs-69fd8c8c84-kwtkf -- bash root@volumes-nfs-69fd8c8c84-kwtkf:/data# root@volumes-nfs-69fd8c8c84-kwtkf:/data# redis-cli 127.0.0.1:6379> set name tom OK 127.0.0.1:6379> save OK 127.0.0.1:6379> exit root@volumes-nfs-69fd8c8c84-kwtkf:/data#
#验证数据生成
root@volumes-nfs-69fd8c8c84-kwtkf:/data# ls /data dump.rdb
存在nfs共享目录
[root@master1 storage]# ls /nfsdata/ dump.rdb
节点现在调度在node1
[root@master1 storage]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES controller-deployment-demo-5f884bb8-2ppvf 1/1 Running 1 (93m ago) 14h 10.244.1.158 node1.org <none> <none> controller-deployment-demo-5f884bb8-fscrl 1/1 Running 10 (93m ago) 6d17h 10.244.1.161 node1.org <none> <none> controller-deployment-demo-5f884bb8-rfsbv 1/1 Running 9 (93m ago) 6d14h 10.244.1.163 node1.org <none> <none> volumes-nfs-69fd8c8c84-kwtkf 1/1 Running 6 (19m ago) 22m 10.244.1.166 node1.org <none> <none>
删除pod
kubectl delete -f storage-nfs-1.yaml
创建一个新pod 固定调度到node2
[root@master1 storage]# cat storage-nfs-1.yaml apiVersion: apps/v1 kind: Deployment metadata: name: volumes-nfs spec: replicas: 1 selector: matchLabels: app: volumes-nfs template: metadata: labels: app: volumes-nfs spec: nodeName: node2.org #指定在node1节点运行pod,节点的/etc/hosts文件或DNS解析此域名 containers: - name: hostpath-redis image: registry.cn-beijing.aliyuncs.com/wangxiaochun/redis:6.2.5 volumeMounts: - name: redisdatapath mountPath: /data volumes: - name: redisdatapath nfs: server: nfs.org #注意:需要宿主机节点/etc/hosts文件或DNS解析此域名,而不是由Pod通过coreDNS完成解析 path: /nfsdata
kubectl apply -f storage-nfs-1.yaml
调度到node2
[root@master1 storage]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES controller-deployment-demo-5f884bb8-2ppvf 1/1 Running 1 (96m ago) 14h 10.244.1.158 node1.org <none> <none> controller-deployment-demo-5f884bb8-fscrl 1/1 Running 10 (96m ago) 6d18h 10.244.1.161 node1.org <none> <none> controller-deployment-demo-5f884bb8-rfsbv 1/1 Running 9 (96m ago) 6d14h 10.244.1.163 node1.org <none> <none> volumes-nfs-6796b4b755-xlbtv 1/1 Running 0 29s 10.244.2.176 node2.org <none> <none>
#验证数据生成
root@master1 storage]# kubectl exec -it volumes-nfs-6796b4b755-xlbtv -- bash root@volumes-nfs-6796b4b755-xlbtv:/data# root@volumes-nfs-6796b4b755-xlbtv:/data# redis-cli 127.0.0.1:6379> get name "tom" 127.0.0.1:6379> exit root@volumes-nfs-6796b4b755-xlbtv:/data# root@volumes-nfs-6796b4b755-xlbtv:/data# exit exit [root@master1 storage]#
#结果显示:基于nfs网络文件存储实现了更强大的文件共享效果
浙公网安备 33010602011771号