Day 36 Task: Managing Persistent Volumes in Your Deployment πŸ’₯

Day 36 Task: Managing Persistent Volumes in Your Deployment πŸ’₯

#90dayasofdevopschallenge

Β·

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.

πŸ“Œ Task-1 Create a Persistent Volume using a file on your node

Create a Persistent Volume Claim that references the Persistent Volume.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mypv
  namespace: todo
spec:
  capacity:
    storage: 2Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: "/home/ubuntu/pvvolume"

Add the persistent volume claim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mypvc
  namespace: todo
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

πŸ“Œ Update your deployment.yml file to include the Persistent Volume Claim.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: todo-deployment
  namespace: todo
  labels:
    app: todo-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: todo-app
  template:
    metadata:
      labels:
        app: todo-app
    spec:
      containers:
      - name: todo-app
        image: trainwithshubham/django-todo:latest
        ports:
        - containerPort: 8000
        volumeMounts:
          - name: pvcdata
            mountPath: /test
      volumes:
        - name: pvcdata
          persistentVolumeClaim:
            claimName: mypvc

πŸ“Œ 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 command kubectl get pods ,

πŸ“Œ Task -2 Accessing data in the Persistent Volume, Connect to a Pod in your Deployment using command : `kubectl exec -it -- /bin/bash

Thank You

Β