Skip to main content

Command Palette

Search for a command to run...

Adding Persistent Storage to an Application on Kubernetes

Updated
2 min read

Demonstration

In Kubernetes, pods are ephemeral. When a pod is deleted or recreated, any data stored inside the container is lost. To persist application data, Kubernetes provides Persistent Volumes (PV) and Persistent Volume Claims (PVC).

In this guide, we will create a PVC and attach it to the Mealie application so its data survives pod restarts.

Step 1: Create a Persistent Volume Claim

First, create a file called mealie-pvc.yaml to define the PersistentVolumeClaim.

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

Create the PVC in the cluster using:

kubectl apply -f mealie-pvc.yaml

Once applied, Kubernetes will create a Persistent Volume Claim.

Step 2: Attach the PVC to the Mealie Deployment

Next, update the deployment.yaml file so that the Mealie container uses the newly created PVC.

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mealie
  name: mealie
  namespace: mealie
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mealie
  template:
    metadata:
      labels:
        app: mealie
    spec:
      containers:
      - image: ghcr.io/mealie-recipes/mealie:v3.12.0
        name: mealie
        ports:
        - containerPort: 9000
        volumeMounts:
        - name: data
          mountPath: /app/data
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: mealie-data

Apply the updated deployment configuration:

kubectl apply -f deployment.yaml

This will dynamically provision a volume using the pvc and mount the volume at the path app/data where all the application related data is located. This data is stored in the persistent volume which means the data persists even when the pods go down.