This guide provides step-by-step instructions for setting up a Postgres StatefulSet in Minikube, performing backups using Kubernetes CronJobs, and restoring the backups from VolumeSnapshots.
- Ensure that you have Minikube and kubectl installed.
- Start Minikube and ensure it is running.
First, enable the necessary Minikube addons for snapshots and CSI host path driver:
minikube addons enable volumesnapshots
minikube addons enable csi-hostpath-driver
Deploy the Postgres StatefulSet using the following command:
kubectl apply -f postgres.sts.yaml
Once the StatefulSet is running, you can execute scripts inside the Postgres pod:
kubectl exec -it postgres-sts-0 -- psql -U postgres
You can now run any SQL scripts inside the Postgres shell.
To allow the CronJob pod to interact with the database, apply the roles defined in the roles.yaml
file:
kubectl apply -f roles.yaml
Now, create the CronJob that will perform the backups:
kubectl apply -f cronjob.yaml
You can manually trigger a backup CronJob either through the Kubernetes dashboard:
minikube dashboard
Or from the command line:
kubectl create job --from=cronjob/backuper postgres-backup-manual
Once the CronJob has run, you can check the logs to verify if it succeeded:
kubectl logs job.batch/postgres-backup-manual
To verify that the backup created a snapshot, list the available VolumeSnapshots:
kubectl get volumesnapshot
If you wish to delete the CronJob, roles, and StatefulSet, run the following commands:
kubectl delete -f cronjob.yaml
kubectl delete -f roles.yaml
kubectl delete -f postgres.sts.yaml
To restore from a previously created snapshot:
- Modify the
restored.yaml
file to reference the correct snapshot name (retrieved from thekubectl get volumesnapshot
command). - Apply the
restored.yaml
file:
kubectl apply -f restored.yaml
Once the restoration process completes, you can access the restored Postgres pod and verify the data:
kubectl exec -it restored-db-0 -- psql -U postgres
To remove the restored pod and other associated resources, run:
kubectl delete -f restored.yaml
Additionally, you can delete all jobs using:
kubectl delete job --all
This guide should help you set up, back up, restore, and clean up Postgres StatefulSets and VolumeSnapshots in Minikube.