forked from mtusi2026/mtusi2026lab4
278 lines
6.9 KiB
YAML
278 lines
6.9 KiB
YAML
# Учебный стенд лабораторной 4. Секреты создаёт scripts/bootstrap, не этот файл.
|
|
# Две API-реплики: docker compose --env-file .env up -d --build --scale api=2
|
|
name: mtusi-lab4
|
|
|
|
x-api-environment: &api-environment
|
|
APP_HOST: 0.0.0.0
|
|
APP_PORT: "8080"
|
|
APP_ENV: lab
|
|
APP_ROLE: api
|
|
RUN_MIGRATIONS: "0"
|
|
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
|
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
|
SESSION_STORE: redis
|
|
DATABASE_URL_FILE: /run/secrets/database_url
|
|
CACHE_URL_FILE: /run/secrets/cache_url
|
|
S3_ENDPOINT: ${S3_ENDPOINT:-http://minio:9000}
|
|
S3_REGION: ${S3_REGION:-us-east-1}
|
|
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
|
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
|
|
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
|
|
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
|
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
|
ATTACHMENT_GC_GRACE_SECONDS: ${ATTACHMENT_GC_GRACE_SECONDS:-900}
|
|
AWS_REQUEST_CHECKSUM_CALCULATION: when_required
|
|
AWS_RESPONSE_CHECKSUM_VALIDATION: when_required
|
|
DB_POOL_SIZE: ${DB_POOL_SIZE:-10}
|
|
DB_MAX_OVERFLOW: ${DB_MAX_OVERFLOW:-10}
|
|
DB_POOL_TIMEOUT_SECONDS: "5"
|
|
DB_CONNECT_TIMEOUT_SECONDS: "2"
|
|
DB_COMMAND_TIMEOUT_SECONDS: "5"
|
|
REDIS_SOCKET_TIMEOUT_SECONDS: "1"
|
|
REDIS_MAX_CONNECTIONS: "50"
|
|
|
|
services:
|
|
postgres:
|
|
image: postgres:16.10-bookworm@sha256:38471f330eb885e04de130b768d6db4e10469e2311879c7e5c699f6d2d8a1c74
|
|
environment:
|
|
POSTGRES_USER: messenger
|
|
POSTGRES_DB: messenger
|
|
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
|
secrets:
|
|
- postgres_password
|
|
volumes:
|
|
- postgres_data:/var/lib/postgresql/data
|
|
ports:
|
|
- "127.0.0.1:5433:5432"
|
|
networks:
|
|
- lab
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
|
|
interval: 2s
|
|
timeout: 3s
|
|
retries: 30
|
|
start_period: 5s
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: "1.00"
|
|
memory: 512M
|
|
restart: unless-stopped
|
|
|
|
redis:
|
|
image: redis:7.4.6-alpine@sha256:3b73847e72874be07e6657b129a94761662b79bc0f679273757d4218573b2a98
|
|
command:
|
|
- redis-server
|
|
- --appendonly
|
|
- yes
|
|
- --appendfsync
|
|
- always
|
|
- --dir
|
|
- /data
|
|
volumes:
|
|
- redis_data:/data
|
|
ports:
|
|
- "127.0.0.1:6379:6379"
|
|
networks:
|
|
- lab
|
|
healthcheck:
|
|
test: ["CMD", "redis-cli", "ping"]
|
|
interval: 2s
|
|
timeout: 3s
|
|
retries: 30
|
|
start_period: 3s
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: "0.50"
|
|
memory: 128M
|
|
restart: unless-stopped
|
|
|
|
minio:
|
|
image: minio/minio:RELEASE.2024-11-07T00-52-20Z
|
|
command: ["server", "/data", "--address", ":9000", "--console-address", ":9001"]
|
|
environment:
|
|
MINIO_ROOT_USER_FILE: /run/secrets/minio_root_user
|
|
MINIO_ROOT_PASSWORD_FILE: /run/secrets/minio_root_password
|
|
MINIO_BROWSER: "off"
|
|
secrets:
|
|
- minio_root_user
|
|
- minio_root_password
|
|
volumes:
|
|
- minio_data:/data
|
|
ports:
|
|
- "127.0.0.1:9000:9000"
|
|
networks:
|
|
- lab
|
|
healthcheck:
|
|
test: ["CMD", "curl", "-f", "http://127.0.0.1:9000/minio/health/live"]
|
|
interval: 3s
|
|
timeout: 5s
|
|
retries: 20
|
|
start_period: 10s
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: "0.50"
|
|
memory: 256M
|
|
restart: unless-stopped
|
|
|
|
minio-init:
|
|
image: minio/mc:RELEASE.2024-11-05T11-29-37Z
|
|
environment:
|
|
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
|
secrets:
|
|
- minio_root_user
|
|
- minio_root_password
|
|
- s3_access_key_id
|
|
- s3_secret_access_key
|
|
volumes:
|
|
- ./infra/minio-bootstrap.sh:/bootstrap.sh:ro
|
|
- ./infra/minio-policy.json:/policy.json:ro
|
|
networks:
|
|
- lab
|
|
depends_on:
|
|
minio:
|
|
condition: service_started
|
|
entrypoint: ["/bin/sh", "/bootstrap.sh"]
|
|
restart: "no"
|
|
|
|
migrate:
|
|
build: .
|
|
environment:
|
|
APP_ROLE: migrate
|
|
RUN_MIGRATIONS: "1"
|
|
APP_ENV: lab
|
|
DATABASE_URL_FILE: /run/secrets/database_url
|
|
secrets:
|
|
- database_url
|
|
networks:
|
|
- lab
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
command: ["python", "-m", "app.migrate"]
|
|
restart: "no"
|
|
deploy:
|
|
replicas: 1
|
|
resources:
|
|
limits:
|
|
cpus: "0.25"
|
|
memory: 128M
|
|
|
|
api:
|
|
build: .
|
|
environment: *api-environment
|
|
secrets:
|
|
- database_url
|
|
- cache_url
|
|
- s3_access_key_id
|
|
- s3_secret_access_key
|
|
networks:
|
|
- lab
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
redis:
|
|
condition: service_healthy
|
|
migrate:
|
|
condition: service_completed_successfully
|
|
minio-init:
|
|
condition: service_completed_successfully
|
|
healthcheck:
|
|
test:
|
|
[
|
|
"CMD",
|
|
"python",
|
|
"-c",
|
|
"import os,urllib.request; p=os.environ.get('APP_PORT','8080'); urllib.request.urlopen('http://127.0.0.1:%s/health/ready'%p, timeout=2).read()",
|
|
]
|
|
interval: 2s
|
|
timeout: 2s
|
|
retries: 16
|
|
start_period: 20s
|
|
stop_grace_period: 8s
|
|
stop_signal: SIGTERM
|
|
deploy:
|
|
replicas: ${API_REPLICAS:-2}
|
|
resources:
|
|
limits:
|
|
cpus: "0.50"
|
|
memory: 512M
|
|
restart: unless-stopped
|
|
|
|
gc:
|
|
build: .
|
|
environment:
|
|
<<: *api-environment
|
|
APP_ROLE: gc
|
|
secrets:
|
|
- database_url
|
|
- s3_access_key_id
|
|
- s3_secret_access_key
|
|
networks:
|
|
- lab
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
migrate:
|
|
condition: service_completed_successfully
|
|
minio-init:
|
|
condition: service_completed_successfully
|
|
command: ["python", "-m", "app.gc"]
|
|
restart: unless-stopped
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: "0.25"
|
|
memory: 128M
|
|
|
|
lb:
|
|
image: haproxy:3.2-bookworm@sha256:baecd003ae5c2376e58d04a5b2ffc7d82d30e9bebe2869f76222b819f719dee3
|
|
volumes:
|
|
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
|
ports:
|
|
- "127.0.0.1:8080:8080"
|
|
- "[::1]:8080:8080"
|
|
networks:
|
|
- lab
|
|
depends_on:
|
|
api:
|
|
condition: service_healthy
|
|
healthcheck:
|
|
test: ["CMD", "haproxy", "-c", "-f", "/usr/local/etc/haproxy/haproxy.cfg"]
|
|
interval: 5s
|
|
timeout: 2s
|
|
retries: 5
|
|
start_period: 3s
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: "0.25"
|
|
memory: 64M
|
|
restart: unless-stopped
|
|
|
|
secrets:
|
|
postgres_password:
|
|
file: ./secrets/postgres_password
|
|
database_url:
|
|
file: ./secrets/database_url
|
|
cache_url:
|
|
file: ./secrets/cache_url
|
|
minio_root_user:
|
|
file: ./secrets/minio_root_user
|
|
minio_root_password:
|
|
file: ./secrets/minio_root_password
|
|
s3_access_key_id:
|
|
file: ./secrets/s3_access_key_id
|
|
s3_secret_access_key:
|
|
file: ./secrets/s3_secret_access_key
|
|
|
|
volumes:
|
|
postgres_data:
|
|
redis_data:
|
|
minio_data:
|
|
|
|
networks:
|
|
lab:
|