apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deploy
  namespace: voting-app-stack
  labels:
    name:  postgres-deploy
spec:
  replicas: 1
  
  selector:
    matchLabels:
      name: postgres-pod
      type: postgres-db

  template:
    metadata:
      labels:
        name: postgres-pod
        type: postgres-db
    spec:
      containers:
      - name: postgres
        image: postgres
        resources:
          limits:
            memory: "128Mi"
            cpu: "100m"
        env:
        - name: POSTGRES_USER
          value: postgres
        - name: POSTGRES_PASSWORD
          value: password
        - name: POSTGRES_DB
          value: voting-app-db
        - name: POSTGRES_HOST_AUTH_METHOD
          value: trust
        ports:
        - containerPort: 5432
          name: postgres
        livenessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - exec pg_isready -U "postgres" -h 127.0.0.1 -p 5432
          failureThreshold: 6
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        readinessProbe:
          exec:
            command:
            - /bin/sh
            - -c
            - -e
            - PGPASSWORD=password
            - exec psql -U "postgres" -d "voting-app-db" -p 5432 -c "SELECT 1"
          failureThreshold: 6
          initialDelaySeconds: 15
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgres-pv-claim
      volumes:
      - name: postgres-pv-claim
        persistentVolumeClaim:
          claimName: postgres-pv-claim