I'm aiming to mount a persistent volume onto a postgres stateful set. To test persistence, I:
- deploy the stateful set, with mounted volume (
kubectl apply -f postgres.yml) - create a table and insert a row
- rescale the stateful set to 0, then delete it (
kubectl scale statefulset/postgresql-db --replicas 0 && kubectl delete statefulset/postgresql-db) - redeploy the stateful set (
kubectl apply -f postgres.yml)
In my new deployment the db is empty, I expected the table and data to be persistent, why is this?
I've been following a few articles to do this, notably:
- https://www.bmc.com/blogs/kubernetes-postgresql/
- https://medium.com/@xcoulon/storing-data-into-persistent-volumes-on-kubernetes-fb155da16666
# kubectl apply -f postgres.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: postgres-pv
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/home/tp/projects/bitbuyer"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-pvc
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgresql-db
spec:
serviceName: postgresql-db-service
selector:
matchLabels:
app: postgresql-db
replicas: 1
template:
metadata:
labels:
app: postgresql-db
spec:
containers:
- name: postgresql-db
image: postgres:latest
volumeMounts:
- name: postgres-pv
mountPath: /data
env:
- name: POSTGRES_PASSWORD
value: pgpassword
- name: PGDATA
value: /data/pgdata
volumes:
- name: postgres-pv
persistentVolumeClaim:
claimName: postgres-pvc