#90daysofdevops #36: Managing Persistent Volumes in Your Deployment

What are Persistent Volumes in k8s

In Kubernetes, a Persistent Volume (PV) is a piece of storage in the cluster that has been provisioned by an administrator. A Persistent Volume Claim (PVC) is a request for storage by a user. The PVC references the PV, and the PV is bound to a specific node. Read official documentation of Persistent Volumes.

Task 1:

Add a Persistent Volume to your Deployment todo app.

  • Create a Persistent Volume using a file on your node. Template

  • Create a Persistent Volume Claim that references the Persistent Volume. Template

  • Update your deployment.yml file to include the Persistent Volume Claim. After Applying pv.yml pvc.yml your deployment file look like this Template

  • Apply the updated deployment using the command: kubectl apply -f deployment.yml

  • Verify that the Persistent Volume has been added to your Deployment by checking the status of the Pods and Persistent Volumes in your cluster. Use this commands kubectl get pods ,

kubectl get pv

⚠️ Don't forget: To apply changes or create files in your Kubernetes deployments, each file must be applied separately. ⚠️

To ensure data persistence in your Deployment, you can add a Persistent Volume.

Here's an example pv.yml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-todo-app
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: "/tmp/data"

This PV definition specifies a capacity of 1Gi, with read-write access for a single node. It uses a hostPath to store the data on the node's local filesystem at /tmp/data.

Create a Persistent Volume Claim (PVC) using a YAML file. Here's an example pvc.yml:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-todo-app
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi

This PVC definition requests 500Mi of storage with read-write access. It will match the PV based on the access mode and capacity.

Update your deployment.yml file to include the Persistent Volume Claim (PVC). Here's an updated snippet:

 apps/v1
kind: Deployment
metadata:
  name: node-app
spec:
  replicas: 5
  selector:
    matchLabels:
      app: node-app
  template:
    metadata:
      labels:
        app: node-app
    spec:
      containers:
      - name: node-app
        image: patelajay745/node-app-new:latest
        ports:
        - containerPort: 3000
        env:
        - name: MY_SECRET
          valueFrom:
            secretKeyRef:
              name: my-secret
              key: my-credentials.txt
        - name: MY_CONFIG
          valueFrom:
            configMapKeyRef:
              name: my-config
              key: my-settings.conf
        volumeMounts:
        - name: data-volume
          mountPath: /app/data
  volumes:
    - name: data-volume
      persistentVolumeClaim:
        claimName: pvc-todo-app

In this updated deployment.yml file, we added the volumeMounts and volumes sections. The volumeMounts section specifies the mount path within the container, while the volumes section references the PVC.

Apply the updated deployment using the command: kubectl apply -f deployment.yml

Verify that the Persistent Volume has been added to your Deployment by checking the status of the Pods and Persistent Volumes in your cluster using the following commands: kubectl get pods and kubectl get pv

Task 2: Accessing data in the Persistent Volume

Connect to a Pod in your Deployment using the following command: kubectl exec -it <pod-name> -- /bin/bash (Replace <pod-name> with the actual name of your Pod)

Once inside the Pod's shell, navigate to the directory where the data is stored. In this case, it will be mounted at /app/data based on the deployment.yml file.

Verify that you can access the data by listing the files and directories within the mounted volume. You can use commands such as ls, cat, or vi to view the contents of specific files.

By following these steps and the example provided, you can successfully manage Persistent Volumes in your Deployment and access the data stored within them.