apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  replicas: 1
  serviceName: postgres-internal
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      terminationGracePeriodSeconds: 0
      containers:
        - name: postgres
          image: postgres:11.5-alpine
          imagePullPolicy: Always
          ports:
            - name: tcp-pg
              containerPort: 5432
              protocol: TCP
          env:
            - name: POSTGRES_DB
              value: postgres
            - name: POSTGRES_USER
              value: postgres
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-secrets
                  key: postgres-password
          livenessProbe:
            exec:
              command: ["pg_isready", "-U", "$(POSTGRES_USER)"]
            initialDelaySeconds: 3
            timeoutSeconds: 2
          readinessProbe:
            exec:
              command: ["pg_isready", "-U", "$(POSTGRES_USER)"]
            initialDelaySeconds: 3
            timeoutSeconds: 2
          volumeMounts:
            - name: database-storage
              mountPath: /var/lib/postgresql/data
  volumeClaimTemplates:
    - metadata:
        name: postgres-storage
        labels:
          app: postgres
      spec:
        accessModes: ["ReadWriteOnce"]
        # storageClassName: <custom storage class>
        resources:
          requests:
            storage: 1Gi
