Menu Close

CKA Exam Task: Create a Persistent Volume

Creating a Persistent Volume and mounting it in a Pod is a common task you need to perform in the CKA or CKAD exams. This post shows how to do it.

Example Exam Task: Create a Persistent Volume

Create a new PersistentVolume named data-volume using host path /Data. The access mode should be ReadWriteMany and it should have a Capacity of 1Gi. Use the existing storage class standard.

Once the PV is created, create a PersistentVolumeClaim in the namespace moon called data-pvc that references it. The PVC should use access mode ReadWriteMany and request 1Gi of storage.

Create a new pod in namespace moon called data-app using image nginx:1.25.1. The pod should mount the PVC with the path /var/data.

Prerequisites

  • A Kubernetes cluster (I’m using a local minikube cluster for this example)
  • Kubectl installed and configured to access the cluster with administrator rights
  • Run kubectl create namespace moon to prepare your cluster

Create the PersistentVolume

First, create the persistent volume using a yaml manifest:

๐Ÿค“: There is no kubectl command to quickly generate a PersistentVolume yaml template like there is for Pods and other resources. One way to be fast in creating the manifest is copy pasting from K8s Docs and editing according to the task.

Here's an example doc you can use for this task.
apiVersion: v1
kind: PersistentVolume
metadata:
  name: data-volume
spec:
  storageClassName: standard
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/Data"

You can verify the PersistentVolume was created by checking its availability status:

kubectl get pv
NAME          CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
data-volume   1Gi        RWX            Retain           Available                                   7m15s

Create the PerstistenVolumeClaim

Next, you have to create a PersistenVolumeClaim that binds the PersistentVolume.

๐Ÿค“: Again, you are allowed to use the Kubernetes docs during the exam. Copy your yaml Manifest from the docs and modify it.

The final PVC manifest looks like this:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data-pvc
  namespace: moon
spec:
  storageClassName: standard
  volumeName: data-volume
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

Create it using kubectl apply -f data-pvc.yaml.

Your PersistentVolumeClaim should look like this after creation:

 k get pvc -n moon
NAME       STATUS   VOLUME        CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data-pvc   Bound    data-volume   1Gi        RWX            standard       3s

Create the Pod and mount the volume

We need to create a Pod that mounts the PVC. We’ll start by creating a yaml Manifest with kubectl:

kubectl run data-app -n moon --image nginx:1.25.1 -o yaml --dry-run=client > data-app.yaml

Modify the manifest of the pod so that it mounts the PVC, after that it should look like this:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: data-app
  name: data-app
  namespace: moon
spec:
  containers:
  - image: nginx:1.25.1
    name: data-app
    resources: {}
    volumeMounts:
      - mountPath: /var/data
        name: data-volume
  volumes:
    - name: data-volume
      persistentVolumeClaim:
        claimName: data-pvc
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

Run the pod using kubectl apply -f data-pod.yaml.

If the pod reaches a running state you mounted the volume successfully.

Conclusion

In this post, you learned:

  • To create a PersistentVolume using a predefined StorageClass
  • To create a PersistentVolumeClaim binding that PV
  • To create a Pod mounting the volume

Don’t want to miss the next post in the Certified Kubernetes Administrator (CKA) series? Follow me on LinkedIn!

Previous post in the CKA series: CKA Exam Task: Create a Kubernetes User with RBAC

Next post in the CKA series: CKA Exam Task: Backup and Restore etcd