Repository navigation
Expand file tree
/
Copy pathdocker-compose.yaml
More file actions
680 lines (646 loc) · 20.1 KB
/
Copy pathdocker-compose.yaml
File metadata and controls
680 lines (646 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
services:
nginx:
image: nginx:alpine
container_name: clean_arch_nginx
ports:
- "8000:80"
volumes:
- ./deploy/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
api-1:
condition: service_healthy
api-2:
condition: service_healthy
api-3:
condition: service_healthy
api-4:
condition: service_healthy
api-5:
condition: service_healthy
api-6:
condition: service_healthy
api-7:
condition: service_healthy
api-8:
condition: service_healthy
restart: unless-stopped
seeder:
build: .
container_name: clean_arch_seeder
env_file: .env.docker
environment:
ELASTICSEARCH_ENABLED: ${ELASTICSEARCH_ENABLED:-false}
command: ["python", "scripts/seed_etcd_config.py"]
depends_on:
etcd:
condition: service_healthy
migrator:
build: .
container_name: clean_arch_migrator
env_file: .env.docker
command: ["alembic", "upgrade", "head"]
environment:
DATABASE_URL: postgresql+asyncpg://library:library_secret@postgres:5432/library_db
depends_on:
postgres:
condition: service_healthy
postgres-extension-init:
condition: service_completed_successfully
seeder:
condition: service_completed_successfully
api-1: &api-service
build: .
container_name: clean_arch_api_1
env_file: .env.docker
depends_on:
migrator:
condition: service_completed_successfully
pgbouncer:
condition: service_healthy
redis:
condition: service_healthy
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/health/ready')"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
restart: unless-stopped
api-2:
<<: *api-service
container_name: clean_arch_api_2
api-3:
<<: *api-service
container_name: clean_arch_api_3
api-4:
<<: *api-service
container_name: clean_arch_api_4
api-5:
<<: *api-service
container_name: clean_arch_api_5
api-6:
<<: *api-service
container_name: clean_arch_api_6
api-7:
<<: *api-service
container_name: clean_arch_api_7
api-8:
<<: *api-service
container_name: clean_arch_api_8
postgres:
image: postgres:16-alpine
container_name: clean_arch_postgres
ports:
- "5432:5432"
environment:
POSTGRES_USER: library
POSTGRES_PASSWORD: library_secret
POSTGRES_DB: library_db
POSTGRES_HOST_AUTH_METHOD: trust
volumes:
- postgres_data:/var/lib/postgresql/data
command:
- "postgres"
- "-c"
- "max_connections=500"
- "-c"
- "shared_buffers=512MB"
- "-c"
- "effective_cache_size=1GB"
- "-c"
- "work_mem=32MB"
- "-c"
- "wal_level=logical"
- "-c"
- "max_wal_senders=4"
- "-c"
- "max_replication_slots=4"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U library -d library_db"]
interval: 10s
timeout: 5s
retries: 5
restart: unless-stopped
postgres-extension-init:
image: postgres:16-alpine
container_name: clean_arch_postgres_extension_init
environment:
PGPASSWORD: library_secret
command:
- "sh"
- "-ec"
- >-
psql -h postgres -U library -d library_db
-v ON_ERROR_STOP=1
-c 'CREATE EXTENSION IF NOT EXISTS pg_trgm'
depends_on:
postgres:
condition: service_healthy
restart: "no"
pgbouncer:
image: edoburu/pgbouncer:latest
container_name: clean_arch_pgbouncer
ports:
- "6432:6432"
environment:
DB_HOST: postgres
DB_PORT: 5432
DB_USER: library
DB_PASSWORD: library_secret
DB_NAME: library_db
LISTEN_PORT: 6432
AUTH_TYPE: trust
POOL_MODE: transaction
MAX_CLIENT_CONN: 10000
MAX_DB_CONNECTIONS: 300
DEFAULT_POOL_SIZE: 200
MIN_POOL_SIZE: 20
IGNORE_STARTUP_PARAMETERS: extra_float_digits
depends_on:
postgres:
condition: service_healthy
healthcheck:
test: ["CMD-SHELL", "pg_isready -h localhost -p 6432 -U library -d library_db || exit 1"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
restart: unless-stopped
redis:
image: redis:7-alpine
container_name: clean_arch_redis
ports:
- "6379:6379"
command: redis-server --maxmemory 256mb --maxmemory-policy allkeys-lru
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 5
restart: unless-stopped
etcd:
image: quay.io/coreos/etcd:v3.5.17
container_name: clean_arch_etcd
ports:
- "2379:2379"
- "2380:2380"
environment:
ETCD_NAME: etcd0
ETCD_DATA_DIR: /etcd-data
ETCD_ADVERTISE_CLIENT_URLS: http://0.0.0.0:2379
ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
ETCD_LISTEN_PEER_URLS: http://0.0.0.0:2380
ETCD_INITIAL_ADVERTISE_PEER_URLS: http://0.0.0.0:2380
ETCD_INITIAL_CLUSTER: etcd0=http://0.0.0.0:2380
ETCD_INITIAL_CLUSTER_STATE: new
ETCD_INITIAL_CLUSTER_TOKEN: etcd-cluster-1
volumes:
- etcd_data:/etcd-data
healthcheck:
test: ["CMD", "etcdctl", "endpoint", "health"]
interval: 10s
timeout: 5s
retries: 5
restart: unless-stopped
# Domain-event pipeline (default) and optional CDC read-model services
zookeeper:
image: confluentinc/cp-zookeeper:7.5.0
container_name: clean_arch_zookeeper
ports:
- "2181:2181"
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
volumes:
- zookeeper_data:/var/lib/zookeeper/data
- zookeeper_log:/var/lib/zookeeper/log
healthcheck:
test: ["CMD", "nc", "-z", "localhost", "2181"]
interval: 10s
timeout: 5s
retries: 5
restart: unless-stopped
kafka:
image: confluentinc/cp-kafka:7.5.0
container_name: clean_arch_kafka
ports:
- "9092:9092"
- "29092:29092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
# Correctness events remain replayable for 30 days. The database outbox
# retains 90 days, leaving another 60-day outage/recovery margin.
KAFKA_LOG_RETENTION_HOURS: 720
# Off, as in production: topic shape is declared by the Debezium
# connectors (topic.creation.*) and DLQ topics are created by the
# messaging layer on first use. With auto-create on, any consumer
# metadata request races the connector and wins with 1 partition.
KAFKA_AUTO_CREATE_TOPICS_ENABLE: "false"
volumes:
- kafka_data:/var/lib/kafka/data
depends_on:
zookeeper:
condition: service_healthy
healthcheck:
test: ["CMD", "kafka-broker-api-versions", "--bootstrap-server", "localhost:9092"]
interval: 10s
timeout: 10s
retries: 10
start_period: 60s
restart: unless-stopped
kafka-domain-topic-init:
image: confluentinc/cp-kafka:7.5.0
container_name: clean_arch_kafka_domain_topic_init
command:
- bash
- -ec
- |
for topic in outbox.event.book outbox.event.loan outbox.event.patron; do
kafka-topics --bootstrap-server kafka:29092 \
--create --if-not-exists --topic "$$topic" \
--partitions 4 --replication-factor 1
kafka-configs --bootstrap-server kafka:29092 \
--entity-type topics --entity-name "$$topic" --alter \
--add-config retention.ms=2592000000
done
depends_on:
kafka:
condition: service_healthy
restart: "no"
debezium:
image: debezium/connect:2.4
container_name: clean_arch_debezium
ports:
- "8083:8083"
environment:
GROUP_ID: 1
CONFIG_STORAGE_TOPIC: debezium_configs
CONFIG_STORAGE_REPLICATION_FACTOR: 1
OFFSET_STORAGE_TOPIC: debezium_offsets
OFFSET_STORAGE_REPLICATION_FACTOR: 1
STATUS_STORAGE_TOPIC: debezium_statuses
STATUS_STORAGE_REPLICATION_FACTOR: 1
BOOTSTRAP_SERVERS: kafka:29092
KEY_CONVERTER: org.apache.kafka.connect.json.JsonConverter
VALUE_CONVERTER: org.apache.kafka.connect.json.JsonConverter
KEY_CONVERTER_SCHEMAS_ENABLE: "false"
VALUE_CONVERTER_SCHEMAS_ENABLE: "false"
depends_on:
kafka:
condition: service_healthy
postgres:
condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8083/connectors"]
interval: 10s
timeout: 5s
retries: 10
start_period: 60s
restart: unless-stopped
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.11.0
platform: linux/amd64
container_name: clean_arch_elasticsearch
ports:
- "9200:9200"
- "9300:9300"
environment:
- discovery.type=single-node
- xpack.security.enabled=false
- "ES_JAVA_OPTS=-Xms4g -Xmx4g -XX:MaxGCPauseMillis=200"
mem_limit: 6g
volumes:
- elasticsearch_data:/usr/share/elasticsearch/data
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9200/_cluster/health"]
interval: 10s
timeout: 5s
retries: 10
start_period: 60s
restart: unless-stopped
profiles:
- cdc
kibana:
image: docker.elastic.co/kibana/kibana:8.11.0
platform: linux/amd64
container_name: clean_arch_kibana
ports:
- "5601:5601"
environment:
ELASTICSEARCH_HOSTS: http://elasticsearch:9200
depends_on:
elasticsearch:
condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:5601/api/status"]
interval: 10s
timeout: 5s
retries: 10
start_period: 60s
restart: unless-stopped
profiles:
- cdc
debezium-outbox-init:
image: curlimages/curl:latest
container_name: clean_arch_debezium_outbox_init
volumes:
- ./deploy/debezium:/config:ro
# The outbox connector is part of business correctness. Recreate it from
# the reviewed config (offsets remain in Kafka), then block until both the
# connector and its task are RUNNING. "Connector exists" is not health.
command:
- sh
- -ec
- |
if curl -fsS http://debezium:8083/connectors/outbox-connector >/dev/null; then
echo 'Replacing Debezium outbox connector with reviewed config...'
curl -fsS -X DELETE \
http://debezium:8083/connectors/outbox-connector
while curl -fsS \
http://debezium:8083/connectors/outbox-connector >/dev/null; do
sleep 1
done
fi
until curl -fsS -X POST -H 'Content-Type: application/json' \
--data @/config/register-outbox-connector.json \
http://debezium:8083/connectors; do
echo 'Outbox connector registration not accepted yet; retrying...'
sleep 2
done
echo 'Waiting for outbox connector and task to be RUNNING...'
while true; do
status="$$(curl -fsS \
http://debezium:8083/connectors/outbox-connector/status || true)"
running="$$(echo "$$status" | grep -o '"state":"RUNNING"' | wc -l)"
if [ "$$running" -ge 2 ] && \
! echo "$$status" | grep -Eq '"state":"(FAILED|UNASSIGNED|PAUSED)"'; then
echo 'Debezium outbox connector is healthy'
break
fi
echo "Outbox connector not ready: $$status"
sleep 2
done
depends_on:
debezium:
condition: service_healthy
kafka-domain-topic-init:
condition: service_completed_successfully
migrator:
condition: service_completed_successfully
debezium-outbox-monitor:
image: curlimages/curl:latest
container_name: clean_arch_debezium_outbox_monitor
command: ["sh", "-c", "while true; do sleep 30; done"]
depends_on:
debezium-outbox-init:
condition: service_completed_successfully
healthcheck:
test:
- CMD-SHELL
- >-
status="$$(curl -fsS
http://debezium:8083/connectors/outbox-connector/status)" &&
test "$$(echo "$$status" | grep -o '"state":"RUNNING"' | wc -l)" -ge 2 &&
! echo "$$status" | grep -Eq '"state":"(FAILED|UNASSIGNED|PAUSED)"'
interval: 15s
timeout: 5s
retries: 3
start_period: 15s
restart: unless-stopped
debezium-cdc-init:
image: curlimages/curl:latest
container_name: clean_arch_debezium_cdc_init
volumes:
- ./deploy/debezium:/config:ro
# Table CDC only feeds the optional Elasticsearch projection.
command:
- sh
- -ec
- |
if curl -fsS http://debezium:8083/connectors/library-connector >/dev/null; then
echo 'Debezium table CDC connector already registered'
else
echo 'Registering Debezium table CDC connector...'
curl -fsS -X POST -H 'Content-Type: application/json' \
--data @/config/register-connector.json \
http://debezium:8083/connectors
fi
depends_on:
debezium:
condition: service_healthy
migrator:
condition: service_completed_successfully
profiles:
- cdc
elasticsearch-init:
image: curlimages/curl:latest
container_name: clean_arch_elasticsearch_init
volumes:
- ./deploy/elasticsearch/mappings:/mappings:ro
command: ["sh", "-c", "echo 'Creating Elasticsearch indices...' && curl -s -X PUT http://elasticsearch:9200/books-000001 -H 'Content-Type: application/json' -d @/mappings/books.json || true && curl -s -X PUT http://elasticsearch:9200/patrons-000001 -H 'Content-Type: application/json' -d @/mappings/patrons.json || true && curl -s -X PUT http://elasticsearch:9200/loans-000001 -H 'Content-Type: application/json' -d @/mappings/loans.json || true && echo 'Indices setup complete!'"]
depends_on:
elasticsearch:
condition: service_healthy
profiles:
- cdc
es-sync:
build: .
# No container_name: this service scales horizontally. Instances share
# the consumer group; Kafka spreads partitions across them.
deploy:
replicas: 2
env_file: .env.docker
command: ["python", "scripts/run_es_sync.py"]
environment:
ETCD_HOST: etcd
ETCD_PORT: 2379
depends_on:
kafka:
condition: service_healthy
elasticsearch-init:
condition: service_completed_successfully
debezium-cdc-init:
condition: service_completed_successfully
etcd:
condition: service_healthy
restart: unless-stopped
profiles:
- cdc
event-worker:
build: .
# No container_name: this service scales horizontally. Instances share
# the consumer group; Kafka spreads partitions across them, and per-book
# ordering survives because events are keyed by aggregate id.
deploy:
replicas: 2
env_file: .env.docker
command: ["python", "scripts/run_event_worker.py"]
environment:
ETCD_HOST: etcd
ETCD_PORT: 2379
depends_on:
kafka:
condition: service_healthy
debezium-outbox-init:
condition: service_completed_successfully
etcd:
condition: service_healthy
pgbouncer:
condition: service_healthy
healthcheck:
test: ["CMD", "python", "-c", "assert 'run_event_worker.py' in open('/proc/1/cmdline').read()"]
interval: 15s
timeout: 5s
retries: 3
restart: unless-stopped
notification-worker:
build: .
# Optional email delivery has a separate consumer group and process. A
# SendGrid outage cannot head-of-line block Catalog/Lending convergence.
deploy:
replicas: 2
env_file: .env.docker
command: ["python", "scripts/run_notification_worker.py"]
environment:
ETCD_HOST: etcd
ETCD_PORT: 2379
depends_on:
kafka:
condition: service_healthy
debezium-outbox-init:
condition: service_completed_successfully
etcd:
condition: service_healthy
pgbouncer:
condition: service_healthy
healthcheck:
test: ["CMD", "python", "-c", "assert 'run_notification_worker.py' in open('/proc/1/cmdline').read()"]
interval: 15s
timeout: 5s
retries: 3
restart: unless-stopped
reservation-reaper:
build: .
container_name: clean_arch_reservation_reaper
env_file: .env.docker
command: ["python", "scripts/run_reservation_reaper.py"]
environment:
ETCD_HOST: etcd
ETCD_PORT: 2379
depends_on:
migrator:
condition: service_completed_successfully
etcd:
condition: service_healthy
pgbouncer:
condition: service_healthy
healthcheck:
test:
- CMD
- python
- -c
- >-
import os,time;
path='/tmp/reservation-reaper-heartbeat';
limit=max(180,int(os.getenv('RESERVATION_REAPER_INTERVAL_SECONDS','60'))*3);
assert os.path.exists(path) and time.time()-os.path.getmtime(path)<limit
interval: 30s
timeout: 5s
retries: 3
start_period: 120s
restart: unless-stopped
outbox-cleaner:
build: .
container_name: clean_arch_outbox_cleaner
env_file: .env.docker
command:
- sh
- -ec
- |
while true; do
python scripts/cleanup_outbox.py \
--retention-hours "$${OUTBOX_RETENTION_HOURS:-2160}" \
--batch-size "$${OUTBOX_CLEANUP_BATCH_SIZE:-500}" \
--max-batches "$${OUTBOX_CLEANUP_MAX_BATCHES:-20}" \
--max-slot-lag-bytes "$${OUTBOX_MAX_SLOT_LAG_BYTES:-1073741824}" \
--replication-slot "$${DEBEZIUM_OUTBOX_SLOT_NAME:-library_outbox_slot}"
sleep "$${OUTBOX_CLEANUP_INTERVAL_SECONDS:-3600}"
done
environment:
ETCD_HOST: etcd
ETCD_PORT: 2379
depends_on:
# Do not start retention until the outbox connector has successfully
# registered its durable slot and both connector/task report RUNNING.
debezium-outbox-init:
condition: service_completed_successfully
etcd:
condition: service_healthy
pgbouncer:
condition: service_healthy
restart: unless-stopped
durable-state-cleaner:
build: .
container_name: clean_arch_durable_state_cleaner
env_file: .env.docker
command:
- sh
- -ec
- |
while true; do
python scripts/cleanup_durable_state.py \
--command-receipt-days "$${COMMAND_RECEIPT_RETENTION_DAYS:-30}" \
--terminal-operation-days "$${TERMINAL_OPERATION_RETENTION_DAYS:-120}" \
--processed-inbox-days "$${PROCESSED_INBOX_RETENTION_DAYS:-120}" \
--quarantine-days "$${EVENT_QUARANTINE_RETENTION_DAYS:-365}" \
--batch-size "$${DURABLE_STATE_CLEANUP_BATCH_SIZE:-500}" \
--max-batches "$${DURABLE_STATE_CLEANUP_MAX_BATCHES:-20}"
sleep "$${DURABLE_STATE_CLEANUP_INTERVAL_SECONDS:-86400}"
done
environment:
ETCD_HOST: etcd
ETCD_PORT: 2379
depends_on:
migrator:
condition: service_completed_successfully
etcd:
condition: service_healthy
pgbouncer:
condition: service_healthy
restart: unless-stopped
locust-master:
image: locustio/locust
container_name: clean_arch_locust_master
ports:
- "8089:8089"
volumes:
- ./tests/load:/mnt/locust
command: -f /mnt/locust/run_stages.py --master -H http://nginx:80
depends_on:
nginx:
condition: service_started
profiles:
- loadtest
locust-worker:
image: locustio/locust
volumes:
- ./tests/load:/mnt/locust
command: -f /mnt/locust/run_stages.py --worker --master-host locust-master
depends_on:
- locust-master
profiles:
- loadtest
volumes:
postgres_data:
etcd_data:
zookeeper_data:
zookeeper_log:
kafka_data:
elasticsearch_data: