Persistent storage for Strapi schema changes on Kubernetes deployment
Strapi, a popular open-source headless CMS built on Node.js, streamlines content management for developers of all skill levels. It offers flexibility by allowing connection to preferred databases, frameworks, and static site generators, while also enabling content creation from any device. However, when working with Strapi in a Kubernetes environment, schema changes made through the admin panel can lead to data loss during pod restarts. This blog explores the challenges and proposes a solution for persistent storage of Strapi schema changes.
The challenge
Strapi primarily utilizes four directories — config, database, public, and src. While it’s ideal to package all these directories within a Docker image, some schema changes made through the admin panel are stored locally on the pod’s file system, specifically in the /opt/app/src directory.
Deploying Strapi on Kubernetes presents two significant challenges:
- Data loss: If a pod restarts due to errors or maintenance, any unsaved schema changes in the /opt/app/src directory are lost.
- Manual overhead: Rebuilding Docker images and redeploying the application after every schema change is time-consuming and inefficient.
The solution
One potential solution to this problem is to manually back up the /opt/app/src directory after making schema changes, rebuild the Docker image, and redeploy it. This approach ensures that the schema changes are preserved even if the pod restarts. However, this is a time-consuming and error-prone process, especially if pod restarts occur frequently.
Recently, we encountered a scenario where a pod restarted immediately after a schema change, preventing us from taking a backup and rebuilding the image. As a result, we lost the schema changes and had to manually delete the corresponding database entries and redo the changes.
Implementation steps
Step 1: Create a persistent volume claim (PVC) — Define a PVC to allocate storage for the /opt/app/src directory.
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: strapi-src-claim
spec:
storageClassName: "gp2"
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: "1G"
Step 2: Mount the (read more..)