How to Add Persistent Volume to Pods in Google Kubernetes Engine

If you want to preserve the data even after a pod deletion or pod failures, you should use persistent volumes. For GKE, you have to option to create google cloud persistent disk and use it as a persistent volumes for the pods.

Setup Persistent Volume For GKE

Note: When using persistent Volume, only one replica will be able to do read-write operation. With more than one replica, you can only use the PD in the read mode.

In this article, I have added to steps to setup persistent volume for the pods running in GKE.

Before deploying the pods, we should create a storage class

The following config will create a storage class gold with gce-pd as the volume provisioner.

Now, we will create a persistentVolumeClaim (PVC). We will use this volume claim in our deployment config.

The following config will create a PVC named jenkins-data in jenkins namespace under gold storage class that we have created first. You can change these names accordingly.

In the Kubernetes deployment config, you can use the newly created PVC using the following volume definition.

Example Deployment Config With Persistent Volume Claim

A full Jenkins container definition using persistent volume is shown below.

Under container spec I defined the volume name and mount path required for the container.

Under Volumes definition, I have mentioned the volume name and our newly created persistent disk for mounting it to the pod.

READ  Jenkins Tutorial For Beginners - Getting Started Guide

This deployment creates a Jenkins pod with all its data mounted to the persistent volume. So, even if you delete the pod, a new pod will come up and mount itself to the persistent volume. So you will see the new Jenkins pod with all the old data and configurations.

Hope this article helps. Let me know in the comment section, if you face any issues,


Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.