1、部署過程
1.1、環境依賴
在部署nfs-client-provisioner之前,需要先部署nfs服務。
因為,nfs-client-provisioner創建的pv都是要在nfs服務器中搭建的。
本示例中的nfs server的地址如下:
[root@node1 /]# showmount -e
Export list for node1:
/data/nfs/rw 192.168.20.0/24
1.2、在集群的每個節點部署nfs工具
執行以下的命令,在每個節點部署nfs工具,后面pod啟動,掛載nfs的存儲會使用
yum install nfs-utils -y
1.3、 創建授權賬戶信息
通過以下yaml文件,創建需要的service account
創建 rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:name: nfs-client-provisioner# replace with namespace where provisioner is deployednamespace: default
---
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: ServiceAccountname: nfs-client-provisioner# replace with namespace where provisioner is deployednamespace: default
roleRef:kind: ClusterRolename: nfs-client-provisioner-runnerapiGroup: 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 deployednamespace: 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 deployednamespace: default
subjects:- kind: ServiceAccountname: nfs-client-provisioner# replace with namespace where provisioner is deployednamespace: default
roleRef:kind: Rolename: leader-locking-nfs-client-provisionerapiGroup: rbac.authorization.k8s.io
kubectl apply -f rbac.yaml
1.4、部署nfs-client-provisioner服務
創建nfs_client_provisioner.yaml
kind: Deployment
apiVersion: apps/v1
metadata:name: nfs-client-provisioner
spec:replicas: 1selector:matchLabels:app: nfs-client-provisionerstrategy:type: Recreatetemplate:metadata:labels:app: nfs-client-provisionerspec:serviceAccountName: nfs-client-provisionercontainers:- name: nfs-client-provisionerimage: quay.io/vbouchaud/nfs-client-provisioner:latestvolumeMounts:- name: nfs-client-rootmountPath: /persistentvolumesenv:- name: PROVISIONER_NAMEvalue: fuseim.pri/ifs- name: NFS_SERVERvalue: 192.168.20.11- name: NFS_PATHvalue: /data/nfs/rwvolumes:- name: nfs-client-rootnfs:server: 192.168.20.11path: /data/nfs/rw
創建: kubectl apply -f nfs_client_provisioner.yaml 查看nfs-client pod的狀態:kubectl get pod | grep nfs
1.5創建storageclass
動態存儲的關聯關系,都是通過stroageclass來完成的。通過下面的yaml文件,創建storageclass
創建nfs-storage-class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:name: managed-nfs-storage
provisioner: fuseim.pri/ifs
parameters:archiveOnDelete: "false"
創建: kubectl apply -f nfs-storage-class.yaml
查詢: kubectl get sc
1.6、創建pvc
創建nfs-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:name: test-claimannotations:volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:accessModes:- ReadWriteManyresources:requests:storage: 10Mi
執行:?kubectl apply -f nfs-pvc.yaml
查詢:kubectl get pvc
這個時候會在nfs的目錄創建出一個目錄。這個目錄就是pv的目錄
1.7、創建pod使用pvc
pvc已經創建好了,創建測試pod,使用pvc
創建test-pod.yaml 文件
kind: Pod
apiVersion: v1
metadata:name: test-pod
spec:containers:- name: test-podimage: busybox:1.36command:- "/bin/sh"args:- "-c"- "touch /mnt/SUCCESS && exit 0 || exit 1"volumeMounts:- name: nfs-pvcmountPath: "/mnt"restartPolicy: "Never"volumes:- name: nfs-pvcpersistentVolumeClaim:claimName: test-claim
kubectl apply -f? ?test-pod.yaml
進入nfs server的目錄里,已經創建好了SUCCESS文件
OK,到這里nfs-client-provisioner工具部署成功。
部署完成nfs-client-provisioner之后,創建好pvc,就可以動態的創建nfs的pv了,不需要手動的進行創建。
2、工作原理簡要說明
基本的工作原理就是,nfs-client-provisioner持續的監控api server,查看創建pvc的請求,如果發現請求的pvc的存儲類stroageclass,存儲類的提供者和自己的是一致的就會根據創建的pvc,動態的在nfs中創建pv。
關于nfs-client-provisioner工作流程的描述:
- 部署 nfs-client-provisioner: 首先,您需要在 Kubernetes 集群中部署 nfs-client-provisioner。您可以通過使用 Kubernetes 配置文件或 Helm Chart 進行部署。這將創建一個運行在集群中的容器,用于監聽 PVC 的創建。
- 創建 StorageClass:?您需要創建一個?StorageClass,其中定義了與?nfs-client-provisioner?通信所需的配置信息,如 NFS 服務器地址、共享路徑等。
- 創建 PersistentVolumeClaim(PVC):?當您在集群中創建一個 PVC 并引用了之前定義的?StorageClass,nfs-client-provisioner?將監聽 PVC 的創建。
- nfs-client-provisioner 的處理:?一旦 PVC 創建,nfs-client-provisioner?會檢測到 PVC 的存在。它會解析 PVC 中定義的存儲需求和?StorageClass?的配置,然后使用這些信息來與 NFS 服務器交互。
- 創建 PersistentVolume(PV):nfs-client-provisioner?會使用 PVC 請求中的信息,以及?StorageClass?的配置,通過 NFS 協議與 NFS 服務器進行通信,并在 NFS 服務器上創建一個目錄以供存儲。
- 綁定 PV 和 PVC:?一旦 NFS 服務器上的目錄創建成功,nfs-client-provisioner?將創建一個 PV,并將其與創建 PVC 綁定起來,從而使 PVC 獲得一個可用的 PV。
- PVC 使用: 現在,PVC 已經與一個動態創建的 PV 綁定。您可以在 Pod 中使用這個 PVC,使 Pod 能夠掛載 NFS 存儲并訪問其中的數據。
通過這個流程,nfs-client-provisioner 實現了動態創建和管理基于 NFS 的 PV,減輕了管理員的工作負擔,同時使開發人員能夠更輕松地使用動態的存儲資源。需要注意的是,確保您在 PVC 和 StorageClass 的定義中提供正確的配置信息,以確保 nfs-client-provisioner 能夠與 NFS 服務器正確交互。