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 mongodb image that will contain a MongoDB database. 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 mongodb image that will contain a MongoDB database. 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. Use the following YAML spec for the PersistentVolume named mongodb-pv.yaml:

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

    kubectl apply -f mongodb-pv.yaml

Create a PersistentVolumeClaim.

  1. Use the following YAML spec for the PersistentVolumeClaim named mongodb-pvc.yaml:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: mongodb-pvc
    spec:
      storageClassName: local-storage
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi
  2. Then, create the PersistentVolumeClaim:

    kubectl apply -f mongodb-pvc.yaml

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

  1. Use the following YAML spec for the pod named mongodb-pod.yaml:

    apiVersion: v1
    kind: Pod
    metadata:
      name: mongodb
    spec:
      containers:
      - image: mongo
        name: mongodb
        volumeMounts:
        - name: mongodb-data
          mountPath: /data/db
        ports:
        - containerPort: 27017
          protocol: TCP
      volumes:
      - name: mongodb-data
        persistentVolumeClaim:
          claimName: mongodb-pvc
  2. Then, create the pod:

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

    kubectl get pods

Access the node and view the data within the volume.

  1. Run the following command:

    kubectl get nodes
  2. Connect to the worker node (get the <node_hostname> from the NAME column of the above output), using the same password as the Kube Master:

    ssh <node_hostname>
  3. Switch to the /mnt/data directory:

    cd /mnt/data
  4. List the contents of the directory:

    ls

Delete the pod and create a new pod with the same YAML spec.

  1. Exit out of the worker node:

    exit
  2. Delete the pod:

    kubectl delete pod mongodb
  3. Create a new pod:

    kubectl apply -f mongodb-pod.yaml

Verify the data still resides on the volume.

  1. Log in to the worker node again:

    ssh <node_hostname>
  2. Switch to the /mnt/data directory:

    cd /mnt/data
  3. List the contents of the directory:

    ls

Conclusion

Congratulations on completing this lab!