apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      service: postgres
  serviceName: postgres
  template:
    metadata:
      labels:
        service: postgres
    spec:
      containers:
      - image: postgres:12-alpine
        name: postgres
        ports:
        - containerPort: 5432
        livenessProbe:
          exec:
            command:
              - sh
              - -c
              - exec pg_isready -d $POSTGRES_DB -U $POSTGRES_USER --host $POD_IP
          failureThreshold: 6
          initialDelaySeconds: 60
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        readinessProbe:
          exec:
            command:
              - sh
              - -c
              - exec pg_isready -d $POSTGRES_DB -U $POSTGRES_USER --host $POD_IP
          failureThreshold: 3
          initialDelaySeconds: 5
          periodSeconds: 5
          successThreshold: 1
          timeoutSeconds: 3
        env:
        - name: POD_IP
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: status.podIP
        volumeMounts:
        - name: postgres-data
          mountPath: /var/lib/postgresql/data
          subPath: postgres
      volumes:
      - name: postgres-data
