|
1 | | -# Lab 06 — Production: postgresql HA-ready with monitoring and external volumes |
| 1 | +# Lab 06 -- Production: PostgreSQL HA with streaming replication, PgBouncer, Prometheus exporter |
2 | 2 | --- |
| 3 | +x-pg-common: &pg-common |
| 4 | + image: bitnami/postgresql:16 |
| 5 | + restart: always |
| 6 | + networks: |
| 7 | + - pg-prod-net |
| 8 | + |
3 | 9 | services: |
4 | | - postgresql: |
5 | | - image: postgres:16 |
6 | | - container_name: it-stack-postgresql |
7 | | - restart: always |
| 10 | + pg-primary: |
| 11 | + <<: *pg-common |
| 12 | + container_name: it-stack-pg-primary |
8 | 13 | ports: |
9 | | - - "5432:$firstPort" |
| 14 | + - "5432:5432" |
10 | 15 | environment: |
11 | | - - IT_STACK_ENV=production |
12 | | - - KEYCLOAK_URL= |
13 | | - - DB_HOST= |
14 | | - - REDIS_HOST= |
15 | | - - GRAYLOG_HOST= |
| 16 | + POSTGRESQL_REPLICATION_MODE: master |
| 17 | + POSTGRESQL_REPLICATION_USER: replicator |
| 18 | + POSTGRESQL_REPLICATION_PASSWORD: "Lab06Password!" |
| 19 | + POSTGRESQL_USERNAME: labadmin |
| 20 | + POSTGRESQL_PASSWORD: "Lab06Password!" |
| 21 | + POSTGRESQL_DATABASE: labapp |
| 22 | + POSTGRESQL_POSTGRES_PASSWORD: "Lab06Password!" |
16 | 23 | volumes: |
17 | | - - postgresql_data:/var/lib/postgresql |
18 | | - - /etc/ssl/certs:/etc/ssl/certs:ro |
| 24 | + - pg-primary-data:/bitnami/postgresql |
| 25 | + healthcheck: |
| 26 | + test: ["CMD-SHELL", "pg_isready -U labadmin -d labapp"] |
| 27 | + interval: 10s |
| 28 | + timeout: 5s |
| 29 | + retries: 10 |
| 30 | + start_period: 60s |
19 | 31 | deploy: |
20 | | - replicas: 1 |
21 | 32 | resources: |
22 | 33 | limits: |
23 | | - cpus: "4.0" |
24 | | - memory: G |
25 | | - reservations: |
26 | | - cpus: "1.0" |
27 | | - memory: 1G |
28 | | - restart_policy: |
29 | | - condition: any |
30 | | - delay: 5s |
31 | | - logging: |
32 | | - driver: gelf |
33 | | - options: |
34 | | - gelf-address: "udp://${GRAYLOG_HOST}:12201" |
35 | | - tag: "it-stack-postgresql" |
| 34 | + cpus: "2.0" |
| 35 | + memory: 2G |
| 36 | + |
| 37 | + pg-replica: |
| 38 | + <<: *pg-common |
| 39 | + container_name: it-stack-pg-replica |
| 40 | + ports: |
| 41 | + - "5433:5432" |
| 42 | + environment: |
| 43 | + POSTGRESQL_REPLICATION_MODE: slave |
| 44 | + POSTGRESQL_MASTER_HOST: pg-primary |
| 45 | + POSTGRESQL_MASTER_PORT_NUMBER: 5432 |
| 46 | + POSTGRESQL_REPLICATION_USER: replicator |
| 47 | + POSTGRESQL_REPLICATION_PASSWORD: "Lab06Password!" |
| 48 | + POSTGRESQL_USERNAME: labadmin |
| 49 | + POSTGRESQL_PASSWORD: "Lab06Password!" |
| 50 | + POSTGRESQL_POSTGRES_PASSWORD: "Lab06Password!" |
| 51 | + volumes: |
| 52 | + - pg-replica-data:/bitnami/postgresql |
| 53 | + depends_on: |
| 54 | + pg-primary: |
| 55 | + condition: service_healthy |
36 | 56 | healthcheck: |
37 | | - test: ["CMD-SHELL", "curl -sf http://localhost/health || exit 1"] |
38 | | - interval: 30s |
39 | | - timeout: 10s |
40 | | - retries: 3 |
| 57 | + test: ["CMD-SHELL", "pg_isready -U labadmin"] |
| 58 | + interval: 10s |
| 59 | + timeout: 5s |
| 60 | + retries: 20 |
41 | 61 | start_period: 120s |
| 62 | + deploy: |
| 63 | + resources: |
| 64 | + limits: |
| 65 | + cpus: "2.0" |
| 66 | + memory: 2G |
| 67 | + |
| 68 | + pgbouncer: |
| 69 | + image: bitnami/pgbouncer:1 |
| 70 | + container_name: it-stack-pgbouncer |
| 71 | + ports: |
| 72 | + - "6432:6432" |
| 73 | + environment: |
| 74 | + POSTGRESQL_HOST: pg-primary |
| 75 | + POSTGRESQL_PORT: 5432 |
| 76 | + POSTGRESQL_DATABASE: labapp |
| 77 | + POSTGRESQL_USERNAME: labadmin |
| 78 | + POSTGRESQL_PASSWORD: "Lab06Password!" |
| 79 | + PGBOUNCER_POOL_MODE: transaction |
| 80 | + PGBOUNCER_MAX_CLIENT_CONN: 200 |
| 81 | + PGBOUNCER_DEFAULT_POOL_SIZE: 20 |
| 82 | + PGBOUNCER_AUTH_TYPE: scram-sha-256 |
| 83 | + depends_on: |
| 84 | + pg-primary: |
| 85 | + condition: service_healthy |
| 86 | + healthcheck: |
| 87 | + test: ["CMD-SHELL", "pg_isready -h localhost -p 6432 -U labadmin"] |
| 88 | + interval: 15s |
| 89 | + timeout: 5s |
| 90 | + retries: 5 |
| 91 | + start_period: 30s |
42 | 92 | networks: |
43 | | - - it-stack-net |
| 93 | + - pg-prod-net |
| 94 | + deploy: |
| 95 | + resources: |
| 96 | + limits: |
| 97 | + cpus: "0.5" |
| 98 | + memory: 256M |
| 99 | + |
| 100 | + postgres-exporter: |
| 101 | + image: prometheuscommunity/postgres-exporter:latest |
| 102 | + container_name: it-stack-postgres-exporter |
| 103 | + ports: |
| 104 | + - "9187:9187" |
| 105 | + environment: |
| 106 | + DATA_SOURCE_NAME: "postgresql://labadmin:Lab06Password!@pg-primary:5432/labapp?sslmode=disable" |
| 107 | + PG_EXPORTER_DISABLE_SETTINGS_METRICS: "false" |
| 108 | + PG_EXPORTER_AUTO_DISCOVER_DATABASES: "true" |
| 109 | + depends_on: |
| 110 | + pg-primary: |
| 111 | + condition: service_healthy |
| 112 | + networks: |
| 113 | + - pg-prod-net |
| 114 | + deploy: |
| 115 | + resources: |
| 116 | + limits: |
| 117 | + cpus: "0.5" |
| 118 | + memory: 256M |
44 | 119 |
|
45 | 120 | networks: |
46 | | - it-stack-net: |
47 | | - external: true |
48 | | - name: it-stack-production |
| 121 | + pg-prod-net: |
| 122 | + driver: bridge |
49 | 123 |
|
50 | 124 | volumes: |
51 | | - postgresql_data: |
52 | | - external: true |
53 | | - name: it-stack-postgresql-data |
| 125 | + pg-primary-data: |
| 126 | + pg-replica-data: |
0 commit comments