In the following example, we will use Docker Desktop Kubernetes cluster, and we will create a hostPath Persistent Volume.
A hostPath Persistent Volume allows mounting files and directories on the Kubernetes Node.
This is helpful for development and testing on a single-node cluster.
Defines the Persistent Volume:
$ vi persistentVolume1.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv1
labels:
type: local
spec:
storageClassName: hostpath
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
hostPath:
path: /run/desktop/mnt/host/c/dev/dockerdesktop/k8s/storage/pv1/
On Docker Desktop the path should be:
/run/desktop/mnt/host/c/CUSTOME_STORAGE_PATH
Persistent Volumes are defined in the core API group, so the
apiVersion field can skip the api group.
A Persistent Volume can be created in one access mode (you cannot combine RWO and RWM).
The Persistent Volume Claim will mount/bind the Persistent Volume with the same access mode.
The "
spec.accessModes" field defines the access mode to the Persistent Volume (how it will be mounted):
ReadWriteOnce (RWO),
ReadWriteMany (RWM),
ReadOnlyMany (ROM).
-
ReadWriteOnce (RWO):
Defines a Persistent Volume with the read/write access mode and can be mounted/bound by one single Persistent Volume Claim.
If multiple Persistent Volume Claims tries to use the Persistent Volume, then only one will be bound and the remaining PVCs will be in status pending.
-
ReadWriteMany (RWM):
Defines a Persistent Volume with the read/write access mode and can be mounted/bound by multiple Persistent Volume Claims (NFS storage).
-
ReadOnlyMany (ROM):
Defines a Persistent Volume with the read only access mode and can be mounted/bound by multiple Persistent Volume Claims.
The "
spec.storageClassName" field defines the name of the storage class.
The "
spec.capacity.storage" field defines size of the volume (must not be larger than the size of the backend storage).
The "
spec.persistentVolumeReclaimPolicy" field defines how the data will managed when the Persistent Volume Claim is deleted: Retain, Delete.
-
Retain:
Deleting the Persistent Volume Claims won't delete the associated data stored in the external storage system.
-
Delete:
Deleting the Persistent Volume Claims will also delete the associated data stored in the external storage system.
To apply the file:
$ kubectl apply -f persistentVolume1.yaml
persistentvolume/pv1 created
View the Persistent Volume:
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 1Gi RWO Retain Available hostpath 4s
The "STATUS" of the Persistent Volume is "Available",
which means it has not yet been bound to a Persistent Volume Claim.