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:
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:
And was modified to include a volumeMount, so that it looked like this:
However, it needs editing a little more so that it now uses the Persistent Volume Claim created in my previous post.
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
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
With the pod launched its configuration can be checked using the command:
kubectl describe pod PODNAME
In this example:
kubectl describe pod nginx
And looking for the section about Volumes, which should reference the Persistent Volume Claim (PVC) and the PVC name.
kubectl describe pvc PVCNAME , i.e. kubectl describe pvc geektechstuffpvc should now also show the “Mounted By:” line filled (in this case by nginx).
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.
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”.
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
And the page is successfully there:
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: