Storing Application Data with Persistentvolumes

KubernetesKubernetesIntermediate
Practice Now

Introduction

In this lab, you will learn how to store application data with PersistentVolumes in Kubernetes.

PersistentVolumes (PVs) are Kubernetes resources that represent a piece of networked storage in a cluster. They can be used to store application data in a way that is independent of the container lifecycle. This means that data can be preserved even if the container is terminated or moved to another node.

In this lab, you will create a PersistentVolume and use it to store data from a simple web application. You will then modify the application to use a PersistentVolumeClaim (PVC) to access the PersistentVolume. Finally, you will modify the PVC to request specific storage resources.

Prerequisites

Before beginning this lab, you should have a basic understanding of Kubernetes and how to create and deploy applications using Kubernetes manifests. You should also have a Kubernetes cluster set up and configured to use PersistentVolumes.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/exec("`Exec`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") subgraph Lab Skills kubernetes/exec -.-> lab-9685{{"`Storing Application Data with Persistentvolumes`"}} kubernetes/get -.-> lab-9685{{"`Storing Application Data with Persistentvolumes`"}} kubernetes/apply -.-> lab-9685{{"`Storing Application Data with Persistentvolumes`"}} end

Create a PersistentVolume

In this step, you will create a PersistentVolume that can be used to store data. You will create a YAML file called pv.yaml with the following content:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /mnt/data

This file creates a PersistentVolume with a capacity of 1Gi and an access mode of ReadWriteOnce. The hostPath field specifies that the data will be stored on the host machine at the path /mnt/data. The persistentVolumeReclaimPolicy field is set to Retain, which means that the data will be preserved even if the PersistentVolume is deleted.

Apply the PersistentVolume to your cluster with the following command:

kubectl apply -f pv.yaml

Deploy a Simple Web Application

In this step, you will deploy a simple web application that will store data on the PersistentVolume you created in Step 1. You will create a YAML file called web-app.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web-app
  template:
    metadata:
      labels:
        app: web-app
    spec:
      containers:
        - name: web-app
          image: nginx
          volumeMounts:
            - name: data
              mountPath: /usr/share/nginx/html/data
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: my-pvc

This file creates a Deployment with one replica and a container that runs the nginx image. The volumeMounts field specifies that the container should mount the PersistentVolume at the path /usr/share/nginx/html/data. The volumes field specifies that the container should use a PersistentVolumeClaim called my-pvc.

Apply the Deployment to your cluster with the following command:

kubectl apply -f web-app.yaml

Create a PersistentVolumeClaim

In this step, you will create a PersistentVolumeClaim that will be used to request storage from the PersistentVolume you created in Step 1. You will create a YAML file called pvc.yaml with the following content:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi

This file creates a PersistentVolumeClaim with an access mode of ReadWriteOnce and a request for 500Mi of storage from the PersistentVolume.

Apply the PersistentVolumeClaim to your cluster with the following command:

kubectl apply -f pvc.yaml

Verify Data Persistence

In this step, you will verify that the data is being persisted on the PersistentVolume. You will access the web application running on the container and write some data to the PersistentVolume.

First, find the name of the pod running your application by running the following command:

kubectl get pods -l app=web-app

You should see a single pod running your application. Note the name of the pod.

Next, run the following command to open a shell session in the container running your application:

kubectl exec -it sh < pod-name > --

Replace with the name of the pod that you noted earlier.

Once you are in the shell session, run the following command to add a test.txt file:

echo "This is a test file." > /usr/share/nginx/html/data/test.txt

This command creates a file called test.txt with the text "This is a test file." in the data directory of the PersistentVolume.

Delete the web application with the following command:

kubectl delete deployment web-app

Recreate the web application with the following command:

kubectl apply -f web-app.yaml

Verify that the file you created in the data directory still exists with the following command:

kubectl get pods -l app=web-app
kubectl exec cat /usr/share/nginx/html/data/test.txt < pod-name > --

Replace with the name of the pod that you noted earlier.

Modify the PVC Storage Resources

In this step, you will modify the PVC to request specific storage resources from the PersistentVolume. You will modify the pvc.yaml file to request 1Gi of storage instead of 500Mi.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Add an allowVolumeExpansion: true field to storageclass in the last line.

kubectl edit storageclass standard
lab-storing-application-data-with-persistentvolumes-5

Apply the updated PersistentVolumeClaim to your cluster with the following command:

kubectl delete deployment web-app
kubectl delete pvc my-pvc
kubectl apply -f web-app.yaml
kubectl apply -f pvc.yaml

Summary

In this lab, you learned how to store application data with PersistentVolumes in Kubernetes. You created a PersistentVolume, deployed a simple web application that used the PersistentVolume, created a PersistentVolumeClaim to request storage from the PersistentVolume, and modified the PersistentVolumeClaim to request specific storage resources. You also verified that the data was being persisted on the PersistentVolume even if the container was terminated or moved to another node.

Other Kubernetes Tutorials you may like