apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres-replica
  namespace: postgres
spec:
  updateStrategy:
    type: RollingUpdate
  
  selector:
    matchLabels:
      app: postgres-replica

  serviceName: postgres-replica
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres-replica 
      namespace: postgres
    spec:
      volumes:
        - name: postgres-config
          configMap:
            name: postgres
            
      terminationGracePeriodSeconds: 10
      
      initContainers:
        - name: setup-replica-data-directory
          image: postgres:10.5
          
          env:
            - name: PGPASSWORD
              valueFrom:
                secretKeyRef:
                  key: replicaPassword
                  name: postgres

          command:
          - sh
          - -c
          - |
            if [ -z "$(ls -A /var/lib/postgresql/data/pgdata)" ]; then
                echo "Running pg_basebackup to catch up replication server...";
                pg_basebackup -R -h postgres -D /var/lib/postgresql/data/pgdata -P -U replication; 
                chown -R postgres:postgres $PGDATA;
            else
                echo "Skipping pg_basebackup because directory is not empty"; 
            fi

          volumeMounts:
            - mountPath: /var/lib/postgresql/data/pgdata
              name: postgres-replica
              subPath: postgres-db

      containers:
        - name: postgres-replica
          image: postgres:9.6
          args: ['-c', 'config_file=/etc/postgres.conf']
          
          imagePullPolicy: IfNotPresent
        
          ports:
            - name: postgres-rep
              containerPort: 5432
              protocol: TCP
          
          resources:
            requests:
              cpu: 100m
              memory: 256Mi
          
          env:
            - name: POSTGRES_USER
              value: postgres
          
            - name: PGUSER
              value: postgres
          
            - name: POSTGRES_DB
              value: postgres
            
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  key: password
                  name: postgres
              
            - name: POD_IP
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: status.podIP
              
          livenessProbe:
            exec:
              command:
                - sh
                - -c
                - exec pg_isready --host $POD_IP
            failureThreshold: 6
            initialDelaySeconds: 60
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5

          readinessProbe:
            exec:
              command:
                - sh
                - -c
                - exec pg_isready --host $POD_IP
            failureThreshold: 3
            initialDelaySeconds: 5
            periodSeconds: 5
            successThreshold: 1
            timeoutSeconds: 3

          volumeMounts:
            - mountPath: /var/lib/postgresql/data/pgdata
              name: postgres-replica
              subPath: postgres-db
            
            - name: postgres-config
              mountPath: /etc/postgres.conf
              subPath: postgres.conf

            - name: postgres-config
              mountPath: /etc/replica.conf
              subPath: replica.conf
            
      
  volumeClaimTemplates:
  - metadata:
      name: postgres
      namespace: postgres
    spec:
      storageClassName: managed-nfs-storage #change with your storageclass
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 2Gi

