使用nfs作为kubernetes动态storageClass存储
1.使用nfs作为kubernetes动态storageClass存储
使用 nfs 作为kubernetes动态 storageClass 存储
1、StorageClass介绍
参考网址:https://github.com/kubernetes-retired/external-storage
https://blog.51cto.com/xuexinhuan/5394844
StorageClass对象会定义下面两部分内容:
1: PV的属性.比如,存储类型,Volume的大小等
2: 创建这种PV需要用到的存储插件
有了这两个信息之后,Kubernetes 就能够根据用户提交的 PVC,找到一个对应的 StorageClass,之后Kubernetes 就会调用该 StorageClass 声明的存储插件,进而创建出需要的 PV。
但是其实使用起来是一件很简单的事情,你只需要根据自己的需求,编写YAML文件即可,然后使用kubectl create命令执行即可。
创建StorageClass流程
- 创建一个可用的 NFS Serve
- 创建 Service Account. 这是用来管控 NFS provisioner 在k8s集群中运行的权限
- 创建 StorageClass. 负责建立PVC并调用 NFS provisioner 进行预定的工作, 并让 PV 与 PVC 建立管理
- 创建 NFS provisioner. 有两个功能,一个是在 NFS 共享目录下创建挂载点(volume), 另一个则是建了 PV 并将 PV 与 NFS 的挂载点建立关联
2、安装配置 NFS Serve
安装:
yum install -y nfs-utils
mkdir /data/nfs-volume
chmod 755 /data/nfs-volume
#chown nfsnobody:nfsnobody /data/nfs-volume
echo "/data/nfs-volume 192.168.1.0/24(rw,sync,all_squash)" > /etc/exports
systemctl enable nfs
systemctl start nfs
验证共享存储是否生效:
showmount -e
3、创建account及相关权限
创建external_storage命名空间
kubectl create ns external-storage
创建RBAC权限,nfs-client-rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: external-storage
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
rules:
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: run-nfs-client-provisioner
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: external-storage
roleRef:
kind: ClusterRole
name: nfs-client-provisioner-runner
apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: external-storage
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: external-storage
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: external-storage
roleRef:
kind: Role
name: leader-locking-nfs-client-provisioner
apiGroup: rbac.authorization.k8s.io
创建基于NFS资源的StorageClass
nfs-client-class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage
provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
archiveOnDelete: "false"
创建NFS provisioner Deployment
nfs-client-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-client-provisioner
labels:
app: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: external-storage
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: nfs-client-provisioner
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: registry.cn-beijing.aliyuncs.com/mydlq/nfs-subdir-external-provisioner:v4.0.0
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: fuseim.pri/ifs
- name: NFS_SERVER
value: 192.168.1.66
- name: NFS_PATH
value: /data/nfs-volume
volumes:
- name: nfs-client-root
nfs:
server: 192.168.1.66
path: /data/nfs-volume
依次应用资源配置清单:
kubectl apply -f nfs-client-rbac.yaml
kubectl apply -f nfs-client-class.yaml
kubectl apply -f nfs-client-deployment.yaml
4、创建PVC,POD验证
创建pvc,并验证:
nfs-client-pvc-test.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-claim
annotations:
volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Mi
如图所示:
创建测试pod,查看是否可以正常挂载
nfs-client-pod-test.yaml
kind: Pod
apiVersion: v1
metadata:
name: test-pod
spec:
containers:
- name: test-pod
image: busybox:1.36.0
command:
- "/bin/sh"
args:
- "-c"
- "touch /mnt/SUCCESS && exit 0 || exit 1"
volumeMounts:
- name: nfs-pvc
mountPath: "/mnt"
restartPolicy: "Never"
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: test-claim
验证:在nfs服务器验证
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示