apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgresql-main
  namespace: warehouse
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: postgresql
  serviceName: "postgresql"
  replicas: 1
  template:
    metadata:
      labels:
        app.kubernetes.io/name: postgresql
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: postgres-exporter
        image: "wrouesnel/postgres_exporter:v0.8.0"
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9187
          name: http
        env:
          - name: PG_EXPORTER_DISABLE_SETTINGS_METRICS
            value: "true"
          - name: PG_EXPORTER_AUTO_DISCOVER_DATABASES
            value: "true"
          - name: DATA_SOURCE_NAME
            value: "postgresql://postgres@:5432/postgres?host=/var/run/postgresql/&sslmode=disable"
        resources:
          requests:
            cpu: 25m
            memory: 64Mi
        volumeMounts:
        - name: postgresql-run
          mountPath: /var/run/postgresql
      - name: postgres
        image: "rascal/postgres:12.2-0.2.14"
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 5432
          name: pqsql
        env:
          - name: POSTGRES_INITDB_ARGS
            value: "--data-checksums"
          - name: PGDATA
            value: "/data/db"
          - name: POSTGRES_INITDB_WALDIR
            value: "/data/wal"
          - name: POSTGRES_USER
            valueFrom:
              secretKeyRef:
                name: postgresql-creds
                key: user
          - name: POSTGRES_PASSWORD
            valueFrom:
              secretKeyRef:
                name: postgresql-creds
                key: password
        resources:
          requests:
            cpu: 500m
            memory: 4Gi
        volumeMounts:
        - name: postgresql-data
          mountPath: /data
        - name: postgresql-run
          mountPath: /var/run/postgresql
        - name: wal-g
          mountPath: /var/lib/postgresql/wal-g
      nodeSelector:
        kubernetes.io/os: linux
        kubernetes.io/arch: amd64 
      volumes:
        - name: wal-g
          secret:
            secretName: wal-g-config
        - name: postgresql-data
#          persistentVolumeClaim:
#            claimName: postgresql-data-pvc
          hostPath:
           path: /var/lib/postgresql/12
           type: DirectoryOrCreate
        - name: postgresql-run
          emptyDir: {}
