# 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 # curl http://localhost:8080/health/ready # # Only the load balancer is published (127.0.0.1). Stores and API replicas are # 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-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 image: mtusi-messenger:lab4 services: postgres: image: postgres:18-alpine@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873 environment: POSTGRES_USER: ${POSTGRES_USER:-messenger} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger} POSTGRES_DB: ${POSTGRES_DB:-messenger} volumes: - postgres18-data:/var/lib/postgresql healthcheck: test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] interval: 5s timeout: 5s retries: 10 start_period: 5s deploy: resources: limits: # Saturation point of the stand (see REPORT.md, track 1): the database # runs at its quota while API replicas idle. Raise this knob to give the # shared dependency more of the host CPU. cpus: "${POSTGRES_CPU_LIMIT:-0.50}" memory: ${POSTGRES_MEM_LIMIT:-384M} restart: unless-stopped redis: image: redis:7.4-alpine command: - redis-server - --appendonly - "yes" - --appendfsync - everysec - --save - "60" - "1" volumes: - redis7-data:/data healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 5s timeout: 3s retries: 10 start_period: 3s deploy: resources: limits: cpus: "0.25" 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 entrypoint: ["/usr/local/bin/migrate"] command: ["up"] environment: APP_CONFIG: /etc/messenger/dev/config.yaml POSTGRES_HOST: postgres POSTGRES_PORT: "5432" POSTGRES_DATABASE: ${POSTGRES_DB:-messenger} POSTGRES_USER: ${POSTGRES_USER:-messenger} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger} POSTGRES_SSL_MODE: disable depends_on: postgres: condition: service_healthy deploy: resources: limits: cpus: "0.50" memory: 256M # One-shot: healthcheck из образа (проверка /health/live) для него бессмыслен. healthcheck: disable: true restart: "no" api: <<: *app build: context: . dockerfile: Dockerfile # Общий сетевой alias: LB резолвит одно имя в адреса всех реплик, поэтому # масштабирование и замена контейнеров не зависят от их номеров/ID. networks: default: aliases: - api-replica env_file: - path: .env required: false # Explicit values win over localhost defaults coming from .env (see .env.example). environment: APP_CONFIG: /etc/messenger/dev/config.yaml POSTGRES_HOST: postgres POSTGRES_PORT: "5432" POSTGRES_DATABASE: ${POSTGRES_DB:-messenger} POSTGRES_USER: ${POSTGRES_USER:-messenger} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger} POSTGRES_SSL_MODE: disable REDIS_ENABLED: "true" 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}" deploy: replicas: ${API_REPLICAS:-2} resources: limits: cpus: "${API_CPU_LIMIT:-0.50}" 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 depends_on: postgres: condition: service_healthy redis: 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 # Готовность проверяет PostgreSQL (критичную зависимость; кэш деградирует # безопасно): под насыщающей нагрузкой ответ может подойти к секундам, и # слишком строгий таймаут помечает занятую, но работающую реплику как # unhealthy. timeout: 5s retries: 6 start_period: 5s restart: unless-stopped # Public entry point. Active /health/ready probes remove a not-ready replica from # rotation entirely; infra/lb/haproxy.cfg holds the upstream, checks and retries. lb: image: haproxy:3.2-alpine volumes: - ./infra/lb/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro ports: - "127.0.0.1:${SERVER_PORT:-8080}:80" # Operator-only stats page on loopback: shows which replica is UP/DOWN and why. - "127.0.0.1:${LB_STATS_PORT:-8404}:8404" depends_on: api: condition: service_healthy deploy: resources: limits: cpus: "${LB_CPU_LIMIT:-0.25}" memory: 64M restart: unless-stopped volumes: postgres18-data: redis7-data: seaweedfs-data: