---
apiVersion: v1
kind: Service
metadata:
  name: jackson-postgres
  labels:
    app: postgres
spec:
  type: ClusterIP
  ports:
    - name: postgres
      port: 5432
      targetPort: postgres
  selector:
    app: postgres
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: jackson-postgres
spec:
  selector:
    matchLabels:
      app: postgres
  serviceName: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:13
          imagePullPolicy: IfNotPresent
          env:
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
            - name: POSTGRES_USER
              value: postgres
            - name: POSTGRES_PASSWORD
              value: password
            - name: POSTGRES_DB
              value: postgres
          ports:
            - name: postgres
              containerPort: 5432
          livenessProbe:
            exec:
              command:
                - /bin/sh
                - -i
                - -c
                - /bin/sh
                - -i
                - -c
                - pg_isready -h 127.0.0.1 -p 5432
            initialDelaySeconds: 30
            timeoutSeconds: 5
            failureThreshold: 3
          readinessProbe:
            exec:
              command:
                - /bin/sh
                - -i
                - -c
                - pg_isready -h 127.0.0.1 -p 5432
            initialDelaySeconds: 5
            timeoutSeconds: 1
            periodSeconds: 10
          volumeMounts:
            - name: jackson-postgres-data
              mountPath: /var/lib/postgresql/data

      volumes:
        - name: jackson-postgres-data
          persistentVolumeClaim:
            claimName: jackson-postgres-data

  volumeClaimTemplates:
    - metadata:
        name: jackson-postgres-data
      spec:
        accessModes:
          - 'ReadWriteOnce'
        resources:
          requests:
            storage: 1Gi
