基于nfs创建StorageClass

发布时间 2023-10-26 12:00:21作者: 王又又的锅
系统 Cetnos7
kubernetes v1.28.0
nfs外部制备器 nfs-subdir-external-provisioner

一、制备器选择

1.每个StorageClass都有一个制备器(Provisioner),用来决定使用哪个卷插件制备 PV,本文基于NFS搭建StorageClass,NFS没有内部制备器,但可以使用外部制备器。也有第三方存储供应商提供自己的外部制备器

image

二、准备制备器NFS Provisioner(制备器可以根据pvc的声明向配置的nfs服务器进行)

1.设置授权RBAC

kubectl apply -f rbac-storageclass.yaml

rbac-storageclass.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-client-provisioner-runner
rules:
  - apiGroups: [""]
    resources: ["nodes"]
    verbs: ["get", "list", "watch"]
  - 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: default
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: default
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: default
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    namespace: default
roleRef:
  kind: Role
  name: leader-locking-nfs-client-provisioner
  apiGroup: rbac.authorization.k8s.io

2、部署nfs provisioner程序

kubectl apply -f nfs-provisioner-deployment.yaml

nfs-provisioner-deployment.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nfs-client-provisioner
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nfs-client-provisioner
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: registry.k8s.io/sig-storage/nfs-subdir-external-provisioner:v4.0.2
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: nfs-client-provisionerr #自己的名字
            - name: NFS_SERVER
              value: 192.168.2.41 #改为自己nfs服务地址
            - name: NFS_PATH
              value: /data #改为自己路径
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.2.41
            path: /data

**可根据自己k8s版本镜像更换为:quay.io/external_storage/nfs-client-provisioner:latest
如使用该镜像,k8sv1.20以上版本关闭了SelfLink
可能会报错:class "nfs": unexpected error getting claim reference: selfLink was empty, can't make reference
需要再api-server启动参数加上(v1.28.0好像已经不支持了,使用nfs-subdir-external-provisioner)
--feature-gates=RemoveSelfLink=false

三、安装StorageClass

kubectl apply -f nfs-storages.yaml

nfs-storage-class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: example-nfs #pod使用storageclass时填的名字 
provisioner: nfs-client-provisioner #和nfs外部制备器名字相同
parameters:
  server: 192.168.2.41 #nfs服务地址
  path: /data
  readOnly: "false"

四、测试验证

1.测试

kubectl apply -f test-PVC.yaml

test-PVC.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: test-claim
spec:
  storageClassName: example-nfs
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

2.验证(我是建在默认表空间,其他表空间请用 -n namespace 指定)

kubectl get pv
kubectl get pvc
image

参考:

官方文档:https://kubernetes.io/zh-cn/docs/concepts/storage/dynamic-provisioning/
官方建议参考nfs-storageslass github地址:https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner
创建制备器过程中问题解答:https://www.cnblogs.com/Eddy24/articles/16955989.html