Creating Persistent Storage for Pods in Kubernetes

30 minutes
  • 6 Learning Objectives

About this Hands-on Lab

Pods in Kubernetes are ephemeral, which makes the local container filesytem unusable, as you can never ensure the pod will remain. To decouple your storage from your pods, you will be creating a persistent volume to mount for use by your pods. You will be deploying a redis image. You will first create the persistent volume, then create the pod YAML for deploying the pod to mount the volume. You will then delete the pod and create a new pod, which will access that same volume.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a PersistentVolume.
  1. Use the following YAML spec for the PersistentVolume named redis-pv.yaml:

    apiVersion: v1
    kind: PersistentVolume
      name: redis-pv
      storageClassName: ""
        storage: 1Gi
        - ReadWriteOnce
        path: "/mnt/data"
  2. Then, create the PersistentVolume:

    kubectl apply -f redis-pv.yaml
Create a PersistentVolumeClaim.
  1. Use the following YAML spec for the PersistentVolumeClaim named redis-pvc.yaml:

    apiVersion: v1
    kind: PersistentVolumeClaim
      name: redisdb-pvc
      storageClassName: ""
        - ReadWriteOnce
          storage: 1Gi
  2. Then, create the PersistentVolumeClaim:

    kubectl apply -f redis-pvc.yaml
Create the redispod image, with a mounted volume to mount path `/data`
  1. Use the following YAML spec for the pod named redispod.yaml:

    apiVersion: v1
    kind: Pod
      name: redispod
      - image: redis
        name: redisdb
        - name: redis-data
          mountPath: /data
        - containerPort: 6379
          protocol: TCP
      - name: redis-data
          claimName: redisdb-pvc
  2. Then, create the pod:

    kubectl apply -f redispod.yaml
  3. Verify the pod was created:

    kubectl get pods
Connect to the container and write some data.
  1. Connect to the container and run the redis-cli:

    kubectl exec -it redispod redis-cli
  2. Set the key space server:name and value "redis server":

    SET server:name "redis server"
  3. Run the GET command to verify the value was set:

    GET server:name
  4. Exit the redis-cli:

Delete `redispod` and create a new pod named `redispod2`.
  1. Delete the existing redispod:

    kubectl delete pod redispod
  2. Open the file redispod.yaml and change line 4 from name: redispod to:

    name: redispod2
  3. Create a new pod named redispod2:

    kubectl apply -f redispod.yaml
Verify the volume has persistent data.
  1. Connect to the container and run redis-cli:

    kubectl exec -it redispod2 redis-cli
  2. Run the GET command to retrieve the data written previously:

    GET server:name
  3. Exit the redis-cli:


Additional Resources

You have been given access to a two-node cluster. Your objective is to create persistent storage for the pod, and prove that the data resides on disk, even when you delete the pod. You must first create a PersistentVolume object in Kubernetes. Once the PersistentVolume has been created, you must create a PersistentVolumeClaim in order for you to claim that volume for the pod. Once you have your PersistentVolume and PersistentVolumeClaim, you are now ready to create the pod.

Create the pod with the image redis and include the volume, mounted to the /data directory. Also, ensure that port 6379 is open on the container. Once you've created the pod, connect to it and write some data to the database using the redis-cli, then use the QUIT command to exit the redis-cli. Then, delete the pod and create a new pod that will mount that same volume. Connect to the new redis pod and retreive the data that you wrote to the database on the first pod. This will prove that the data persists beyond the life of the pod. Perform the following tasks in order to complete this hands-on lab:

  • Create a PersistentVolume named redis-pv with 1Gi of storage and ReadWriteOnce access mode. Mount the PersistentVolume to the /mnt/data directory on the host
  • Create a PersistentVolumeClaim named redisdb-pvc with a request for 1Gi of storage. Make sure it has the same access mode as the PersistentVolume
  • Create a pod named redispod using the redis image and a container name of redisdb. The mount path on the container must be /data and you must open port 6379 on the container. Make sure you mount the same PVC that you created in the last step
  • Connect to the container by running the redis-cli command from within the container. Use the SET command to apply the key space server:name as "redis server". Use the GET command to verify that the server:name keyspace has been set. Use the QUIT command to exit the redis-cli
  • Delete the pod named redispod
  • Open the pod yaml and change the name of the pod to redispod2. Create the new pod with that YAML spec
  • Connect to the redispod2 container and run the redis-cli from within the container. Run the command GET server:name to retrieve the keyspace that we set with the previous pod. The result will be "redis server", which means the data persisted from redispod to redispod2.

Having persistent storage means that we can use that same volume with different pods in our Kubernetes cluster and access the same data.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?