konkrer
konkrer

Reputation: 70

How to have data persist in GKE kubernetes StatefulSet with postgres?

So I'm just trying to get a web app running on GKE experimentally to familiarize myself with Kubernetes and GKE.

I have a statefulSet (Postgres) with a persistent volume/ persistent volume claim which is mounted to the Postgres pod as expected. The problem I'm having is having the Postgres data endure. If I mount the PV at var/lib/postgres the data gets overridden with each pod update. If I mount at var/lib/postgres/data I get the warning:

initdb: directory "/var/lib/postgresql/data" exists but is not empty It contains a lost+found directory, perhaps due to it being a mount point. Using a mount point directly as the data directory is not recommended. Create a subdirectory under the mount point.

Using Docker alone having the volume mount point at var/lib/postgresql/data works as expected and data endures, but I don't know what to do now in GKE. How does one set this up properly?

Setup file:

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: sm-pd-volume-claim
spec:
  storageClassName: "standard"
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1G
---
apiVersion: "apps/v1"
kind: "StatefulSet"
metadata:
  name: "postgis-db"
  namespace: "default"
  labels:
    app: "postgis-db"
spec:
  serviceName: "postgis-db"
  replicas: 1
  selector:
    matchLabels:
      app: "postgis-db"
  template:
    metadata:
      labels:
        app: "postgis-db"
    spec:
      terminationGracePeriodSeconds: 25
      containers:
      - name: "postgis"
        image: "mdillon/postgis"
        ports:
        - containerPort: 5432
          name: postgis-port
        volumeMounts:
        - name: sm-pd-volume
          mountPath: /var/lib/postgresql/data
      volumes:
      - name: sm-pd-volume
        persistentVolumeClaim:
          claimName: sm-pd-volume-claim

Upvotes: 2

Views: 1209

Answers (1)

Subramanian Manickam
Subramanian Manickam

Reputation: 1359

You are getting this error because the postgres pod has tried to mount the data directory on / folder. It is not recommended to do so.

You have to create subdirectory to resolve this issues on the statefulset manifest yaml files.

  volumeMounts:
    - name: sm-pd-volume
      mountPath: /var/lib/postgresql/data
      subPath: data

Upvotes: 11

Related Questions