Lab 5 - Persistent Storage¶
30 min Intermediate
Problem¶
The Death Star plans can't be lost no matter what happens, so we need to store them in a database whose data survives Pod restarts.
A container's filesystem is thrown away when its Pod is deleted. To keep data, the Pod needs a PersistentVolumeClaim (PVC). On OpenShift you normally don't create PersistentVolumes by hand. The cluster's default StorageClass provisions one automatically when a PVC asks for storage.
Create a PersistentVolumeClaim¶
- The PVC is named
postgresql-pvc. - It requests
1Giof storage. - It uses the access mode
ReadWriteOnce. - It doesn't set a
storageClassName, so the cluster's default StorageClass is used.
Create a PostgreSQL Pod that uses the claim¶
- The Pod is named
postgresql-podand has the labelapp: postgresql. - It uses the image
quay.io/sclorg/postgresql-16-c9s. This is the upstream build of Red Hat's PostgreSQL image, and it runs as any non-root user. - It exposes
containerPort5432. -
It sets these environment variables:
Name Value POSTGRESQL_USERrebelPOSTGRESQL_PASSWORDpasswordPOSTGRESQL_DATABASEdeathstar -
It mounts the PVC at
/var/lib/pgsql/data.
Setup¶
One StorageClass should be marked (default).
Note
If the default StorageClass uses WaitForFirstConsumer binding mode, the PVC shows Pending until a Pod uses it. That's expected.
Verification¶
-
The PVC is
Boundand the Pod isRunning: -
Save the plans to the database:
-
Delete the Pod, then recreate it from the same manifest:
-
The plans survived:
Going further¶
- Look at the PersistentVolume that was created for you with
oc get pv. You may need cluster-reader access. What is itsRECLAIM POLICY, and what happens to the data when you delete the PVC? - A bare Pod is fine for learning, but real databases run as a StatefulSet with
volumeClaimTemplates. See StatefulSets.