Difficulty: intermediate
Estimated Time: 15-20 minutes

Digital Academy Logo

Welcome to the Digial Academy's "Kubernetes Fundamentals" series. This is Module 4 - Introduction to Persistent Volumes.

In this lab, we will create PersistentVolumes, and provide them to Pods via PersistentVolumeClaims. 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 to understand why that is. For instance, 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.

Developer(s): William Hearn and Zachary Seguin

Congratulations, you now have an understanding of Kubernetes Persistent Volumes next we will be taking a cluster operations.

Module 4 - Introduction to Persistent Volumes

Step 1 of 4

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 ccrash, 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
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: nginx-data
      mountPath: /data/nginx
  volumes:
  - 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/