PersistentVolume(PV)是Kubernetes集群中的一块存储,由管理员创建或使用存储类动态创建。其生命周期独立于使用PV的任何单个Pod。
如何手动创建一个PV
下面是手动创建PV的manifest。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   | apiVersion: v1 kind: PersistentVolume metadata:   name: task-pv-volume   labels:     type: local spec:   storageClassName: hostpath   capacity:     storage: 10Gi   accessModes:     - ReadWriteOnce   hostPath:     path: "/tmp/data"
   | 
storageClassName: StorageClass的名称。当一个PV配置了该字段之后,该PV就只能被使用该名称的PVC绑定。如果没有指定,则可以被任意StroageClass绑定。
capacity: PV的存储能力,目前仅支持指定存储大小。
accessModes:PV的访问模式。支持ReadWriteOnce(RWO)、ReadOnlyMany(ROX)、ReadWriteMany(RWX).Pod中的volume只能使用一个访问模式,即使PV支持多种模式。
persistentVolumeReclaimPolicy:PV回收策略。支持Retain (手动创建的PersistentVolumes的默认值), Delete (动态创建的PersistentVolumes的默认值), and Recycle (已过时).
创建
1
   | kubectl apply -f https://raw.githubusercontent.com/chengqing-su/kubernetes-learning/master/volumes/pv.yaml
   | 
执行kubectl get pv task-pv-volume -o yaml结果如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
   | apiVersion: v1 kind: PersistentVolume metadata:   annotations:     kubectl.kubernetes.io/last-applied-configuration: |       {"apiVersion":"v1","kind":"PersistentVolume","metadata":{"annotations":{},"labels":{"type":"local"},"name":"task-pv-volume"},"spec":{"accessModes":["ReadWriteOnce"],"capacity":{"storage":"10Gi"},"hostPath":{"path":"/tmp/data"},"storageClassName":"hostpath"}}   creationTimestamp: "2020-03-25T14:45:48Z"   finalizers:   - kubernetes.io/pv-protection   labels:     type: local   name: task-pv-volume   resourceVersion: "4540164"   selfLink: /api/v1/persistentvolumes/task-pv-volume   uid: 8d0c4711-38f7-456c-a402-14c34ccf2df3 spec:   accessModes:   - ReadWriteOnce   capacity:     storage: 10Gi   hostPath:     path: /tmp/data     type: ""   persistentVolumeReclaimPolicy: Retain   storageClassName: hostpath   volumeMode: Filesystem status:   phase: Available
   | 
PV的阶段
在上面的结果中,status.phase表示PV的所处的阶段。PV的阶段如下:
Available: 该PV没有被PersistentVolumeClaim(PVC)绑定。
Bound: 该PV已经被PersistentVolumeClaim(PVC)绑定。
Released: 绑定该PV的PersistentVolumeClaim(PVC)已经被删除,但是该PV对应的资源还没有被回收。
Failed: 该PV自动回收失败