Skip to main content

Creating Persistent Storage for Pods in Kubernetes

Hands-On Lab

 

Photo of Chad Crowell

Chad Crowell

DevOps Training Architect II in Content

Length

00:30:00

Difficulty

Intermediate

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Creating Persistent Storage for Pods in Kubernetes

In this hands-on lab, to decouple our storage from our pods, we will create a persistent volume to mount for use by our pods. We will deploy a redis image. We will first create the persistent volume, then create the pod YAML for deploying the pod to mount the volume. We will then delete the pod and create a new pod, which will access that same volume.

Log in to the Kube Master server using the credentials on the lab page (either in your local terminal, using the Instant Terminal feature, or using the public IP), and work through the objectives listed.

Create a PersistentVolume.

  1. Create the file, named redis-pv.yaml:

    vim redis-pv.yaml
  2. Use the following YAML spec for the PersistentVolume:

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: redis-pv
    spec:
      storageClassName: ""
      capacity:
        storage: 1Gi
      accessModes:
        - ReadWriteOnce
      hostPath:
        path: "/mnt/data"
  3. Then, create the PersistentVolume:

    kubectl apply -f redis-pv.yaml

Create a PersistentVolumeClaim.

  1. Create the file, named redis-pvc.yaml:

    vim redis-pvc.yaml
  2. Use the following YAML spec for the PersistentVolumeClaim:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: redisdb-pvc
    spec:
      storageClassName: ""
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi
  3. Then, create the PersistentVolumeClaim:

    kubectl apply -f redis-pvc.yaml

Create a pod from the redispod image, with a mounted volume to mount path /data.

  1. Create the file, named redispod.yaml:

    vim redispod.yaml
  2. Use the following YAML spec for the pod:

    apiVersion: v1
    kind: Pod
    metadata:
      name: redispod
    spec:
      containers:
      - image: redis
        name: redisdb
        volumeMounts:
        - name: redis-data
          mountPath: /data
        ports:
        - containerPort: 6379
          protocol: TCP
      volumes:
      - name: redis-data
        persistentVolumeClaim:
          claimName: redisdb-pvc
  3. Then, create the pod:

    kubectl apply -f redispod.yaml
  4. 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:

    QUIT

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:

    QUIT

Conclusion

Congratulations on completing this lab!