K8s – Question6

Use context: kubectl config use-context k8s-c1-H

Create a new PersistentVolume named safari-pv. It should have a capacity of 2Gi, accessMode ReadWriteOnce, hostPath /Volumes/Data and no storageClassName defined.

Next create a new PersistentVolumeClaim in Namespace project-tiger named safari-pvc . It should request 2Gi storage, accessMode ReadWriteOnce and should not define a storageClassName. The PVC should bound to the PV correctly.

Finally create a new Deployment safari in Namespace project-tiger which mounts that volume at /tmp/safari-data. The Pods of that Deployment should be of image httpd:2.4.41-alpine.

Setting Up Persistent Storage in Kubernetes with PersistentVolume and PersistentVolumeClaim

In Kubernetes, managing persistent storage is crucial for stateful applications. This guide will walk you through creating a PersistentVolume (PV) and PersistentVolumeClaim (PVC), and then using them in a Deployment to ensure your data persists across Pod restarts.

Step 1: Create the PersistentVolume

First, we’ll create a PersistentVolume by editing a YAML file. Find an example from the official Kubernetes documentation and modify it to suit our needs:

Edit the file as follows:

Once the file is ready, create the PersistentVolume with the following command:

Step 2: Create the PersistentVolumeClaim

Next, we’ll create a PersistentVolumeClaim to bind to our PV. Again, find an example from the official documentation and modify it:

Edit the file as follows:

Create the PVC with the following command:

You can check if the PV and PVC are bound correctly by running:

You should see output indicating that both the PV and PVC are in the “Bound” state:

Step 3: Create a Deployment and Mount the Volume

Now, we’ll create a Deployment and mount the PVC to a specific path in the container. Start by generating the Deployment YAML:

te d # 6_dep.yaml apiVersion: apps/v1 kind: Deployment metadata: name: safari namespace: project-tiger spec: replicas: 1 selector: matchLabels: app: safari template: metadata: labels: app: safari spec: volumes: # add – name: data # add persistentVolumeClaim: # add claimName: safari-pvc # add containers: – image: httpd:2.4.41-alpine name: container volumeMounts: # add – name: data # add mountPath: /tmp/safari-data # add

Create the Deployment with the following command:

Step 4: Verify the Volume Mount

Finally, confirm that the volume is correctly mounted in the Pod by describing the Pod:

You should see the mount path listed in the output, indicating that the volume is properly mounted:

In this guide, we walked through creating a PersistentVolume, binding it to a PersistentVolumeClaim, and mounting it in a Deployment. This ensures that your application has persistent storage that remains available even if the Pod is restarted.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *