forked from mtusi2026/mtusi2026lab6
380 lines
11 KiB
YAML
380 lines
11 KiB
YAML
x-telemetry-env: &telemetry-env
|
|
OTEL_EXPORTER_OTLP_ENDPOINT: http://otel-collector:4318
|
|
OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
|
|
OTEL_RESOURCE_ATTRIBUTES: deployment.environment.name=lab
|
|
APP_VERSION: ${APP_VERSION:-lab6}
|
|
|
|
services:
|
|
postgres:
|
|
image: postgres:16.4-alpine
|
|
environment:
|
|
POSTGRES_USER: ${POSTGRES_USER}
|
|
POSTGRES_DB: ${POSTGRES_DB}
|
|
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
|
secrets:
|
|
- postgres_password
|
|
volumes:
|
|
- postgres-data:/var/lib/postgresql/data
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
retries: 10
|
|
start_period: 5s
|
|
networks: [data]
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "1.0", memory: 512M }
|
|
|
|
redis:
|
|
image: redis:7.4-alpine
|
|
command: ["redis-server", "--appendonly", "yes"]
|
|
volumes:
|
|
- redis-data:/data
|
|
healthcheck:
|
|
test: ["CMD", "redis-cli", "ping"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
retries: 10
|
|
start_period: 5s
|
|
networks: [data]
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.25", memory: 128M }
|
|
|
|
migrate:
|
|
image: migrate/migrate:v4.17.1
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
secrets: [database_url]
|
|
volumes:
|
|
- ./migrations:/migrations:ro
|
|
entrypoint: ["/bin/sh", "-ec"]
|
|
command:
|
|
- exec migrate -path=/migrations -database="$$(cat /run/secrets/database_url)" up
|
|
restart: "no"
|
|
networks: [data]
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.25", memory: 128M }
|
|
|
|
api:
|
|
build:
|
|
context: .
|
|
dockerfile: Dockerfile
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
migrate:
|
|
condition: service_completed_successfully
|
|
redis:
|
|
condition: service_healthy
|
|
s3-init:
|
|
condition: service_completed_successfully
|
|
s3-proxy:
|
|
condition: service_started
|
|
environment:
|
|
<<: *telemetry-env
|
|
OTEL_SERVICE_NAME: messenger-api
|
|
PORT: ${PORT}
|
|
DATABASE_URL_FILE: /run/secrets/database_url
|
|
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS}
|
|
MAX_CONCURRENT_REQUESTS: ${MAX_CONCURRENT_REQUESTS}
|
|
DB_MAX_CONNS: ${DB_MAX_CONNS}
|
|
DB_MIN_CONNS: ${DB_MIN_CONNS}
|
|
DB_CONN_TIMEOUT_SECONDS: ${DB_CONN_TIMEOUT_SECONDS}
|
|
CACHE_URL: ${CACHE_URL}
|
|
CHAT_CACHE_TTL_SECONDS: 30
|
|
DRAIN_SECONDS: 4
|
|
GOMEMLIMIT: 200MiB
|
|
S3_ENDPOINT: http://s3-proxy:8333
|
|
S3_BUCKET: attachments
|
|
S3_REGION: us-east-1
|
|
S3_ACCESS_KEY_FILE: /run/secrets/s3_api_access_key
|
|
S3_SECRET_KEY_FILE: /run/secrets/s3_api_secret_key
|
|
MAX_UPLOADS: 1
|
|
IMAGE_MEMORY_MB: 384
|
|
secrets:
|
|
- database_url
|
|
- s3_api_access_key
|
|
- s3_api_secret_key
|
|
stop_grace_period: 20s
|
|
healthcheck:
|
|
test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8080/health/ready"]
|
|
interval: 3s
|
|
timeout: 2s
|
|
retries: 3
|
|
start_period: 5s
|
|
networks: [edge, data, s3access, telemetry]
|
|
deploy:
|
|
replicas: 2
|
|
resources:
|
|
limits: { cpus: "0.5", memory: 512M }
|
|
|
|
lb:
|
|
image: haproxy:3.0-alpine
|
|
depends_on:
|
|
api:
|
|
condition: service_healthy
|
|
volumes:
|
|
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
|
ports:
|
|
- "127.0.0.1:8080:8080"
|
|
networks: [edge]
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.25", memory: 128M }
|
|
|
|
seaweedfs:
|
|
image: chrislusf/seaweedfs:4.50
|
|
command: ["server", "-dir=/data", "-s3", "-s3.config=/run/secrets/s3_config"]
|
|
secrets: [s3_config]
|
|
volumes:
|
|
- s3-data:/data
|
|
healthcheck:
|
|
test: ["CMD", "wget", "-q", "--spider", "http://127.0.0.1:9333/cluster/status"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
retries: 12
|
|
start_period: 10s
|
|
networks: [s3backend]
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.5", memory: 512M }
|
|
|
|
s3-init:
|
|
image: amazon/aws-cli:2.39.1
|
|
depends_on:
|
|
seaweedfs:
|
|
condition: service_healthy
|
|
s3-proxy: { condition: service_started }
|
|
secrets: [s3_admin_access_key, s3_admin_secret_key]
|
|
entrypoint: ["/bin/sh", "-c"]
|
|
command:
|
|
- |
|
|
export AWS_ACCESS_KEY_ID="$$(cat /run/secrets/s3_admin_access_key)"
|
|
export AWS_SECRET_ACCESS_KEY="$$(cat /run/secrets/s3_admin_secret_key)"
|
|
export AWS_DEFAULT_REGION=us-east-1
|
|
EP=http://s3-proxy:8333
|
|
for i in $$(seq 1 60); do
|
|
aws --endpoint-url $$EP s3api head-bucket --bucket attachments 2>/dev/null \
|
|
|| aws --endpoint-url $$EP s3api create-bucket --bucket attachments 2>/dev/null \
|
|
|| { sleep 2; continue; }
|
|
if aws --endpoint-url $$EP s3api put-object --bucket attachments --key _health/init-probe --body /etc/hostname >/dev/null 2>&1; then
|
|
aws --endpoint-url $$EP s3api delete-object --bucket attachments --key _health/init-probe >/dev/null 2>&1 || true
|
|
exit 0
|
|
fi
|
|
sleep 2
|
|
done
|
|
exit 1
|
|
restart: "no"
|
|
networks: [s3access]
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.25", memory: 256M }
|
|
|
|
s3-proxy:
|
|
image: haproxy:3.0-alpine
|
|
volumes:
|
|
- ./infra/haproxy-s3.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
|
depends_on:
|
|
seaweedfs:
|
|
condition: service_healthy
|
|
networks: [s3backend, s3access]
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.25", memory: 64M }
|
|
reconciler:
|
|
build:
|
|
context: .
|
|
dockerfile: Dockerfile
|
|
command: ["./reconcile", "-loop=10m", "-grace=1h", "-check-missing"]
|
|
depends_on:
|
|
migrate:
|
|
condition: service_completed_successfully
|
|
s3-init:
|
|
condition: service_completed_successfully
|
|
environment:
|
|
DATABASE_URL_FILE: /run/secrets/database_url
|
|
S3_ENDPOINT: http://s3-proxy:8333
|
|
S3_BUCKET: attachments
|
|
S3_REGION: us-east-1
|
|
S3_ACCESS_KEY_FILE: /run/secrets/s3_api_access_key
|
|
S3_SECRET_KEY_FILE: /run/secrets/s3_api_secret_key
|
|
secrets: [database_url, s3_api_access_key, s3_api_secret_key]
|
|
stop_grace_period: 5s
|
|
networks: [data, s3access]
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.25", memory: 128M }
|
|
healthcheck:
|
|
disable: true
|
|
restart: unless-stopped
|
|
|
|
broker:
|
|
image: redis:7.4-alpine
|
|
command: ["redis-server", "--appendonly", "yes", "--appendfsync", "always", "--maxmemory-policy", "noeviction"]
|
|
volumes: [broker-data:/data]
|
|
networks: [data]
|
|
healthcheck:
|
|
test: ["CMD", "redis-cli", "ping"]
|
|
interval: 3s
|
|
timeout: 2s
|
|
retries: 10
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.25", memory: 128M }
|
|
|
|
worker: &pipeline
|
|
build:
|
|
context: .
|
|
dockerfile: Dockerfile
|
|
command: ["./worker", "-mode=worker"]
|
|
depends_on:
|
|
migrate: { condition: service_completed_successfully }
|
|
s3-init: { condition: service_completed_successfully }
|
|
broker: { condition: service_healthy }
|
|
environment: &pipeline-env
|
|
<<: *telemetry-env
|
|
OTEL_SERVICE_NAME: messenger-worker
|
|
DATABASE_URL_FILE: /run/secrets/database_url
|
|
BROKER_URL: redis://broker:6379/0
|
|
S3_ENDPOINT: http://s3-proxy:8333
|
|
S3_BUCKET: attachments
|
|
S3_REGION: us-east-1
|
|
S3_ACCESS_KEY_FILE: /run/secrets/s3_api_access_key
|
|
S3_SECRET_KEY_FILE: /run/secrets/s3_api_secret_key
|
|
WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-1}
|
|
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
|
|
JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-30}
|
|
STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400}
|
|
IMAGE_MEMORY_MB: 384
|
|
GOMEMLIMIT: 128MiB
|
|
secrets: [database_url, s3_api_access_key, s3_api_secret_key]
|
|
networks: [data, s3access, telemetry]
|
|
stop_grace_period: 40s
|
|
healthcheck: { disable: true }
|
|
restart: unless-stopped
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "1.0", memory: 1024M }
|
|
|
|
outbox:
|
|
<<: *pipeline
|
|
command: ["./worker", "-mode=relay"]
|
|
environment:
|
|
<<: *pipeline-env
|
|
OTEL_SERVICE_NAME: messenger-outbox
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.25", memory: 192M }
|
|
|
|
otel-collector:
|
|
image: otel/opentelemetry-collector-contrib:0.128.0
|
|
command: ["--config=/etc/otelcol/config.yaml"]
|
|
environment:
|
|
GOMEMLIMIT: 150MiB
|
|
volumes:
|
|
- ./infra/otel/collector.yaml:/etc/otelcol/config.yaml:ro
|
|
networks: [telemetry]
|
|
restart: unless-stopped
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.5", memory: 256M }
|
|
|
|
prometheus:
|
|
image: prom/prometheus:v3.5.0
|
|
command:
|
|
- --config.file=/etc/prometheus/prometheus.yaml
|
|
- --storage.tsdb.path=/prometheus
|
|
- --storage.tsdb.retention.time=7d
|
|
- --storage.tsdb.retention.size=512MB
|
|
volumes:
|
|
- ./infra/otel/prometheus.yaml:/etc/prometheus/prometheus.yaml:ro
|
|
- ./infra/otel/alerts.yaml:/etc/prometheus/alerts.yaml:ro
|
|
- prometheus-data:/prometheus
|
|
networks: [telemetry]
|
|
restart: unless-stopped
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.5", memory: 384M }
|
|
|
|
loki:
|
|
image: grafana/loki:3.5.5
|
|
command: ["-config.file=/etc/loki/lab.yaml"]
|
|
volumes:
|
|
- ./infra/otel/loki.yaml:/etc/loki/lab.yaml:ro
|
|
- loki-data:/loki
|
|
networks: [telemetry]
|
|
restart: unless-stopped
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.5", memory: 384M }
|
|
|
|
tempo:
|
|
image: grafana/tempo:2.8.2
|
|
command: ["-config.file=/etc/tempo/lab.yaml"]
|
|
volumes:
|
|
- ./infra/otel/tempo.yaml:/etc/tempo/lab.yaml:ro
|
|
- tempo-data:/var/tempo
|
|
networks: [telemetry]
|
|
restart: unless-stopped
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.5", memory: 384M }
|
|
|
|
grafana:
|
|
image: grafana/grafana:12.1.1
|
|
environment:
|
|
GF_SECURITY_ADMIN_PASSWORD__FILE: /run/secrets/grafana_admin_password
|
|
GF_USERS_ALLOW_SIGN_UP: "false"
|
|
GF_AUTH_ANONYMOUS_ENABLED: "true"
|
|
GF_AUTH_ANONYMOUS_ORG_ROLE: Viewer
|
|
GF_ANALYTICS_REPORTING_ENABLED: "false"
|
|
GF_ANALYTICS_CHECK_FOR_UPDATES: "false"
|
|
secrets: [grafana_admin_password]
|
|
volumes:
|
|
- grafana-data:/var/lib/grafana
|
|
- ./infra/otel/grafana/provisioning:/etc/grafana/provisioning:ro
|
|
- ./infra/otel/grafana/dashboards:/etc/grafana/dashboards:ro
|
|
ports:
|
|
- "127.0.0.1:${GRAFANA_PORT:-3000}:3000"
|
|
networks: [edge, telemetry]
|
|
restart: unless-stopped
|
|
deploy:
|
|
resources:
|
|
limits: { cpus: "0.5", memory: 384M }
|
|
|
|
networks:
|
|
edge:
|
|
data:
|
|
s3backend:
|
|
internal: true
|
|
s3access:
|
|
internal: true
|
|
telemetry:
|
|
internal: true
|
|
|
|
secrets:
|
|
database_url:
|
|
file: ./secrets/database_url.txt
|
|
postgres_password:
|
|
file: ./secrets/postgres_password.txt
|
|
s3_config: { file: ./secrets/s3_config.json }
|
|
s3_api_access_key: { file: ./secrets/s3_api_access_key.txt }
|
|
s3_api_secret_key: { file: ./secrets/s3_api_secret_key.txt }
|
|
s3_admin_access_key: { file: ./secrets/s3_admin_access_key.txt }
|
|
s3_admin_secret_key: { file: ./secrets/s3_admin_secret_key.txt }
|
|
grafana_admin_password: { file: ./secrets/grafana_admin_password.txt }
|
|
|
|
volumes:
|
|
postgres-data:
|
|
redis-data:
|
|
s3-data:
|
|
broker-data:
|
|
prometheus-data:
|
|
loki-data:
|
|
tempo-data:
|
|
grafana-data:
|