Difficulty: intermediate
Estimated Time: 30-45 minutes

BoxBoat Logo

Welcome to the Persistent Volumes and Claims lab. We'll learn the basics of how to manage persistent storage, and how to supply this storage to running Pods.

In general, our microservices are stateless, so they won't need any persistent storage. However, microservices are almost never truly stateless, and this can be important. In addition, you might be running a Database in your Kubernetes cluster, and want to provide it Kubernetes-managed persistent storage.

For more information, see the Kubernetes documentation.

Please email feedback to: [email protected]

Congratulations, you have completed the Persistent Volume lab! Next up, we'll dive right into cluster operations.

Kubernetes Fundamentals II - PersistentVolumes

Step 1 of 7

Local Volumes

To start off, let's create a local emptyDir volume for a Pod. An emptyDir volume has the same lifecycle as the Pod. If the container(s) in the Pod restart or crash, the volume will live on and persist data. However, if the Pod is removed, then the volume will be removed as well.

First, let's look at a simple Pod Manifest:

apiVersion: v1
kind: Pod
  name: nginx
  - name: nginx
    image: nginx
    - name: nginx-data
      mountPath: /data/nginx
  - name: nginx-data
    emptyDir: {}

In this Manifest file, we create a Pod and provide a container specification to run nginx. In this scenario, we are creating a local volume for nginx. It will be mounted inside of the nginx container at /data/nginx.

On the host system, Kubernetes will provide an empty directory for storage.

Next, let's launch this Pod, and store some data:

kubectl create -f ./resources/nginx-local.yaml

We can see that the Pod was started:

kubectl get pods --watch

Once it has successfully started, let's go into the nginx container and drop a file.

kubectl exec -it nginx -- touch /data/nginx/test.txt

Now, let's force the nginx container in the Pod to restart. At this point, the Kubelet will destroy that container, and start a new one.

pkill nginx & kubectl get pods --watch

Now, we can open up another bash session into the container, and we'll find that our file is still there:

kubectl exec nginx -- ls /data/nginx/