peter (admin)   -  



 

Creating a Pod Using a Persistent Volume Claim

Here we will learn how to create a pod that uses the requested storage through a PersistentVolumeClaim, which is connected to a PersistentVolume. We will store data inside it and then confirm that the same data is still available even after deleting and recreating the pod.

Let's get started.

Verify the Kubernetes Cluster

For this, we are already logged into both the master node and the worker node.

To verify the cluster, run:

kubectl get nodes

Here you can see both the master and worker nodes are in the Ready state. This means our cluster is up and both nodes are properly connected and working.

Verify the Persistent Volume

Let's verify the PersistentVolume:

kubectl get pv

The previously created volume my-pv is in the Bound state and connected with my-pvc. This means the storage is already in use.

Next, get the PersistentVolumeClaim:

kubectl get pvc

Here you can see my-pvc is also in the Bound state with volume my-pv. This confirms that the storage is properly connected and ready to be used by a pod.

Create the Pod

Let's create a pod using a YAML file named pod.yaml.

Using YAML allows us to clearly define all the configuration in one place, including the container and storage settings. It helps us attach the PersistentVolumeClaim properly without missing anything.

Instead of typing multiple commands, we can control everything in one file. This is why YAML is commonly used in Kubernetes for creating resources.

The YAML configuration for our pod is:

apiVersion: v1
kind: Pod
metadata:
  name: pvc-test
spec:
  containers:
    - name: nginx
      image: nginx
      volumeMounts:
        - name: my-storage
          mountPath: /data
  volumes:
    - name: my-storage
      persistentVolumeClaim:
        claimName: my-pvc

Here, inside the metadata section, we give the pod the name pvc-test so Kubernetes can identify it.

In the spec section, we define the container. We give it a name and use the NGINX image so that a web server can run.

The volumeMounts section defines the path /data inside the container. This is the location where the storage will be available. We also give the volume a name so it can be connected to the actual volume.

In the volumes section, we define the real storage source. We use a PersistentVolumeClaim and give the claim name my-pvc. This means the pod will use the storage that is already connected to the PersistentVolume.

This setup allows the pod to read and write data inside /data, and that data will be stored in the PersistentVolume instead of the container.

Save the file and exit.

Apply the Pod Configuration

Apply the YAML file so Kubernetes can create the pod and connect it with the PersistentVolumeClaim:

kubectl apply -f pod.yaml

To verify the pod:

kubectl get pods

The pod pvc-test should be in the Running state, which means the container is running successfully.

Verify the Volume Mount

To see more details about the pod, run:

kubectl describe pod pvc-test

You can see that the container is running successfully with the NGINX image.

Under the mount section, you can see that /data is connected with my-storage, which means our volume is attached inside the container.

In the volume section, you can see that my-storage comes from the PersistentVolumeClaim named my-pvc. This confirms that the pod is using the storage we created earlier.

The container is therefore not using temporary storage. Instead, it is connected to persistent storage. Any data written inside /data will be saved in the PersistentVolume and will remain available even if the pod is deleted.

Store Data in the Persistent Volume

Now it is time to test whether the storage keeps the data.

Go inside the pod so we can create and verify data directly in the mounted storage path:

kubectl exec -it pvc-test -- /bin/bash

Inside the pod, create a file containing some data:

echo "my persistent data" > /data/file.txt

The file is stored in /data/file.txt, which is our mounted storage location.

Read the file:

cat /data/file.txt

The output should show:

my persistent data

This means the data has been stored successfully in the PersistentVolume.

The data is not stored only inside the container. It is stored in the attached persistent storage.

Exit from the container:

exit

Delete and Recreate the Pod

Now let's delete the pod to test whether the data stored in the PersistentVolume remains available after the pod is removed:

kubectl delete pod pvc-test

After deleting the pod, apply the YAML file again:

kubectl apply -f pod.yaml

This creates a new pod with the same configuration and reconnects it to the PersistentVolumeClaim.

Verify the Persistent Data

Go inside the newly created pod:

kubectl exec -it pvc-test -- /bin/bash

Now read the data from the storage path:

cat /data/file.txt

The output should again show:

my persistent data

This confirms that the data is still available even after deleting the original pod.

The data is stored in the PersistentVolume rather than inside the container. When the new pod is created, it connects to the same storage and can read the same data.

This proves that the storage is working as persistent storage.

Summary

We created a pod that uses storage through the PersistentVolumeClaim my-pvc, which is connected to the PersistentVolume my-pv.

We then stored data inside the mounted /data directory, deleted the pod, recreated it, and verified that the same data was still available.

This demonstrates how a pod can use persistent storage through a PersistentVolumeClaim and retain data even when the pod is deleted and recreated.

 


Comments (0)

There are no comments yet. Be the first to comment.

Log in or register to leave a comment. all comments are subject to admin approval