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:
- Create a StorageClass named
tekion-scusing thekubernetes.io/no-provisionerprovisioner and aRetainreclaim policy. - Create a PersistentVolume named
tekion-pvwith:
- Capacity: 5Gi
- StorageClass:
tekion-sc - Access mode: ReadWriteOnce (RWO)
- Host path:
/tmp/data
- Create a PersistentVolumeClaim named
tekion-pvcthat requests 2Gi of storage and is bound totekion-pv. - Create a Pod named
tekion-podusing thenginximage. - Mount the PVC inside the Pod at
/app/data.
Create the StorageClass
Create a file named tekion-sc.yaml:
Apply the manifest:
$ kubectl apply -f tekion-sc.yaml
storageclass.storage.k8s.io/tekion-sc createdVerify the StorageClass:
$ kubectl get storageclass
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
tekion-sc kubernetes.io/no-provisioner Retain Immediate false 12sYou 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:
Apply the manifest:
$ kubectl apply -f tekion-pv.yaml
persistentvolume/tekion-pv createdVerify 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> 39sYou 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:
Apply the manifest:
$ kubectl apply -f tekion-pvc.yaml
persistentvolumeclaim/tekion-pvc createdNow 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> 2m7sThe 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
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.yamlEdit tekion-pod.yaml and add the PVC volume and volume mount.
The final manifest can be:
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 createdVerify the Pod:
$ kubectl get pod tekion-pod
NAME READY STATUS RESTARTS AGE
tekion-pod 1/1 Running 0 2m1sThe 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-podLook 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-pvcThis 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/dataYou should receive output showing the /app/data directory.
You can also inspect the mounted filesystem:
kubectl exec tekion-pod -- df -h /app/dataCKA 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.