Managing Persistent Volumes in Your Deployment

2 min read

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.

How to create a PV?

Add a Persistent Volume to your Deployment todo app.

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

      apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: pv-todo-app
      spec:
        capacity:
          storage: 1Gi
        accessModes:
          - ReadWriteOnce
        persistentVolumeReclaimPolicy: Retain
        hostPath:
          path: "/tmp/data"
    
  • Create a Persistent Volume Claim that references the Persistent Volume.

      apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: pvc-todo-app
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 500Mi
    
  • Update your deployment.yml file to include the Persistent Volume Claim. After Applying pv.yml pvc.yml deployment file look like this

      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: sri766/django-app
                ports:
                  - containerPort: 8000
                volumeMounts:
                  - name: todo-app-data
                    mountPath: /app
            volumes:
              - name: todo-app-data
                persistentVolumeClaim:
                  claimName: pvc-todo-app
    
  • 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

Happy Learning!!