apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: recipes
    tier: database
  name: recipes-postgresql
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: recipes
  serviceName: recipes-postgresql
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: recipes
        tier: database
      name: recipes-postgresql
      namespace: default
    spec:
      containers:      
      - name: recipes-db
        image: postgres:14.2-alpine3.15
        imagePullPolicy: IfNotPresent      
        env:
        - name: PGDATA
          value: /data/pgdata
        - name: POSTGRES_USER
          value: recipes
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: default-secrets
              key: postgresql-password
        - name: POSTGRES_DB
          value: recipes
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - exec pg_isready -U "recipes" -d "recipes" -h 127.0.0.1 -p 5432
          failureThreshold: 6
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        ports:
        - containerPort: 5432
          name: postgresql
          protocol: TCP
        readinessProbe:
          exec:
            command:
            - sh
            - -c
            - -e
            - pg_isready -U "recipes" -d "recipes" -h 127.0.0.1 -p 5432
          failureThreshold: 6
          initialDelaySeconds: 5
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        resources:
          requests:
            cpu: 250m
            memory: 256Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: "/data/"
          name: data
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      serviceAccount: recipes
      serviceAccountName: recipes
      terminationGracePeriodSeconds: 30
  volumeClaimTemplates:
  - apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: data
      namespace: default
    spec:
      accessModes:
      - ReadWriteOnce
      storageClassName: nfs-client      
      resources:
        requests:
          storage: 5Gi