Kubernetes Volume | Persistent Volume, Persistent Volume Claim

Sanket NankarSanket Nankar
2 min read

Step 1: Create the PersistentVolume (PV)

We need a PV named pv-demo, with ReadWriteMany, 512Mi storage, and host path /data/config.

pv-demo.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-demo
spec:
  capacity:
    storage: 512Mi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: /data/config

Apply the PV:

kubectl apply -f pv-demo.yaml
kubectl get pv

You should see pv-demo in the list with status Available.

Step 2: Create the PersistentVolumeClaim (PVC)

The PVC requests 256Mi storage, uses empty string storageClassName (to bind manually to PV), and is named pvc-demo.

pvc-demo.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-demo
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 256Mi
  storageClassName: ""

Apply the PVC:

kubectl apply -f pvc-demo.yaml
kubectl get pvc

Check that the PVC is Bound to pv-demo.

Step 3: Create a Pod using the PVC

We mount the PVC in a Pod named app at /var/app/config using nginx:latest.

pod-app.yaml

apiVersion: v1
kind: Pod
metadata:
  name: app
spec:
  containers:
    - name: nginx-container
      image: nginx:latest
      volumeMounts:
        - mountPath: /var/app/config
          name: config-volume
  volumes:
    - name: config-volume
      persistentVolumeClaim:
        claimName: pvc-demo

Apply the Pod:

kubectl apply -f pod-app.yaml
kubectl get pods

Wait until the Pod is Running.

Step 4: Open an interactive shell and create a file

Open the shell:

kubectl exec -it app -- /bin/bash

Inside the Pod:

cd /var/app/config
echo "Hello from PV!" > test-file.txt
ls -l
cat test-file.txt

Exit the shell:

exit

You can verify that the file is present on the host at /data/config as well.

Key Learnings & Insights

Today, I explored Kubernetes PersistentVolumes and PersistentVolumeClaims. I created a PV with host path storage, bound a PVC with an empty storage class and mounted it in an Nginx Pod. I tested ReadWriteMany access by creating a file inside the Pod.

Key Takeaways:

  • PV and PVC decouple storage from Pods for flexibility.

  • ReadWriteMany allows multiple Pods to share the same storage.

  • An empty storageClassName allows manual binding to a PV.

  • HostPath volumes are simple for learning but not ideal for production.

  • Kubernetes makes persistent storage manageable and portable across Pods.


0
Subscribe to my newsletter

Read articles from Sanket Nankar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sanket Nankar
Sanket Nankar