Managing Stateful Applications with Persistent Storage

Tutorial 4 of 5

1. Introduction

In this tutorial, we'll learn how to manage stateful applications using Kubernetes and persistent storage. Stateful applications require data persistence, meaning the data must survive the lifecycle of a pod. Kubernetes provides a way to manage this through Persistent Volumes and Persistent Volume Claims.

By the end of this tutorial, you will be able to:

  • Understand the concept of stateful applications and persistent storage.
  • Deploy a stateful application in Kubernetes.
  • Configure the application to use persistent storage.

Prerequisites: Basic understanding of Kubernetes, Pods, and Docker is required.

2. Step-by-Step Guide

Kubernetes uses Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) to manage persistent storage. A PV is a piece of storage that has been provisioned by an administrator, while a PVC is a request for storage by a user.

To use persistent storage in your application, you need to:

  1. Define a Persistent Volume.
  2. Define a Persistent Volume Claim.
  3. Update your application to use the Persistent Volume Claim.

Best Practices and Tips

  • Always define a storage class for your Persistent Volumes.
  • Ensure your application gracefully handles the case where the requested Persistent Volume Claim is not available.

3. Code Examples

Example 1: Defining a Persistent Volume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  storageClassName: standard
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /tmp/data

This code defines a Persistent Volume named my-pv with a storage capacity of 1Gi. The hostPath attribute specifies that this volume is located at /tmp/data on the host node.

Example 2: Defining a Persistent Volume Claim

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

This code defines a Persistent Volume Claim named my-pvc that requests 1Gi of storage from a Persistent Volume.

Example 3: Updating the Application to use the Persistent Volume Claim

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: nginx
    volumeMounts:
    - mountPath: /usr/share/nginx/html
      name: my-storage
  volumes:
  - name: my-storage
    persistentVolumeClaim:
      claimName: my-pvc

This code updates the Pod definition to use the Persistent Volume Claim my-pvc. The volumeMounts attribute specifies that the volume is mounted at /usr/share/nginx/html in the container.

4. Summary

In this tutorial, we learned how to manage stateful applications using Persistent Volumes and Persistent Volume Claims in Kubernetes. We saw how to define a Persistent Volume, a Persistent Volume Claim, and update an application to use the Persistent Volume Claim.

Next steps for learning

  • Learn how to backup and restore data in Persistent Volumes.
  • Understand how to scale stateful applications in Kubernetes.

Additional resources

5. Practice Exercises

  1. Exercise 1: Define a Persistent Volume with a capacity of 2Gi and access mode of ReadWriteMany.

Solution:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  storageClassName: standard
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: /tmp/data
  1. Exercise 2: Define a Persistent Volume Claim that requests 2Gi of storage from the Persistent Volume defined in Exercise 1.

Solution:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
  1. Exercise 3: Update the Pod definition from the tutorial to use the Persistent Volume Claim defined in Exercise 2.

Solution:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: nginx
    volumeMounts:
    - mountPath: /usr/share/nginx/html
      name: my-storage
  volumes:
  - name: my-storage
    persistentVolumeClaim:
      claimName: my-pvc

Tips for further practice

  • Practice defining different types of Persistent Volumes and Persistent Volume Claims.
  • Experiment with different access modes and storage classes.