1、Kubernetes集群管理员通过提供不同的存储类,可以满足用户不同的服务质量级别、备份策略和任意策略要求的存储需求。动态存储卷供应使用StorageClass进行实现,其允许存储卷按需被创建。如果没有动态存储供应,Kubernetes集群的管理员将不得不通过手工的方式类创建新的存储卷。通过动态存储卷,Kubernetes将能够按照用户的需要,自动创建其需要的存储。
基于StorageClass的动态存储供应整体过程如下图所示:
1)集群管理员预先创建存储类(StorageClass);
2)用户创建使用存储类的持久化存储声明(PVC:PersistentVolumeClaim);
3)存储持久化声明通知系统,它需要一个持久化存储(PV: PersistentVolume);
4)系统读取存储类的信息;
5)系统基于存储类的信息,在后台自动创建PVC需要的PV;
6)用户创建一个使用PVC的Pod;
7)Pod中的应用通过PVC进行数据的持久化;
8)而PVC使用PV进行数据的最终持久化处理。
2、example
首先创建nfs服务,参考另外一篇文章:http://www.cnblogs.com/cuishuai/p/7798154.html
1)创建rbac授权,(如果集群开启了rbac)
# cat nfs-rbac.yaml
--- apiVersion: v1 kind: ServiceAccount metadata: name: nfs-provisioner namespace: monitoring --- kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1 metadata: name: nfs-provisioner-runner namespace: monitoring 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: ["watch", "create", "update", "patch"] - apiGroups: [""] resources: ["services", "endpoints"] verbs: ["get","create","list", "watch","update"] - apiGroups: ["extensions"] resources: ["podsecuritypolicies"] resourceNames: ["nfs-provisioner"] verbs: ["use"] --- kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: run-nfs-provisioner subjects: - kind: ServiceAccount name: nfs-provisioner namespace: monitoring roleRef: kind: ClusterRole name: nfs-provisioner-runner apiGroup: rbac.authorization.k8s.io
2)创建nfs的nfs-client-provisioner
# cat nfs-deployment.yaml
kind: Deployment apiVersion: extensions/v1beta1 metadata: name: nfs-client-provisioner namespace: monitoring spec: replicas: 1 strategy: type: Recreate template: metadata: labels: app: nfs-client-provisioner spec: serviceAccount: nfs-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: fuseim.pri/ifs - name: NFS_SERVER value: 10.2.68.77 - name: NFS_PATH value: /data/opv volumes: - name: nfs-client-root nfs: server: 10.2.68.77 path: /data/opv
!!PS:10.2.68.77是nfs服务的监听地址,/data/opv是nfs共享的目录。
3)创建storageclass
# cat storageclass-nfs.yaml
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: grafana-nfs namespace: monitoring provisioner: fuseim.pri/ifs reclaimPolicy: Retain
reclaimPolicy:有两种策略:Delete、Retain。默认是Delet
!!PS:fuseim.pri/ifs为上面deployment上创建的PROVISIONER_NAME。
4)创建PersistenetVolumeClaim
在存储类被正确创建后,就可以创建PersistenetVolumeClaim来请求StorageClass,而StorageClass将会为PersistenetVolumeClaim自动创建一个可用PersistentVolume。PersistenetVolumeClaim是对PersistenetVolume的声明,即PersistenetVolume为存储的提供者,而PersistenetVolumeClaim为存储的消费者。
# cat pvc.yaml
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: test-claim namespace: monitoring # annotations: # volume.beta.kubernetes.io/storage-class: "grafana-nfs" spec:
storageClassName: grafana-nfs accessModes: - ReadWriteMany resources: requests: storage: 1Mi
!!PS:grafana-nfs为上面创建的storageclass的name,1Mi是设置的目录的大小。
5)testpod
kind: Pod apiVersion: v1 metadata: name: test-pod namespace: monitoring spec: containers: - name: test-pod image: busybox 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