How to Use Volumes for State Persistence in Kubernetes

Updated: February 1, 2024 By: Guest Contributor Post a comment

Introduction

One of the core principles of containerized applications is their ephemeral and stateless nature. However, most real-world applications have state—data that persists across sessions and restarts. In Kubernetes, managing state and performing tasks such as saving user data or managing application settings means understanding how to use volumes efficiently.

This tutorial will dive into Kubernetes volumes, which provide a mechanism for persisting data generated by a Pod. We’ll explore how to create and manage volumes, allowing applications to be stateful. The discussion will conclude with a hands-on example to reinforce the concepts covered.

Understanding Volumes in Kubernetes

Kubernetes volumes have a lifetime that is coupled with a Pod. When a Pod ceases to exist, the data on the volume can be retained and the volume can be reattached to a new Pod. Kubernetes supports various types of volumes, each designed for specific use cases and storage needs.

The simplest form of volume is an emptyDir, which is created when a Pod starts and is destroyed when the Pod stops. They are useful for temporary storage.

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mycontainer
    image: nginx
    volumeMounts:
    - name: cache-storage
      mountPath: /cache
  volumes:
  - name: cache-storage
    emptyDir: {}

However, for enduring storage needs, Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) are the way to go. They separate the concept of physical storage from how it is consumed. The capacity is declared using a PV, while a PVC is a request for storage by a user.

Defining Storage with Persistent Volumes

A PV defines the details of the external storage device. It can exist in the cluster as a network-attached storage (NAS), on the cloud, such as an AWS Elastic Block Store (EBS), or locally on a node’s filesystem.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0001
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /tmp
    server: 172.17.0.2

Access to a PV is controlled through accessModes, such as ReadWriteOnce (RWO), which means the volume can be mounted as read-write by a single node.

Claiming Storage with Persistent Volume Claims

A PVC allows a user to specify the characteristics of the storage they need, such as size and access modes. Kubernetes then matches the request with an available PV.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myclaim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

Once a PVC binds to a PV, it claims the full capacity of that volume regardless of the actual amount of storage it’s using. After this, a Pod can mount the PVC to its storage volume.

Binding Volumes to Pods

To make use of a PVC within a Pod, the PVC must be referenced in the Pod configuration.

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mycontainer
    image: nginx
    volumeMounts:
    - name: mypd
      mountPath: /usr/share/nginx/html
  volumes:
  - name: mypd
    persistentVolumeClaim:
      claimName: myclaim

The storage seamlessly becomes part of the Pod’s filesystem. Using these volume claim mechanisms, stateful applications can survive restarts and maintain their saved data.

Conclusion

To efficiently manage state in Kubernetes, it’s imperative to master volumes, persistent storage definitions, and the intricacies of their lifecycle within your clusters. By following these guidelines and experimenting with the different types, you can determine the optimal storage solution for your app’s specific needs, ensuring data resiliency and high availability.

Through proper understanding and management of Kubernetes volumes, your stateful applications can be as resilient and scalable as stateless ones, giving you the best of both worlds in your cloud-native architecture.