apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
  labels:
    app: postgres
    component: database
spec:
  serviceName: postgres
  selector:
    matchLabels:
      app: postgres
      component: database
  template:
    metadata:
      labels:
        app: postgres
        component: database
    spec:
      securityContext:
        runAsUser: 70 # postgres user
        runAsGroup: 70
        fsGroup: 70
      containers:
        - name: postgres
          image: postgres:14.1-alpine3.15
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgres-config
            - secretRef:
                name: postgres-secret
          volumeMounts:
            - name: postgres-config
              mountPath: /var/config
            - name: postgres-data
              mountPath: /var/lib/postgresql/data
          resources:
            limits:
              cpu: "200m"
              memory: "256Mi"
          livenessProbe:
            exec:
              command:
                - "/bin/sh"
                - "-c"
                - "exec pg_isready -h 127.0.0.1 -p 5432"
            initialDelaySeconds: 5
            periodSeconds: 5
      volumes:
        - name: postgres-config
          configMap:
            name: postgres-config
  volumeClaimTemplates:
    - metadata:
        name: postgres-data
        labels:
          app: postgres
          component: database
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 500Mi
