forked from mtusi2026/mtusi2026lab3
feat(infra): add replicated API and readiness-aware load balancing
Provide resource profiles, persistent storage, parallel Nginx health checks and graceful restart hooks.
This commit is contained in:
+113
@@ -0,0 +1,113 @@
|
||||
services:
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
target: runtime
|
||||
restart: unless-stopped
|
||||
init: true
|
||||
cpus: '1.0'
|
||||
mem_limit: 512m
|
||||
deploy:
|
||||
replicas: 2
|
||||
command: ['sh', '-c', 'INSTANCE_ID=$$(hostname) exec node dist/main.js']
|
||||
environment:
|
||||
NODE_ENV: production
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: 8080
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||
DB_POOL_MAX: ${DB_POOL_MAX:-15}
|
||||
CACHE_URL: redis://redis:6379/0
|
||||
CACHE_PREFIX: ${CACHE_PREFIX:-messenger:cache:v1}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test:
|
||||
- CMD
|
||||
- curl
|
||||
- --fail
|
||||
- --silent
|
||||
- --show-error
|
||||
- --max-time
|
||||
- '2'
|
||||
- --output
|
||||
- /dev/null
|
||||
- http://127.0.0.1:8080/health/ready
|
||||
interval: 10s
|
||||
timeout: 3s
|
||||
start_period: 20s
|
||||
retries: 5
|
||||
stop_grace_period: 30s
|
||||
|
||||
nginx:
|
||||
image: nginx:1.30.6-alpine
|
||||
restart: unless-stopped
|
||||
cpus: '0.5'
|
||||
mem_limit: 128m
|
||||
ports:
|
||||
- '127.0.0.1:${APP_PORT:-8080}:80'
|
||||
volumes:
|
||||
- ./infra/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
- ./infra/start-nginx.sh:/etc/nginx/start-nginx.sh:ro
|
||||
entrypoint: ['/bin/sh', '/etc/nginx/start-nginx.sh']
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
|
||||
migrate:
|
||||
build:
|
||||
context: .
|
||||
target: migrate
|
||||
cpus: '1.0'
|
||||
mem_limit: 512m
|
||||
environment:
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
|
||||
postgres:
|
||||
image: postgres:17.6-bookworm
|
||||
restart: unless-stopped
|
||||
cpus: '1.0'
|
||||
mem_limit: 512m
|
||||
environment:
|
||||
POSTGRES_DB: ${POSTGRES_DB:-chat}
|
||||
POSTGRES_USER: ${POSTGRES_USER:-app}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-local_dev_password}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ['CMD-SHELL', 'pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
start_period: 10s
|
||||
retries: 10
|
||||
stop_grace_period: 30s
|
||||
|
||||
redis:
|
||||
image: redis:7.4.5-alpine
|
||||
restart: unless-stopped
|
||||
cpus: '0.5'
|
||||
mem_limit: 256m
|
||||
command:
|
||||
['redis-server', '--appendonly', 'yes', '--appendfsync', 'everysec']
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
healthcheck:
|
||||
test: ['CMD', 'redis-cli', 'ping']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
stop_grace_period: 30s
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
Reference in New Issue
Block a user