Kubernetes部署nfs-provisioner 实现PV 动态供给(StorageClass)

nfs搭建可参考:
https://blog.csdn.net/weixin_48711696/article/details/135703378?spm=1001.2014.3001.5502

一、部署nfs-orivisioer

nfs-provisioner项目地址:
https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/tree/master/deploy
分别下载class.yaml,deployment.yaml,rabc.yaml. 也可以直接从下面复制粘贴
class.yaml 创建存储类

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-client   #存储类名字
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
  archiveOnDelete: "false"  #pvc删除后,pv不删除(nfs共享目录不删除),设置true会删除

deployment.yaml 部署nfs-client-provisioner
这里除了nfs配置信息还改了镜像地址,因为默认的镜像下载地址在国外。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
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: zhaoguanghui6/nfs-client-provisioner:v4.0.1 
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: k8s-sigs.io/nfs-subdir-external-provisioner
            - name: NFS_SERVER
              value: 10.3.243.101 #nfs服务器IP,改成自己的
            - name: NFS_PATH
              value: /ifs/kubernetes  #nfs服务器共享目录
      volumes:
        - name: nfs-client-root
          nfs:
            server: 10.3.243.101  #nfs服务器IP
            path: /ifs/kubernetes   #nfs服务器共享目录

rabc.yaml 授权(连接k8s-api)

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
kubectl apply -f class.yaml
kubectl apply -f deployment.yaml
kubectl apply -f rabc.yaml

二 、创建pvc测试

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc2  
spec: 
  storageClassName: "nfs-client"  #存储类的名字,只需要加这一个参数
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi