apiVersion: v1
kind: PersistentVolume
metadata:
  name: psql-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /mnt/data/postgres
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: psql-volume-claim
  labels:
    type: local
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: psql-bp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: psql-bp
  template:
    metadata:
      labels:
        app: psql-bp
    spec:
      containers:
        - name: postgres
          image: postgres:latest
          env:
            - name: POSTGRES_DB
              valueFrom:
                configMapKeyRef:
                  name: fastbin-config
                  key: DB_DATABASE
            - name: POSTGRES_USER
              valueFrom:
                configMapKeyRef:
                  name: fastbin-config
                  key: DB_USERNAME
            - name: POSTGRES_PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: fastbin-config
                  key: DB_PASSWORD
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: psql-storage
          readinessProbe:
            exec:
              command:
                - sh
                - "-c"
                - "pg_isready -U $POSTGRES_USER -d $POSTGRES_DB"
            initialDelaySeconds: 5
            periodSeconds: 5
            timeoutSeconds: 5
            failureThreshold: 5
      volumes:
        - name: psql-storage
          persistentVolumeClaim:
            claimName: psql-volume-claim

---
apiVersion: v1
kind: Service
metadata:
  name: psql-service
spec:
  ports:
    - port: 5432
      targetPort: 5432
  selector:
    app: psql-bp
