apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgresql
  labels:
    app: keycloak
    role: persistence
spec:
  selector:
    matchLabels:
      app: keycloak
      role: persistence
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: keycloak
        role: persistence
    spec:
      containers:
      - name: postgresql
        image: postgres:9.6.19-alpine
        env:
        - name: POSTGRES_USER
          value: "keycloak"
          # Required for pg_isready in the health probes.
        - name: PGUSER
          value: "keycloak"
        - name: POSTGRES_DB
          value: ""
        - name: POSTGRES_INITDB_ARGS
          value: ""
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgresql
              key: postgres-password
        - name: POD_IP
          valueFrom: { fieldRef: { fieldPath: status.podIP } }
        ports:
        - name: postgresql
          containerPort: 5432
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - exec pg_isready --host $POD_IP
          initialDelaySeconds: 60
          timeoutSeconds: 5
          failureThreshold: 6
        readinessProbe:
          exec:
            command:
            - sh
            - -c
            - exec pg_isready --host $POD_IP
          initialDelaySeconds: 5
          timeoutSeconds: 3
          periodSeconds: 5
        resources:
          requests:
            cpu: 100m
            memory: 256Mi
        volumeMounts:
        - name: data
          mountPath: /var/lib/postgresql/data/pgdata
          subPath: postgresql-db
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: postgresql