Skip to main content

TRG 3.02 - Persist Data

StatusCreatedPost-History
Draft17-Jul-2023'loos' typo fix
Active07-Mar-2023
Draft02-Jan-2023n/a
Moved02-Jan-2023content moved

Why

In cases where data has to be persisted (database, uploaded files etc.), Kubernetes must be configured to create Persistent Volume that is attached to an underlying disk where data remains even after the deletion of the application. Otherwise, an incidental deletion will delete all state.

Description

Using stateful data requires additional caution to not lose data by accident. Therefore, when a pod/deployment/statefulset resource is removed, data will still be available on the StorageClass's disk that was used.

Persistent Volumes can have different reclaim policies, such as "Retain," "Recycle," and "Delete". The default reclaim policy is set to "Delete" for dynamically provisioned PVs. This implies that provisioned persistent volume gets automatically erased when a user removes the associated PersistentVolumeClaim. However, this automated deletion might not be suitable, especially if the volume contains valuable data. In such scenarios, opting for the "Retain" policy is more fitting. With the "Retain" policy, deleting a PersistentVolumeClaim won't result in the corresponding PersistentVolume being deleted, allowing users to manually recover all of its data.

How

Example PersistentVolumeClaim:

# pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-persistent-tmp-demo
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50Mi

This can be referenced in the volumes section of a Pod/Deployment/StatefulSet resource:

# deployment.yaml
#...
volumes:
- name: pv-tmp-demo
persistentVolumeClaim:
claimName: pvc-persistent-tmp-demo
#...
tip

It is not recommended to directly request the claim in a StatefulSet! Rather create the PVC separately and reference that as an existing claim. See the example in Bitnami's Postgresql chart where an existing claim can be referenced for the primary database at the primary.persistence.existingClaim property.