lab4: S3-вложения, presigned upload и проверки

This commit is contained in:
Vladislav Kunetskiy
2026-10-21 22:42:48 +03:00
parent d342b2a227
commit 49c2fe3071
79 changed files with 12266 additions and 910 deletions
+73 -7
View File
@@ -1,4 +1,4 @@
# Lab3 stand: two API replicas behind a HAProxy load balancer.
# Lab4 stand: two API replicas behind HAProxy, PostgreSQL, Redis and SeaweedFS S3.
#
# cp -n .env.example .env
# docker compose --env-file .env up -d --build
@@ -8,13 +8,13 @@
# reachable from the host via `docker compose exec` / `docker compose logs`.
# No container_name anywhere: replica hostnames stay the short container IDs,
# which the application reports back as X-Instance-Id.
name: mtusi-messenger-lab3
name: mtusi-messenger-lab4
# The image is built once by the api service. migrate and s3-init only consume
# it; duplicating `build` here makes Compose compile the same Go tree in parallel
# and OOMs Docker Desktop's default VM.
x-app: &app
build:
context: .
dockerfile: Dockerfile
image: mtusi-messenger:lab3
image: mtusi-messenger:lab4
services:
postgres:
@@ -67,6 +67,54 @@ services:
memory: 192M
restart: unless-stopped
# Private S3-compatible object store. Only the S3 API is published, on
# loopback, so the presigned-upload track and evidence can reach it from the
# host. master/volume/filer ports stay inside the compose network.
seaweedfs:
image: chrislusf/seaweedfs:3.80@sha256:1055999e08eed1789b0ae45d235126e4495e23d3fb9d6396293fd42539b1ae6a
command:
- server
- -s3
- -s3.config=/etc/seaweedfs/s3.json
- -dir=/data
- -master.volumeSizeLimitMB=64
volumes:
- ./infra/s3/s3.json:/etc/seaweedfs/s3.json:ro
- seaweedfs-data:/data
ports:
- "127.0.0.1:${S3_PORT:-8333}:8333"
healthcheck:
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:8333/healthz || exit 1"]
interval: 5s
timeout: 3s
retries: 20
start_period: 60s
deploy:
resources:
limits:
cpus: "1.00"
memory: 768M
restart: unless-stopped
# One-shot, idempotent bucket bootstrap and identity probe.
s3-init:
<<: *app
entrypoint: ["/usr/local/bin/s3init"]
environment:
S3_ENDPOINT: http://seaweedfs:8333
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_ADMIN_ACCESS_KEY: ${S3_ADMIN_ACCESS_KEY:-lab4admin}
S3_ADMIN_SECRET_ACCESS_KEY: ${S3_ADMIN_SECRET_ACCESS_KEY:-lab4admin-secret}
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:-lab4app}
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:-lab4app-secret}
depends_on:
seaweedfs:
condition: service_healthy
healthcheck:
disable: true
restart: "no"
# One-shot schema bootstrap; runs once to completion before any API replica.
migrate:
<<: *app
@@ -95,6 +143,9 @@ services:
api:
<<: *app
build:
context: .
dockerfile: Dockerfile
# Общий сетевой alias: LB резолвит одно имя в адреса всех реплик, поэтому
# масштабирование и замена контейнеров не зависят от их номеров/ID.
networks:
@@ -117,6 +168,18 @@ services:
REDIS_HOST: redis
REDIS_PORT: "6379"
CACHE_ENABLED: "true"
S3_ENDPOINT: http://seaweedfs:8333
S3_PUBLIC_ENDPOINT: ${S3_PUBLIC_ENDPOINT:-http://127.0.0.1:8333}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:-lab4app}
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:-lab4app-secret}
S3_USE_PATH_STYLE: "true"
S3_PRESIGN_TTL: ${S3_PRESIGN_TTL:-5m}
S3_SWEEP_INTERVAL: ${S3_SWEEP_INTERVAL:-1m}
S3_SWEEP_GRACE: ${S3_SWEEP_GRACE:-10m}
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
# Размер пула соединений ОДНОЙ реплики; общий расход = реплики × значение.
# Это один из общих ресурсов, который масштабирование расходует первым.
POSTGRES_MAX_CONNS: "${POSTGRES_MAX_CONNS:-10}"
@@ -125,7 +188,7 @@ services:
resources:
limits:
cpus: "${API_CPU_LIMIT:-0.50}"
memory: ${API_MEM_LIMIT:-256M}
memory: ${API_MEM_LIMIT:-384M}
# The app drains on SIGTERM within SERVER_SHUTDOWN_TIMEOUT; keep the grace
# period above it so Docker does not SIGKILL an in-flight request.
stop_grace_period: 15s
@@ -136,6 +199,8 @@ services:
condition: service_healthy
migrate:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/health/ready"]
interval: 10s
@@ -171,3 +236,4 @@ services:
volumes:
postgres18-data:
redis7-data:
seaweedfs-data: