apiVersion: v1 kind: PersistentVolumeClaim metadata: name: postgres namespace: storage spec: storageClassName: longhorn-static accessModes: - ReadWriteMany resources: requests: storage: 5Gi --- apiVersion: apps/v1 kind: Deployment metadata: name: postgres namespace: storage labels: app: postgres annotations: reloader.stakater.com/auto: "true" spec: replicas: 1 revisionHistoryLimit: 1 strategy: type: Recreate selector: matchLabels: app: postgres template: metadata: labels: app: postgres spec: terminationGracePeriodSeconds: 10 containers: - name: postgres image: postgres:latest args: ['-c', 'config_file=/etc/postgresql/postgresql.conf', '-c', 'hba_file=/etc/postgresql/pg_hba.conf'] imagePullPolicy: IfNotPresent envFrom: - configMapRef: name: postgres - secretRef: name: postgres env: - name: POD_IP valueFrom: fieldRef: apiVersion: v1 fieldPath: status.podIP volumeMounts: - name: postgres-files mountPath: /etc/postgresql/postgresql.conf subPath: postgresql-master.conf readOnly: true - name: postgres-files mountPath: /etc/postgresql/pg_hba.conf subPath: pg_hba.conf readOnly: true - name: postgres-files mountPath: /docker-entrypoint-initdb.d/create-replica-user.sh subPath: create-replica-user.sh readOnly: true - name: postgres-data mountPath: /var/lib/postgresql/data readOnly: false 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 volumes: - name: postgres-files configMap: name: postgres-files - name: postgres-data persistentVolumeClaim: claimName: postgres --- # apiVersion: apps/v1 # kind: StatefulSet # metadata: # name: postgres-replica # namespace: storage # labels: # app: postgres-replica # annotations: # reloader.stakater.com/auto: "true" # spec: # selector: # matchLabels: # app: postgres-replica # replicas: 1 # serviceName: postgres-replica # updateStrategy: # type: RollingUpdate # template: # metadata: # labels: # app: postgres-replica # spec: # terminationGracePeriodSeconds: 10 # initContainers: # - name: setup-replica-data # image: postgres:latest # imagePullPolicy: IfNotPresent # env: # - name: PGPASSWORD # valueFrom: # secretKeyRef: # name: postgres # key: REPLICATION_PASSWORD # 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: # - name: postgres-replica-data # mountPath: /etc/postgresql/data # readOnly: false # containers: # - name: postgres-replica # image: postgres:latest # args: ['-c', 'config_file=/etc/postgresql/postgresql.conf'] # imagePullPolicy: IfNotPresent # envFrom: # - configMapRef: # name: postgres # - secretRef: # name: postgres # env: # - name: POD_IP # valueFrom: # fieldRef: # apiVersion: v1 # fieldPath: status.podIP # volumeMounts: # - name: postgres-files # mountPath: /etc/postgresql/postgresql.conf # subPath: postgresql-replica.conf # readOnly: true # - name: postgres-replica-data # mountPath: /var/lib/postgresql/data # readOnly: false # 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 # volumes: # - name: postgres-files # configMap: # name: postgres-files # volumeClaimTemplates: # - metadata: # name: postgres-replica-data # namespace: storage # spec: # storageClassName: longhorn-static # accessModes: # - ReadWriteOnce # resources: # requests: # storage: 1Gi --- apiVersion: v1 kind: Service metadata: name: postgres namespace: storage spec: type: LoadBalancer externalTrafficPolicy: Cluster selector: app: postgres ports: - name: db port: 5432 protocol: TCP --- # apiVersion: v1 # kind: Service # metadata: # name: postgres-replica # namespace: storage # spec: # type: LoadBalancer # externalTrafficPolicy: Cluster # selector: # app: postgres-replica # ports: # - name: db # port: 5432 # protocol: TCP