Persistent Volume and Persistent Volume Claims- Kubernetes

Joan Ngugi
3 min readMar 29, 2020

Volumes in Kubernetes can be thought of as a directory that is accessible to the containers in a pod. Volumes help you persist data even if your container restarts. There are different types of volumes in Kubernetes and the type defines how the volume is created and it’s content.

Persistent Volume

A persistent volume is a piece of storage in a Kubernetes cluster. PersistentVolumes are a cluster-level resource like nodes, which don’t belong to any namespace. It is provisioned by the administrator and has a particular file size. This way, a developer deploying their app on Kubernetes need not know the underlying infrastructure. When the developer needs a certain amount of persistent storage for their application, the system administrator configures the cluster so that they consume the PersistentVolume provisioned in an easy way.

Creating Persistent Volume

kind: PersistentVolume
apiVersion: v1
metadata:
name:pv01
spec:
capacity: # defines the capacity of PV we are creating
storage: 10Gi #the amount of storage we are tying to claim
accessModes: # defines the rights of the volume we are creating
- ReadWriteOnce
hostPath:
path: "/tmp/data01" # path to which we are creating the volume

Challenge

  1. Create a Persistent Volume named pv, with access mode ReadWriteMany, storage classname shared, 512MB of storage capacity and the host path /data/config.

2. Save the file and create the persistent volume.

3. View the persistent volume.

  • Our persistent volume status is available meaning it is available and it has not been mounted yet. This status will change when we mount the persistentVolume to a persistentVolumeClaim.

PersistentVolumeClaim

In a real ecosystem, a system admin will create the PersistentVolume then a developer will create a PersistentVolumeClaim which will be referenced in a pod. A PersistentVolumeClaim is created by specifying the minimum size and the access mode they require from the persistentVolume.

Challenge

  1. Create a Persistent Volume Claim that requests the Persistent Volume we had created above. The claim should request 256MB. Ensure that the Persistent Volume Claim has the same storageClassName as the persistentVolume you had previously created.

2. Save and create the pvc

3. View the pvc

4. Let’s see what has changed in the pv we had initially created.

Our status has now changed from available to bound.

5. Create a new pod named myapp with image nginx that will be used to Mount the Persistent Volume Claim with the path /var/app/config.

Mounting a Claim

apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
name: app
spec:
volumes:
- name:congigpvc
persistenVolumeClaim:
claimName: pvc # reference the pvc name
containers:
- image: nginx
name: app
volumeMounts:
- mountPath: "/data/app/config"
name: configpvc

--

--