apiVersion: apps/v1
kind: Deployment
metadata:
  name: pgbouncer-example
  namespace: default
  labels:
    app: pgbouncer-example
spec:
  revisionHistoryLimit: 10  # removes old replicasets for deployment rollbacks
  strategy:
    rollingUpdate:
      maxUnavailable: 0  # Avoid Terminating and ContainerCreating at the same time
  selector:
    matchLabels:
      app: pgbouncer-example
  template:
    metadata:
      labels:
        app: pgbouncer-example
    spec:
      # Optional: keep the POD close to the apps:
      #nodeSelector:
      #  purpose: apps
      containers:
        - name: pgbouncer
          image: edoburu/pgbouncer:1.9.0
          #imagePullPolicy: Always
          ports:
            - containerPort: 5432
          envFrom:
            - secretRef:
               name: pgbouncer-example-env
          env:
            - name: POOL_MODE
              value: session
            - name: SERVER_RESET_QUERY
              value: DISCARD ALL
          livenessProbe:
            tcpSocket:
              port: 5432
            periodSeconds: 60
          lifecycle:
            preStop:
              exec:
                # Allow existing queries clients to complete within 120 seconds
                command: ["/bin/sh", "-c", "killall -INT pgbouncer && sleep 120"]
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop: ['all']
