Persistent Volume and Persistent Volume Claims- Kubernetes

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

2. Save the file and create the persistent volume.

3. View the persistent volume.

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

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

--

--

Big Data & Analytics, Data Science, Machine Learning, Data Engineering | ngugijoan.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Joan Ngugi

Big Data & Analytics, Data Science, Machine Learning, Data Engineering | ngugijoan.com