Skip to main content

Command Palette

Search for a command to run...

Day36 of #90DaysOfDevOps

Managing Persistent Volumes in Your Deployment 💥

Published
•3 min read•View as Markdown
A

Hi there! I'm Anuj Tomar, an enthusiastic and aspiring DevOps engineer and Cloud engineer with a passion for integrating development and operations to create seamless, efficient, and automated workflows. I'm driven by the challenges of modern software development and am dedicated to continuous learning and improvement in the DevOps field.

What are Persistent Volumes in k8s

Persistent Volumes (PVs) in Kubernetes (k8s) are a way to manage storage resources in a cluster. They provide a mechanism for users to request and consume storage resources without having to know the details of the underlying storage infrastructure. PVs are a part of the Kubernetes storage architecture and are used to persist data beyond the lifecycle of individual pods.

Here are some key points about Persistent Volumes:

  1. Abstraction: PVs abstract the details of how storage is provided, allowing developers to focus on application development rather than storage management.

  2. Lifecycle: PVs have an independent lifecycle from pods. They exist beyond the life of a pod and can be reused by different pods.

  3. Provisioning: PVs can be either statically or dynamically provisioned. Static provisioning involves pre-creating PVs by an administrator, while dynamic provisioning allows Kubernetes to automatically create PVs based on storage class definitions.

  4. Binding: PVs are bound to Persistent Volume Claims (PVCs), which are requests for storage by users. Once a PV is bound to a PVC, it is considered "claimed" and can be used by a pod.

  5. Access Modes: PVs support different access modes, such as ReadWriteOnce, ReadOnlyMany, and ReadWriteMany, which define how the volume can be accessed by pods.

  6. Reclaim Policy: PVs have a reclaim policy that determines what happens to the volume after it is released by a PVC. Common policies include Retain, Recycle, and Delete.

By using Persistent Volumes, Kubernetes provides a flexible and efficient way to manage storage resources, ensuring data persistence and availability across the cluster.


Today’s Tasks:

Task 1: Add a Persistent Volume to Your Deployment Todo App

In this task, we will add a Persistent Volume to your todo-app Kubernetes deployment. Persistent storage is crucial for any app that needs to retain data, such as databases or file storage.

Steps:

  1. Create a Persistent Volume (PV) using a YAML file on your node.

    pv.yml:

      apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: pv-todo-app
      spec:
        capacity:
          storage: 1Gi
        accessModes:
          - ReadWriteOnce
        persistentVolumeReclaimPolicy: Retain
        hostPath:
          path: "/tmp/data"
    
  2. Create a Persistent Volume Claim (PVC) that requests storage from the PV.

    pvc.yml:

      apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: pvc-todo-app
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 500Mi
    
  3. Update the deployment file to use the Persistent Volume Claim.

    deployment.yml:

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: todo-app-deployment
      spec:
        replicas: 1
        selector:
          matchLabels:
            app: todo-app
        template:
          metadata:
            labels:
              app: todo-app
          spec:
            containers:
              - name: todo-app
                image: rishikeshops/todo-app
                ports:
                  - containerPort: 8000
                volumeMounts:
                  - name: todo-app-data
                    mountPath: /app
            volumes:
              - name: todo-app-data
                persistentVolumeClaim:
                  claimName: pvc-todo-app
    
  4. Apply the Persistent Volume and Claim:

      kubectl apply -f pv.yml
      kubectl apply -f pvc.yml
      kubectl apply -f deployment.yml
    
  5. Verify the addition of Persistent Volume:

      kubectl get pv
      kubectl get pvc
      kubectl get pods
    

Note: Remember to apply each YAML file separately before applying the deployment changes.


Task 2: Accessing Data in the Persistent Volume

Once the Persistent Volume is added to your app, it’s time to ensure that the data can be accessed from within the pod.

Steps:

  1. Connect to a Pod in your deployment using this command:

      kubectl exec -it <pod-name> -- /bin/bash
    
  2. Verify Access to the data stored in the Persistent Volume from within the Pod:

    • Navigate to the mounted path (/app) inside the container.

    • Check if the data is present and accessible.

Need help understanding how Persistent Volumes work? Check out this helpful video tutorial.


🎉 Conclusion

Today, we explored how to use Persistent Volumes to ensure that your Kubernetes applications can store data permanently, even across pod restarts. This is a crucial feature for any stateful application.

More from this blog

Anuj Tomar blog's

36 posts

Welcome to Anuj Tomar blog's ! Here, I share my journey in tech, covering DevOps and cloud computing. Explore tutorials, tips, and insights to fuel your learning and growth in technology