Modifying A Pod To Use A Persistent Volume / Persistent Volume Claim (Kubernetes)

Pod manifest with PVC details

This blog post is going to look at connecting a Kubernetes pod to a Persistent Volume (PV) using a Persistent Volume Claim (PVC). I have split my blog posts whilst I have been learning about Kubernetes as they were getting quite lengthy and each is worthy of its own space. If you need to learn about (or brush up on) any of the items I am writing about please look at my previous posts:

Creating a Kubernetes Pod

Creating a Persistent Volume (PV)

Creating a Persistent Volume Claim (PVC)

My set up during this learning is Minikube running on a MacBook. Minikube installation notes can be found at https://kubernetes.io/docs/tasks/tools/install-minikube/

Assigning the PVC to a Pod

The original pod manifest looked like this:

A Kubernetes pod manifest to create nginx
A Kubernetes pod manifest to create nginx

And was modified to include a volumeMount, so that it looked like this:

test_pod.yml now has volumes and mount paths
test_pod.yml now has volumes and mount paths

However, it needs editing a little more so that it now uses the Persistent Volume Claim created in my previous post.

geektechstuff_k8_pod_pvc_3
Pod manifest with PVC details

apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
volumes:
# Changed name to match Persistent Volume (PV) name
– name: geektechstuffpv
# Changed to a persistentVolumeClaim (PVC)
persistentVolumeClaim:
# Changed to a ClaimName and set to the PVC name
claimName: geektechstuffpvc
containers:
– image: nginx:latest
name: nginx
volumeMounts:
# Where the volume will mount in the container
– mountPath: /usr/share/nginx/html
# Same name as the PVß
name: geektechstuffpv
ports:
– containerPort: 80
name: http
protocol: TCP

 

If you are already running the pod from my earlier posts it can be deleted using the command:

kubectl delete pod PODNAME

For my nginx pod this is:

kubectl delete pod nginx

Note: This will delete the data in pod

geektechstuff_k8_pod_pvc_1

With the previous pod deleted (if needed) and the pod manifest YAML updated, its time to apply it using:

kubectl apply -f PODMANIFESTNAME

for my example this is,

kubectl apply -f test_pod.yml

kubectl apply -f test_pod.yml
kubectl apply -f test_pod.yml

With the pod launched its configuration can be checked using the command:

kubectl describe pod PODNAME

In this example:

kubectl describe pod nginx

kubectl describe pod nginx
kubectl describe pod nginx

And looking for the section about Volumes, which should reference the Persistent Volume Claim (PVC) and the PVC name.

geektechstuff_k8_pod_pvc_5
describe pod, PVC details

kubectl describe pvc PVCNAME , i.e. kubectl describe pvc geektechstuffpvc should now also show the “Mounted By:” line filled (in this case by nginx).

kubectl describe pvc geektechstuffpvc
kubectl describe pvc geektechstuffpvc

So how do we know it works?

I used kubectl exec -it PODNAME — /bin/bash (i.e. kubectl exec -it nginx — /bin/bash) to access the inside of my nginx container.

kubectl exec -it nginx -- /bin/bash
kubectl exec -it nginx — /bin/bash

With the persistent volume mapping to /usr/share/nginx/html , which we set earlier in the pod manifest, I navigated to that folder within the container and used the echo command to create a very basic index.html file that just says “geektechstuff”.

/usr/share/nginx/html
/usr/share/nginx/html

The location /usr/share/nginx/html is the default location nginx servers up web pages from, and by default it serves the index.html page. With the potential index.html saved, I turned on port forwarding so that port 8080 of my MacBook remaps to port 80 (HTTP) of the nginx pod.

kubectl port-forward nginx 8080:80

kubectl port-foward
kubectl port-foward

And the page is successfully there:

A very basic webpage saying geektechstuff
A very basic webpage saying geektechstuff

Now it’s time to break the container and get it to restart. For this I again used the exec command to get into the container and then I used service nginx stop to stop the nginx service, which also causes the container to restart. Here is a video to demonstrate: