forked from mtusi2026/mtusi2026lab3
feat: add resource limits, per-service secrets and failover/rolling experiments
This commit is contained in:
+35
-9
@@ -10,11 +10,20 @@ services:
|
||||
- -ec
|
||||
- |
|
||||
cd /secrets
|
||||
[ -s pg_password ] || head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n' > pg_password
|
||||
printf 'postgresql://messenger:%s@postgres:5432/messenger?connection_limit=10&pool_timeout=5&connect_timeout=5&socket_timeout=10&application_name=api&options=-c%%20statement_timeout%%3D5000' "$$(cat pg_password)" > database_url
|
||||
[ -s redis_password ] || head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n' > redis_password
|
||||
printf 'redis://:%s@redis:6379/0' "$$(cat redis_password)" > cache_url
|
||||
chmod 0444 pg_password database_url redis_password cache_url
|
||||
mkdir -p postgres redis api migrate
|
||||
for name in pg_password:postgres redis_password:redis; do
|
||||
file=$${name%%:*}; dir=$${name##*:}
|
||||
[ ! -s "$$file" ] || mv "$$file" "$$dir/$$file"
|
||||
[ -s "$$dir/$$file" ] || head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n' > "$$dir/$$file"
|
||||
done
|
||||
rm -f database_url cache_url
|
||||
printf 'postgresql://messenger:%s@postgres:5432/messenger?connection_limit=10&pool_timeout=5&connect_timeout=5&socket_timeout=10&application_name=api&options=-c%%20statement_timeout%%3D5000' "$$(cat postgres/pg_password)" > api/database_url
|
||||
printf 'redis://:%s@redis:6379/0' "$$(cat redis/redis_password)" > api/cache_url
|
||||
printf 'postgresql://messenger:%s@postgres:5432/messenger' "$$(cat postgres/pg_password)" > migrate/database_url
|
||||
chmod 0444 postgres/* redis/* api/* migrate/*
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "0.1", memory: 32M }
|
||||
|
||||
postgres:
|
||||
image: postgres:17.2-alpine
|
||||
@@ -24,7 +33,10 @@ services:
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/pg_password
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
- secrets:/run/secrets:ro
|
||||
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: postgres } }
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "1.0", memory: 512M }
|
||||
healthcheck:
|
||||
test: ["CMD", "pg_isready", "-U", "messenger", "-d", "messenger"]
|
||||
interval: 5s
|
||||
@@ -43,7 +55,10 @@ services:
|
||||
- -c
|
||||
- exec redis-server --save '' --appendonly no --maxmemory 64mb --maxmemory-policy allkeys-lru --requirepass "$$(cat /run/secrets/redis_password)"
|
||||
volumes:
|
||||
- secrets:/run/secrets:ro
|
||||
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: redis } }
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "0.25", memory: 128M }
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "redis-cli --no-auth-warning -a \"$$(cat /run/secrets/redis_password)\" ping | grep -q PONG"]
|
||||
interval: 5s
|
||||
@@ -62,7 +77,10 @@ services:
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
volumes:
|
||||
- secrets:/run/secrets:ro
|
||||
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: migrate } }
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "0.5", memory: 512M }
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
@@ -82,9 +100,11 @@ services:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
CACHE_URL_FILE: /run/secrets/cache_url
|
||||
volumes:
|
||||
- secrets:/run/secrets:ro
|
||||
- { type: volume, source: secrets, target: /run/secrets, read_only: true, volume: { subpath: api } }
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
resources:
|
||||
limits: { cpus: "0.5", memory: 256M }
|
||||
stop_grace_period: 10s
|
||||
depends_on:
|
||||
migrate:
|
||||
@@ -99,6 +119,9 @@ services:
|
||||
- "127.0.0.1:${APP_PORT:-8080}:8080"
|
||||
volumes:
|
||||
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "0.25", memory: 64M }
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
@@ -117,6 +140,9 @@ services:
|
||||
volumes:
|
||||
- .:/work
|
||||
- /var/run/docker.sock:/var/run/docker.sock
|
||||
deploy:
|
||||
resources:
|
||||
limits: { cpus: "1.0", memory: 256M }
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
|
||||
Reference in New Issue
Block a user