Skip to content

Commit 0c8a400

Browse files
committed
feat(lab-06): PostgreSQL production -- HA replication (bitnami), PgBouncer, postgres-exporter
1 parent 78f6ecb commit 0c8a400

File tree

3 files changed

+260
-98
lines changed

3 files changed

+260
-98
lines changed

.github/workflows/ci.yml

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ jobs:
3737
echo "Validating: docker/docker-compose.integration.yml"
3838
docker compose -f docker/docker-compose.integration.yml config -q
3939
echo "OK: docker/docker-compose.integration.yml"
40-
for f in docker/docker-compose.production.yml; do
41-
echo "Checking scaffold: $f"
42-
docker compose -f "$f" config --no-interpolate -q 2>&1 && echo "OK: $f" \
43-
|| echo "WARN: $f has placeholder variables (scaffold — not yet built out)"
44-
done
40+
echo "Validating: docker/docker-compose.production.yml"
41+
docker compose -f docker/docker-compose.production.yml config -q
42+
echo "OK: docker/docker-compose.production.yml"
4543
4644
- name: ShellCheck — lab test scripts
4745
run: |
@@ -262,4 +260,40 @@ jobs:
262260

263261
- name: Cleanup
264262
if: always()
265-
run: docker compose -f docker/docker-compose.integration.yml down -v
263+
run: docker compose -f docker/docker-compose.integration.yml down -v
264+
265+
lab-06-smoke:
266+
name: Lab 06 — PostgreSQL Production HA (primary+replica+PgBouncer+exporter)
267+
runs-on: ubuntu-latest
268+
needs: validate
269+
continue-on-error: true
270+
steps:
271+
- uses: actions/checkout@v4
272+
273+
- name: Install tools
274+
run: sudo apt-get install -y postgresql-client curl
275+
276+
- name: Start production stack
277+
run: docker compose -f docker/docker-compose.production.yml up -d
278+
279+
- name: Wait for PG primary
280+
run: timeout 120 bash -c 'until pg_isready -h localhost -p 5432 -U labadmin; do sleep 3; done'
281+
282+
- name: Wait for PG replica
283+
run: timeout 60 bash -c 'until pg_isready -h localhost -p 5433 -U labadmin; do sleep 3; done'
284+
285+
- name: Wait for PgBouncer
286+
run: timeout 60 bash -c 'until pg_isready -h localhost -p 6432 -U labadmin; do sleep 3; done'
287+
288+
- name: Run Lab 03-06 test script
289+
env:
290+
PG_PASS: "Lab06Password!"
291+
run: bash tests/labs/test-lab-03-06.sh
292+
293+
- name: Collect logs on failure
294+
if: failure()
295+
run: docker compose -f docker/docker-compose.production.yml logs
296+
297+
- name: Cleanup
298+
if: always()
299+
run: docker compose -f docker/docker-compose.production.yml down -v
Lines changed: 111 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,126 @@
1-
# Lab 06 Production: postgresql HA-ready with monitoring and external volumes
1+
# Lab 06 -- Production: PostgreSQL HA with streaming replication, PgBouncer, Prometheus exporter
22
---
3+
x-pg-common: &pg-common
4+
image: bitnami/postgresql:16
5+
restart: always
6+
networks:
7+
- pg-prod-net
8+
39
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
813
ports:
9-
- "5432:$firstPort"
14+
- "5432:5432"
1015
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!"
1623
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
1931
deploy:
20-
replicas: 1
2132
resources:
2233
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
3656
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
4161
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
4292
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
44119

45120
networks:
46-
it-stack-net:
47-
external: true
48-
name: it-stack-production
121+
pg-prod-net:
122+
driver: bridge
49123

50124
volumes:
51-
postgresql_data:
52-
external: true
53-
name: it-stack-postgresql-data
125+
pg-primary-data:
126+
pg-replica-data:

0 commit comments

Comments
 (0)