This commit is contained in:
Getlles
2026-11-23 21:13:23 +03:00
parent 019af0eafe
commit 8c7b051c2a
32 changed files with 1189 additions and 321 deletions
+262 -103
View File
@@ -1,20 +1,59 @@
services:
postgres:
image: postgres:15.5-alpine3.19
environment:
POSTGRES_DB: messenger
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}
openbao:
image: quay.io/openbao/openbao:2.0.1
entrypoint: ["bao"]
command: ["server", "-config=/openbao/config/openbao.hcl"]
cap_add: ["IPC_LOCK"]
volumes:
- ./infra/openbao/openbao.hcl:/openbao/config/openbao.hcl:ro
- ./infra/openbao/policy-api.hcl:/openbao/policies/policy-api.hcl:ro
- ./infra/openbao/policy-worker.hcl:/openbao/policies/policy-worker.hcl:ro
- ./infra/certs/openbao.crt:/openbao/certs/openbao.crt:ro
- ./infra/certs/openbao.key:/openbao/certs/openbao.key:ro
- ./infra/certs/ca.crt:/openbao/certs/ca.crt:ro
- baodata:/openbao/file
deploy:
resources:
limits:
cpus: '0.50'
memory: 512M
healthcheck:
test: ["CMD-SHELL", "bao status -tls-skip-verify >/dev/null 2>&1 || test $$? -eq 2"]
interval: 5s
timeout: 3s
retries: 10
postgres:
image: postgres:15.5-alpine3.19
user: "70:70"
cap_drop: ["ALL"]
security_opt: ["no-new-privileges:true"]
environment:
POSTGRES_DB: messenger
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}
entrypoint: ["/bin/sh", "-c"]
command:
- |
set -e
cp /etc/pg/certs-src/postgres.crt /tmp/postgres.crt
cp /etc/pg/certs-src/postgres.key /tmp/postgres.key
cp /etc/pg/certs-src/ca.crt /tmp/ca.crt
chmod 600 /tmp/postgres.key
chmod 644 /tmp/postgres.crt /tmp/ca.crt
exec docker-entrypoint.sh postgres \
-c ssl=on \
-c ssl_cert_file=/tmp/postgres.crt \
-c ssl_key_file=/tmp/postgres.key \
-c ssl_ca_file=/tmp/ca.crt
volumes:
- pgdata:/var/lib/postgresql/data
- ./infra/certs:/etc/pg/certs-src:ro
deploy:
resources:
limits:
cpus: '0.50'
memory: 512M
reservations:
cpus: '0.10'
memory: 128M
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d messenger"]
interval: 3s
@@ -23,16 +62,32 @@ services:
redis:
image: redis:7.2.4-alpine
command: redis-server --appendonly yes --appendfsync everysec --loglevel warning
user: "999:1000"
cap_add: ["CHOWN", "SETGID", "SETUID"]
command: >
redis-server
--appendonly yes
--appendfsync everysec
--tls-port 6379
--port 0
--tls-cert-file /etc/redis/certs/redis.crt
--tls-key-file /etc/redis/certs/redis.key
--tls-ca-cert-file /etc/redis/certs/ca.crt
--tls-auth-clients no
cap_drop: ["ALL"]
security_opt: ["no-new-privileges:true"]
volumes:
- redisdata:/data
- ./infra/certs/redis.crt:/etc/redis/certs/redis.crt:ro
- ./infra/certs/redis.key:/etc/redis/certs/redis.key:ro
- ./infra/certs/ca.crt:/etc/redis/certs/ca.crt:ro
deploy:
resources:
limits:
cpus: '0.25'
memory: 256M
healthcheck:
test: ["CMD", "redis-cli", "ping"]
test: ["CMD", "redis-cli", "--tls", "--cacert", "/etc/redis/certs/ca.crt", "-p", "6379", "ping"]
interval: 3s
timeout: 3s
retries: 5
@@ -41,8 +96,11 @@ services:
image: postgres:15.5-alpine3.19
environment:
PGPASSWORD: ${POSTGRES_PASSWORD}
PGSSLMODE: verify-full
PGSSLROOTCERT: /certs/ca.crt
volumes:
- ./migrations:/migrations
- ./migrations:/migrations:ro
- ./infra/certs/ca.crt:/certs/ca.crt:ro
command: >
sh -c "until pg_isready -h postgres -U postgres; do sleep 1; done;
psql -h postgres -U postgres -d messenger -f /migrations/init.sql"
@@ -53,19 +111,29 @@ services:
s3:
image: chrislusf/seaweedfs:3.67
command: ["server", "-s3", "-dir=/data", "-s3.port=8333"]
command:
- "server"
- "-s3"
- "-dir=/data"
- "-s3.port=8333"
- "-s3.cert.file=/etc/s3/certs/s3.crt"
- "-s3.key.file=/etc/s3/certs/s3.key"
environment:
- AWS_ACCESS_KEY_ID=appuser
- AWS_SECRET_ACCESS_KEY=appsecretpassword
- AWS_ACCESS_KEY_ID=${S3_ACCESS_KEY_ID:-appuser}
- AWS_SECRET_ACCESS_KEY=${S3_SECRET_ACCESS_KEY:-appsecretpassword}
cap_drop: ["ALL"]
security_opt: ["no-new-privileges:true"]
volumes:
- s3data:/data
- ./infra/certs/s3.crt:/etc/s3/certs/s3.crt:ro
- ./infra/certs/s3.key:/etc/s3/certs/s3.key:ro
deploy:
resources:
limits:
cpus: '0.50'
memory: 512M
healthcheck:
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:8333/ || test $$? -eq 8 || exit 1"]
test: ["CMD-SHELL", "wget --no-check-certificate -q -O /dev/null https://127.0.0.1:8333/ || test $$? -eq 8 || exit 1"]
interval: 5s
timeout: 3s
retries: 5
@@ -74,13 +142,15 @@ services:
s3-init:
image: python:3.11.7-slim-bookworm
volumes:
- ./scripts:/scripts
- ./requirements.txt:/requirements.txt
- ./scripts:/scripts:ro
- ./requirements.txt:/requirements.txt:ro
- ./infra/certs/ca.crt:/certs/ca.crt:ro
environment:
- S3_ENDPOINT=http://s3:8333
- S3_ENDPOINT=https://s3:8333
- S3_BUCKET=messenger-attachments
- S3_ROOT_ACCESS_KEY=appuser
- S3_ROOT_SECRET_KEY=appsecretpassword
- S3_ROOT_ACCESS_KEY=${S3_ACCESS_KEY_ID:-appuser}
- S3_ROOT_SECRET_KEY=${S3_SECRET_ACCESS_KEY:-appsecretpassword}
- AWS_CA_BUNDLE=/certs/ca.crt
command: >
sh -c "pip install --no-cache-dir -r /requirements.txt &&
python /scripts/bootstrap_s3.py"
@@ -96,45 +166,61 @@ services:
- "--storage.tsdb.path=/prometheus"
- "--storage.tsdb.retention.time=7d"
- "--web.enable-remote-write-receiver"
cap_drop: ["ALL"]
security_opt: ["no-new-privileges:true"]
volumes:
- ./infra/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
- ./infra/prometheus/alerts.yml:/etc/prometheus/alerts.yml:ro
- promdata:/prometheus
healthcheck:
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:9090/-/healthy || exit 1"]
interval: 5s
timeout: 3s
retries: 5
- ./infra/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
- ./infra/prometheus/alerts.yml:/etc/prometheus/alerts.yml:ro
- promdata:/prometheus
deploy:
resources:
limits:
cpus: '0.25'
memory: 256M
loki:
image: grafana/loki:3.0.0
command: ["-config.file=/etc/loki/local-config.yaml"]
cap_drop: ["ALL"]
security_opt: ["no-new-privileges:true"]
volumes:
- ./infra/loki/loki-config.yaml:/etc/loki/local-config.yaml:ro
- lokidata:/loki
healthcheck:
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:3100/ready || exit 1"]
interval: 5s
timeout: 3s
retries: 5
deploy:
resources:
limits:
cpus: '0.25'
memory: 256M
tempo:
image: grafana/tempo:2.4.1
command: ["-config.file=/etc/tempo.yaml"]
cap_drop: ["ALL"]
security_opt: ["no-new-privileges:true"]
volumes:
- ./infra/tempo/tempo-config.yaml:/etc/tempo.yaml:ro
- tempodata:/var/tempo
healthcheck:
test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:3200/ready || exit 1"]
interval: 5s
timeout: 3s
retries: 5
deploy:
resources:
limits:
cpus: '0.25'
memory: 256M
otel-collector:
image: otel/opentelemetry-collector-contrib:0.97.0
command: ["--config=/etc/otelcol-contrib/collector.yaml"]
cap_drop: ["ALL"]
security_opt: ["no-new-privileges:true"]
volumes:
- ./infra/otel/collector.yaml:/etc/otelcol-contrib/collector.yaml:ro
- ./infra/certs/otel-collector.crt:/etc/otelcol-contrib/certs/otel-collector.crt:ro
- ./infra/certs/otel-collector.key:/etc/otelcol-contrib/certs/otel-collector.key:ro
- ./infra/certs/ca.crt:/etc/otelcol-contrib/certs/ca.crt:ro
deploy:
resources:
limits:
cpus: '0.25'
memory: 320M
depends_on:
- prometheus
- loki
@@ -143,46 +229,82 @@ services:
grafana:
image: grafana/grafana:10.4.2
ports:
- "3000:3000"
- "127.0.0.1:3000:3000"
environment:
- GF_SECURITY_ADMIN_USER=${GF_ADMIN_USER:-admin}
- GF_SECURITY_ADMIN_PASSWORD=${GF_ADMIN_PASSWORD:-admin}
- GF_AUTH_ANONYMOUS_ENABLED=false
cap_drop: ["ALL"]
security_opt: ["no-new-privileges:true"]
volumes:
- ./infra/grafana/provisioning:/etc/grafana/provisioning:ro
- ./infra/grafana/dashboards:/var/lib/grafana/dashboards:ro
- ./infra/grafana/dashboard:/var/lib/grafana/dashboards:ro
- grafanadata:/var/lib/grafana
deploy:
resources:
limits:
cpus: '0.25'
memory: 256M
depends_on:
prometheus:
condition: service_healthy
loki:
condition: service_healthy
tempo:
condition: service_healthy
- prometheus
- loki
- tempo
api-1:
build: .
stop_grace_period: 15s
user: "10001:10001"
cap_drop: ["ALL"]
security_opt: ["no-new-privileges:true"]
command:
- "uvicorn"
- "app:app"
- "--host"
- "0.0.0.0"
- "--port"
- "8443"
- "--ssl-certfile"
- "/certs/api-1.crt"
- "--ssl-keyfile"
- "/certs/api-1.key"
- "--ssl-ca-certs"
- "/certs/ca.crt"
- "--ssl-cert-reqs"
- "2"
environment:
- DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}@postgres:5432/messenger
- REDIS_URL=redis://redis:6379/0
- DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@postgres:5432/messenger?sslmode=verify-full&sslrootcert=/certs/ca.crt
- REDIS_URL=rediss://redis:6379/0?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt
- SESSION_TTL_SECONDS=3600
- INSTANCE_ID=api-replica-1
- ALLOWED_LB_CLIENT_CN=edge-lb
- ENFORCE_MTLS_CLIENT=true
- SECRET_HMAC_KEY=${SECRET_HMAC_KEY}
- S3_ENDPOINT=http://s3:8333
- S3_ENDPOINT=https://s3:8333
- S3_BUCKET=messenger-attachments
- S3_ACCESS_KEY_ID=appuser
- S3_SECRET_ACCESS_KEY=appsecretpassword
- BROKER_STREAM=attachment_jobs
- AWS_CA_BUNDLE=/certs/ca.crt
- BAO_ADDR=https://openbao:8200
- BAO_CACERT=/certs/ca.crt
- ARGON2_MEMORY_KIB=19456
- ARGON2_ITERATIONS=2
- ARGON2_PARALLELISM=1
- OTEL_SERVICE_NAME=messenger-api
- OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
- OTEL_EXPORTER_OTLP_ENDPOINT=https://otel-collector:4318
- OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
- OTEL_EXPORTER_OTLP_CERTIFICATE=/certs/ca.crt
- OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=lab
deploy:
resources:
limits:
cpus: '0.50'
memory: 256M
env_file:
- ./infra/certs/api-approle.env
volumes:
- ./infra/certs/api-1.crt:/certs/api-1.crt:ro
- ./infra/certs/api-1.key:/certs/api-1.key:ro
- ./infra/certs/ca.crt:/certs/ca.crt:ro
- ./infra/certs/lb-ca.crt:/certs/lb-ca.crt:ro
- ./infra/certs/edge-lb.crt:/certs/edge-lb.crt:ro
- ./infra/certs/edge-lb.key:/certs/edge-lb.key:ro
healthcheck:
test: ["CMD", "python", "-c", "import ssl, urllib.request; ctx = ssl.create_default_context(cafile='/certs/ca.crt'); ctx.load_cert_chain('/certs/edge-lb.crt', '/certs/edge-lb.key'); urllib.request.urlopen('https://api-1:8443/health/ready', context=ctx, timeout=3)"]
interval: 5s
timeout: 3s
retries: 3
depends_on:
postgres:
condition: service_healthy
@@ -192,37 +314,69 @@ services:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
otel-collector:
condition: service_started
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8080/health/ready')"]
interval: 5s
timeout: 3s
retries: 3
openbao:
condition: service_healthy
api-2:
build: .
stop_grace_period: 15s
user: "10001:10001"
cap_drop: ["ALL"]
security_opt: ["no-new-privileges:true"]
command:
- "uvicorn"
- "app:app"
- "--host"
- "0.0.0.0"
- "--port"
- "8443"
- "--ssl-certfile"
- "/certs/api-2.crt"
- "--ssl-keyfile"
- "/certs/api-2.key"
- "--ssl-ca-certs"
- "/certs/ca.crt"
- "--ssl-cert-reqs"
- "2"
environment:
- DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}@postgres:5432/messenger
- REDIS_URL=redis://redis:6379/0
- DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@postgres:5432/messenger?sslmode=verify-full&sslrootcert=/certs/ca.crt
- REDIS_URL=rediss://redis:6379/0?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt
- SESSION_TTL_SECONDS=3600
- INSTANCE_ID=api-replica-2
- ALLOWED_LB_CLIENT_CN=edge-lb
- ENFORCE_MTLS_CLIENT=true
- SECRET_HMAC_KEY=${SECRET_HMAC_KEY}
- S3_ENDPOINT=http://s3:8333
- S3_ENDPOINT=https://s3:8333
- S3_BUCKET=messenger-attachments
- S3_ACCESS_KEY_ID=appuser
- S3_SECRET_ACCESS_KEY=appsecretpassword
- BROKER_STREAM=attachment_jobs
- AWS_CA_BUNDLE=/certs/ca.crt
- BAO_ADDR=https://openbao:8200
- BAO_CACERT=/certs/ca.crt
- ARGON2_MEMORY_KIB=19456
- ARGON2_ITERATIONS=2
- ARGON2_PARALLELISM=1
- OTEL_SERVICE_NAME=messenger-api
- OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
- OTEL_EXPORTER_OTLP_ENDPOINT=https://otel-collector:4318
- OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
- OTEL_EXPORTER_OTLP_CERTIFICATE=/certs/ca.crt
- OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=lab
env_file:
- ./infra/certs/api-approle.env
volumes:
- ./infra/certs/api-2.crt:/certs/api-2.crt:ro
- ./infra/certs/api-2.key:/certs/api-2.key:ro
- ./infra/certs/ca.crt:/certs/ca.crt:ro
- ./infra/certs/lb-ca.crt:/certs/lb-ca.crt:ro
- ./infra/certs/edge-lb.crt:/certs/edge-lb.crt:ro
- ./infra/certs/edge-lb.key:/certs/edge-lb.key:ro
deploy:
resources:
limits:
cpus: '0.50'
memory: 256M
healthcheck:
test: ["CMD", "python", "-c", "import ssl, urllib.request; ctx = ssl.create_default_context(cafile='/certs/ca.crt'); ctx.load_cert_chain('/certs/edge-lb.crt', '/certs/edge-lb.key'); urllib.request.urlopen('https://api-2:8443/health/ready', context=ctx, timeout=3)"]
interval: 5s
timeout: 3s
retries: 3
depends_on:
postgres:
condition: service_healthy
@@ -232,33 +386,41 @@ services:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
otel-collector:
condition: service_started
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8080/health/ready')"]
interval: 5s
timeout: 3s
retries: 3
openbao:
condition: service_healthy
worker:
build: .
user: "10001:10001"
cap_drop: ["ALL"]
security_opt: ["no-new-privileges:true"]
command: ["python", "worker.py"]
stop_grace_period: 15s
environment:
- DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}@postgres:5432/messenger
- REDIS_URL=redis://redis:6379/0
- S3_ENDPOINT=http://s3:8333
- DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@postgres:5432/messenger?sslmode=verify-full&sslrootcert=/certs/ca.crt
- REDIS_URL=rediss://redis:6379/0?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt
- S3_ENDPOINT=https://s3:8333
- S3_BUCKET=messenger-attachments
- S3_ACCESS_KEY_ID=appuser
- S3_SECRET_ACCESS_KEY=appsecretpassword
- AWS_CA_BUNDLE=/certs/ca.crt
- BAO_ADDR=https://openbao:8200
- BAO_CACERT=/certs/ca.crt
- BROKER_STREAM=attachment_jobs
- BROKER_GROUP=attachment_workers
- JOB_MAX_ATTEMPTS=5
- MAX_IMAGE_PIXELS=40000000
- OTEL_SERVICE_NAME=messenger-worker
- OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
- OTEL_EXPORTER_OTLP_ENDPOINT=https://otel-collector:4318
- OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
- OTEL_EXPORTER_OTLP_CERTIFICATE=/certs/ca.crt
- OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=lab
env_file:
- ./infra/certs/worker-approle.env
volumes:
- ./infra/certs/ca.crt:/certs/ca.crt:ro
healthcheck:
test: ["CMD-SHELL", "python -c \"import os, pathlib; assert any('worker.py' in pathlib.Path('/proc', p, 'cmdline').read_bytes().replace(b'\\x00', b' ').decode('utf-8', 'ignore') for p in os.listdir('/proc') if p.isdigit())\""]
interval: 5s
timeout: 3s
retries: 3
deploy:
resources:
limits:
@@ -273,20 +435,21 @@ services:
condition: service_completed_successfully
s3-init:
condition: service_completed_successfully
otel-collector:
condition: service_started
healthcheck:
test: ["CMD", "python", "-c", "import os, redis; redis.Redis.from_url(os.environ['REDIS_URL']).ping()"]
interval: 5s
timeout: 3s
retries: 3
openbao:
condition: service_healthy
nginx:
image: nginx:1.25.4-alpine
ports:
- "8080:8080"
- "8443:8443"
cap_drop: ["ALL"]
cap_add: ["CHOWN", "SETGID", "SETUID", "NET_BIND_SERVICE"]
security_opt: ["no-new-privileges:true"]
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./infra/certs/edge-lb.crt:/etc/nginx/certs/edge-lb.crt:ro
- ./infra/certs/edge-lb.key:/etc/nginx/certs/edge-lb.key:ro
- ./infra/certs/ca.crt:/etc/nginx/certs/ca.crt:ro
deploy:
resources:
limits:
@@ -295,11 +458,6 @@ services:
depends_on:
- api-1
- api-2
healthcheck:
test: ["CMD", "nginx", "-t"]
interval: 5s
timeout: 3s
retries: 3
volumes:
pgdata:
@@ -308,4 +466,5 @@ volumes:
promdata:
lokidata:
tempodata:
grafanadata:
grafanadata:
baodata: