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.
kind: StorageClass apiVersion: storage.k8s.io/v1 metadata: name: gold provisioner: kubernetes.io/gce-pd parameters: type: pd-ssd
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.
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: jenkins-data namespace: jenkins spec: accessModes: - ReadWriteOnce resources: requests: storage: 50Gi storageClassName: gold
In the Kubernetes deployment config, you can use the newly created PVC using the following volume definition.
volumes: - name: jenkins-persistent-storage persistentVolumeClaim: claimName: jenkins-data
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.
[irp posts=”647″ name=”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.
apiVersion: extensions/v1beta1 # for versions before 1.7.0 use apps/v1beta1 kind: Deployment metadata: name: jenkins-deployment namespace: jenkins spec: replicas: 1 selector: matchLabels: app: jenkins template: metadata: labels: app: jenkins spec: containers: - name: jenkins image: bibinwilson/priveleged-jenkins ports: - containerPort: 8080 volumeMounts: - name: jenkins-persistent-storage mountPath: /var/jenkins_home volumes: - name: jenkins-persistent-storage persistentVolumeClaim: claimName: jenkins-data
Hope this article helps. Let me know in the comment section, if you face any issues,