0

I'm trying to create a PostgreSQL database in Google Kubernetes Engine, backed on a PVC to maintain the stored databases. Currently I have this definition:

apiVersion: apps/v1
kind: StatefulSet 
metadata:
  name: postgres
spec:
  serviceName: "postgres"
  selector:
    matchLabels:
      db: postgres
  template:
    metadata:
      labels:
        db: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:12.1
        ports:
        - containerPort: 5432 
        volumeMounts:
        - name: postgres-disk
          mountPath: "/var/lib/postgresql"
        env:
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        - name: PGUSER
          value: postgres
        - name: POSTGRES_DB
          value: postgres
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: password

  volumeClaimTemplates:
  - metadata:
      name: postgres-disk
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: "standard"
      resources:
        requests:
          storage: 1Gi

It works as expected, but if I kill the pod to simulate a node failure the whole database is restarted from scratch losing all stored data.

  • There are many configuration points to check, that could cause data loss, and many of them are out of GKE scope or unknown at this time: is data committed before killing the pod; where transaction logs are stored; what is the initdbscript, and so forth. Does database save your data when you shutdown the database gracefully? – mebius99 Feb 3 at 16:12
  • It should be noted that killing a pod simulates crash of a particular process only, not a node failure. That way impact on the data consistency might be significantly different between a pod and a node crash. – mebius99 Feb 4 at 11:14
  • I see your points, but if the data cannot survive a pod crash I cannot trust it to survive a full node failure. – James Gordon Feb 4 at 18:16
  • As mebius99 noted you can't really simulate a node crash in GKE, although you can do it in cloud providers that run on bare metal as Digital Ocean. With some adaptions to handle the differences in the DO storage the configuration in the answer works. – James Gordon Feb 4 at 18:28
0

For what I found the problem are the mount permissions, adding a folder level does the trick

volumeMounts:
- name: data-disk
  mountPath: "/var/lib/postgresql/data"
env:
- name: PGDATA
  value: "/var/lib/postgresql/data/pgdata"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.