Kubernetes Persistent Volumes

A PersistentVolume (PV) is a piece of storage in a Kubernetes cluster, provisioned by an administrator or dynamically provisioned using a Storage Class. Its lifecycle is independent of any individual Pod that uses it.

How to Manually Create a PV

Below is the manifest for manually creating a PV.

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: The name of the StorageClass. Once a PV has this field set, it can only be bound by a PVC that requests that same StorageClass name. If not specified, it can be bound by any StorageClass.

capacity: The storage capacity of the PV. Currently only specifying storage size is supported.

accessModes: The access modes of the PV. Supported modes are ReadWriteOnce (RWO), ReadOnlyMany (ROX), and ReadWriteMany (RWX). A volume in a Pod can only use one access mode at a time, even if the PV supports multiple modes.

persistentVolumeReclaimPolicy: The reclaim policy for the PV. Supported policies are Retain (default for manually created PersistentVolumes), Delete (default for dynamically provisioned PersistentVolumes), and Recycle (deprecated).

Create

1
kubectl apply -f https://raw.githubusercontent.com/chengqing-su/kubernetes-learning/master/volumes/pv.yaml

Running kubectl get pv task-pv-volume -o yaml produces the following output:

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 Phases

In the output above, status.phase indicates the current phase of the PV. The possible phases are:

  • Available: The PV has not yet been bound to a PersistentVolumeClaim (PVC).

  • Bound: The PV has been bound to a PersistentVolumeClaim (PVC).

  • Released: The PersistentVolumeClaim (PVC) that was bound to this PV has been deleted, but the resources associated with the PV have not yet been reclaimed.

  • Failed: The PV has failed its automatic reclamation.