K8s postgres not persisting data

10/6/2021

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:

# 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
-- Preston
kubernetes
postgresql

1 Answer

10/6/2021

Issue could be possible due to

volumeMounts:
- name: postgres-data
  mountPath: /var/lib/postgresql/data
  subPath: pgdata

however this is what i am using for postgres

https://github.com/harsh4870/Keycloack-postgres-kubernetes-deployment/blob/main/postgres.yaml

apiVersion: v1
kind: Service
metadata:
  name: postgres
spec:
  ports:
  - name: pgql
    port: 5432
    targetPort: 5432
    protocol: TCP
  selector:
    app: postgres
---
apiVersion: apps/v1  
kind: StatefulSet
metadata:
  name: postgres
spec:
  serviceName: "postgres"
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:9.5
        volumeMounts:
        - name: postgres-data
          mountPath: /var/lib/postgresql/data
          subPath: pgdata
        env:
        - name: POSTGRES_USER
          value: root
        - name: POSTGRES_PASSWORD
          value: password
        - name: POSTGRES_DB
          value: kong
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        ports:
        - containerPort: 5432
      terminationGracePeriodSeconds: 60
  volumeClaimTemplates:
  - metadata:
      name: postgres-data
    spec:
      accessModes:
      - "ReadWriteOnce"
      resources:
        requests:
          storage: 10Gi
-- Harsh Manvar
Source: StackOverflow