Skip to main content

Storage

Mount a Volume Into a Pod Using Kubernetes PV, PVC and SC

You are a DevOps engineer at Devopscube working on a new microservices-based application named Tekion. The application requires persistent storage for one of its components.

Your task is to configure Kubernetes persistent storage using a StorageClass, PersistentVolume, PersistentVolumeClaim, and Pod.

Configure and verify the following resources in the Kubernetes cluster:

  1. Create a StorageClass named tekion-sc using the kubernetes.io/no-provisioner provisioner and a Retain reclaim policy.
  2. Create a PersistentVolume named tekion-pv with:
    • Capacity: 5Gi
    • StorageClass: tekion-sc
    • Access mode: ReadWriteOnce (RWO)
    • Host path: /tmp/data
  1. Create a PersistentVolumeClaim named tekion-pvc that requests 2Gi of storage and is bound to tekion-pv.
  2. Create a Pod named tekion-pod using the nginx image.
  3. Mount the PVC inside the Pod at /app/data.
📚
For more details on PV, PVC, and SC, refer to the official Kubernetes documentation.

Create the StorageClass

Create a file named tekion-sc.yaml:

💡
If you’re unsure about the YAML structure or syntax, check the official Kubernetes documentation for a clear reference.
tekion-sc.yaml
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: tekion-sc provisioner: kubernetes.io/no-provisioner reclaimPolicy: Retain

Apply the manifest:

$ kubectl apply -f tekion-sc.yaml

storageclass.storage.k8s.io/tekion-sc created

Verify the StorageClass:

$  kubectl get storageclass

NAME        PROVISIONER                    RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
tekion-sc   kubernetes.io/no-provisioner   Retain          Immediate           false                  12s

You should see tekion-sc with the kubernetes.io/no-provisioner provisioner and Retain reclaim policy.

Why is kubernetes.io/no-provisioner used?

kubernetes.io/no-provisioner indicates that Kubernetes should not dynamically provision storage for this StorageClass. Instead, the administrator manually creates the PersistentVolume. In this exercise, that manually created PV is tekion-pv.

Create the Kubernetes Persistent Volume

Create a file named tekion-pv.yaml:

persistent-volume.yaml
apiVersion: v1 kind: PersistentVolume metadata: name: tekion-pv spec: capacity: storage: 5Gi storageClassName: tekion-sc accessModes: - ReadWriteOnce hostPath: path: /tmp/data

Apply the manifest:

$ kubectl apply -f tekion-pv.yaml

persistentvolume/tekion-pv created

Verify the PV:

$ kubectl get pv

NAME        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
tekion-pv   5Gi        RWO            Retain           Available           tekion-sc      <unset>                          39s

You should see tekion-pv with 5Gi capacity and a STATUS of Available, since no PVC has been created yet.

Create the Kubernetes persistent storage

Create a file named tekion-pvc.yaml:

tekion-pvc.yaml
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: tekion-pvc spec: accessModes: - ReadWriteOnce storageClassName: tekion-sc resources: requests: storage: 2Gi

Apply the manifest:

$ kubectl apply -f tekion-pvc.yaml

persistentvolumeclaim/tekion-pvc created

Now check both the PV and PVC:

$ kubectl get pv 

NAME        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
tekion-pv   5Gi        RWO            Retain           Bound    default/tekion-pvc   tekion-sc      <unset>                          4m59s

$ kubectl get pvc

NAME         STATUS   VOLUME      CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
tekion-pvc   Bound    tekion-pv   5Gi        RWO            tekion-sc      <unset>                 2m7s

The PVC and PV should both showSTATUS: Bound, confirming that the PVC has successfully claimed the PV.

The claim can bind to the PV because the PV has enough capacity to satisfy the requested storage.

Understand the PV and PVC Relationship

Understand the PV and PVC Relationship

The StorageClass defines the storage type, the PV (tekion-pv) provides 5Gi, and the PVC (tekion-pvc) requests 2Gi. Once the PVC binds to the PV, the Pod can use the storage through /app /data.

Create the Pod

You can generate a basic Pod manifest using:

kubectl run tekion-pod \
  --image=nginx \
  --dry-run=client -o yaml > tekion-pod.yaml

Edit tekion-pod.yaml and add the PVC volume and volume mount.

The final manifest can be:

tekion-pod.yaml
apiVersion: v1 kind: Pod metadata: name: tekion-pod spec: containers: - name: tekion-pod image: nginx volumeMounts: - name: tekion-volume mountPath: /app/data volumes: - name: tekion-volume persistentVolumeClaim: claimName: tekion-pvc

The important configuration is the connection between the volume mount and the PVC. The Pod mounts tekion-volume at /app/data, while the volumes section connects the same volume to tekion-pvc using persistentVolumeClaim. The volume name must match in both sections.

It must match the PVC name tekion-pvc. This connects the Pod to the PVC and mounts the storage at /app/data.

Deploy the Pod

Apply the Pod manifest:

$ kubectl apply -f tekion-pod.yaml

pod/tekion-pod created

Verify the Pod:

$ kubectl get pod tekion-pod

NAME         READY   STATUS    RESTARTS   AGE
tekion-pod   1/1     Running   0          2m1s

The Pod should be running successfully with the container ready.
The PV and PVC should both be in the Bound state, while the Pod should be Running. This confirms that the storage chain is configured and working correctly.

Verify the Volume Mount

Following this command

kubectl describe pod tekion-pod

Look for the Mounts section.

You should see information similar to:

Mounts:
      /app/data from tekion-volume (rw)

The Volumes section should show:

Volumes:
  tekion-volume:
    Type:       PersistentVolumeClaim
    ClaimName:  tekion-pvc

This confirms that the Pod is using tekion-pvc.

Verify the Mount Inside the Container

You can also verify that /app/data exists inside the nginx container:

kubectl exec tekion-pod -- ls -ld /app/data

You should receive output showing the /app/data directory.

You can also inspect the mounted filesystem:

kubectl exec tekion-pod -- df -h /app/data

CKA Exam Tips

These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.

Tip 1: Check the StorageClass API Version

Make sure the StorageClass uses the correct API version: storage.k8s.io/v1.

Tip 2: Match the StorageClass

The storageClassName must match in both the PV and PVC, such as tekion-sc.

Tip 3: Check the Mount Path

Make sure the volume is mounted at the required path: /app/data.

Tip 4: Check Kubernetes PV and PVC

Use kubectl get pv and kubectl get pvc to verify that both resources are Bound.

Tip 5: Check Pod Events

If the Pod isn't running, use kubectl describe pod tekion-pod and check the Events section for clues.