NFS-Client-Provisioner部署
部署NFS-Client Provisioner的初衷,就是为了根据PVC的需求自动创建符合要求的PV。
首先,必须拥有自己的NFS Server, 而且k8s集群能够正常访问之。
之后,在k8s master上应用以下yaml文件:
1 RBAC.yaml
apiVersion: v1kind: ServiceAccountmetadata: name: nfs-client-provisioner # replace with namespace where provisioner is deployed namespace: nfs-client-provisioner---kind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1metadata: name: nfs-client-provisioner-runnerrules: - 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: ClusterRoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata: name: run-nfs-client-provisionersubjects: - kind: ServiceAccount name: nfs-client-provisioner # replace with namespace where provisioner is deployed namespace: nfs-client-provisionerroleRef: kind: ClusterRole name: nfs-client-provisioner-runner apiGroup: rbac.authorization.k8s.io---kind: RoleapiVersion: rbac.authorization.k8s.io/v1metadata: name: leader-locking-nfs-client-provisioner # replace with namespace where provisioner is deployed namespace: nfs-client-provisionerrules: - apiGroups: [""] resources: ["endpoints"] verbs: ["get", "list", "watch", "create", "update", "patch"]---kind: RoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata: name: leader-locking-nfs-client-provisionersubjects: - kind: ServiceAccount name: nfs-client-provisioner # replace with namespace where provisioner is deployed namespace: nfs-client-provisionerroleRef: kind: Role name: leader-locking-nfs-client-provisioner apiGroup: rbac.authorization.k8s.io
2 Deployment.yaml
apiVersion: apps/v1kind: Deploymentmetadata: name: nfs-client-provisioner labels: app: nfs-client-provisioner # replace with namespace where provisioner is deployed namespace: defaultspec: replicas: 1 selector: matchLabels: app: nfs-client-provisioner 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: quay.io/external_storage/nfs-client-provisioner:latest volumeMounts: - name: nfs-client-root mountPath: /persistentvolumes env: - name: PROVISIONER_NAME value: zbb.test/nfs - name: NFS_SERVER value: 10.0.0.31 - name: NFS_PATH value: /netshare volumes: - name: nfs-client-root nfs: server: 10.0.0.31 path: /netshare
注意修改env和volumes中关于NFS Server的参数
3 storageclass.yaml
apiVersion: storage.k8s.io/v1kind: StorageClassmetadata: name: managed-nfs-storageprovisioner: zbb.test/nfs # or choose another name, must match deployment's env PROVISIONER_NAME'parameters: archiveOnDelete: "false"
注意:
1) storageclass中的provisioner必须与deployment中的定义一致!
2) 如果不把nfs-client-provisioner部署到默认名称空间中,需要将"# replace with namespace where provisioner is deployed"标注的namespace全部改为目标namespace名称
3)Storagelass的名称可以修改成自己想要的
查看已部署的Storagelass:
# kubectl get storageclass# kubectl get cs
(参考<2>)将managednfs-storage这个storageclass设置为kubernetes的默认存储后端:
# kubectl patch storageclass managed-nfs-storage -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
由此, 部署完成, 下面给出个测试示例:
4 test-pvc.yaml
kind: PersistentVolumeClaimapiVersion: v1metadata: name: test-claim annotations: volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"spec: accessModes: - ReadWriteMany resources: requests: storage: 1Mi
5 test-pod.yaml
kind: PodapiVersion: v1metadata: name: test-podspec: containers: - name: test-pod image: busybox:1.24 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动态供给PV啦,不需要手工创建咯
测试平台:kubernetes 1.16.3
OS: CentOS Linux release 7.7.1908 (Core)
参考资料:
<1> https://github.com/kubernetes-incubator/external-storage/tree/master/nfs-client
<2> https://www.cnblogs.com/robinunix/p/11133032.html
在此向参考资料的作者表示感谢!!