Усиление контейнеров и проверка отказоустойчивости

This commit is contained in:
EgorAnti
2026-11-26 17:26:19 +03:00
parent d23847f953
commit 60fa12ce6e
24 changed files with 418 additions and 68 deletions
+121 -23
View File
@@ -31,8 +31,8 @@ x-api-environment: &api-environment
REDIS_HOST: redis
REDIS_PORT: 6379
REDIS_SSL_ENABLED: "true"
REDIS_CONNECT_TIMEOUT: ${REDIS_CONNECT_TIMEOUT:-1s}
REDIS_COMMAND_TIMEOUT: ${REDIS_COMMAND_TIMEOUT:-500ms}
REDIS_CONNECT_TIMEOUT: ${REDIS_CONNECT_TIMEOUT:-5s}
REDIS_COMMAND_TIMEOUT: ${REDIS_COMMAND_TIMEOUT:-2s}
REDIS_POOL_MAX_ACTIVE: ${REDIS_POOL_MAX_ACTIVE:-8}
REDIS_POOL_MAX_IDLE: ${REDIS_POOL_MAX_IDLE:-8}
REDIS_POOL_MIN_IDLE: ${REDIS_POOL_MIN_IDLE:-0}
@@ -82,22 +82,34 @@ x-api-environment: &api-environment
ATTACHMENT_RECONCILE_INTERVAL_MILLIS: ${ATTACHMENT_RECONCILE_INTERVAL_MILLIS:-600000}
ATTACHMENT_RECONCILE_INITIAL_DELAY_MILLIS: ${ATTACHMENT_RECONCILE_INITIAL_DELAY_MILLIS:-60000}
x-runtime-hardening: &runtime-hardening
security_opt: ["no-new-privileges:true"]
pids_limit: 256
services:
openbao:
<<: *runtime-hardening
image: ghcr.io/openbao/openbao:2.7.3
command: ["server", "-config=/openbao/config/openbao.hcl"]
user: "0:0"
command: ["server"]
environment:
BAO_ADDR: https://openbao:8200
BAO_CACERT: /run/pki/service-ca/ca.crt
ports:
- "127.0.0.1:${OPENBAO_PORT:-8200}:8200"
volumes:
- openbao-data:/openbao/data
- ./infra/openbao:/openbao/config:ro
- openbao-data:/openbao/file
- ./infra/openbao/openbao.hcl:/openbao/config/openbao.hcl:ro
- ./infra/openbao/api-policy.hcl:/openbao/policies/api-policy.hcl:ro
- ./infra/openbao/worker-policy.hcl:/openbao/policies/worker-policy.hcl:ro
- ./secrets/pki:/run/pki:ro
cap_drop: [ALL]
security_opt: ["no-new-privileges:true"]
cap_add: [CHOWN, DAC_OVERRIDE, FOWNER, KILL, SETGID, SETUID]
read_only: true
tmpfs: ["/tmp:size=32m,mode=1777"]
restart: unless-stopped
cpus: 0.5
mem_limit: 512m
healthcheck:
test: ["CMD-SHELL", "bao status >/dev/null 2>&1; code=$$?; test $$code -eq 0 -o $$code -eq 2"]
interval: 5s
@@ -106,9 +118,13 @@ services:
networks: [backend]
openbao-agent-api:
<<: *runtime-hardening
image: ghcr.io/openbao/openbao:2.7.3
user: "0:0"
command: ["agent", "-config=/openbao/config/agent.hcl"]
environment:
BAO_SKIP_DROP_ROOT: "true"
SKIP_CHOWN: "true"
depends_on:
openbao:
condition: service_started
@@ -118,14 +134,21 @@ services:
- ./secrets/openbao/api:/run/bootstrap:ro
- api-runtime-secrets:/run/rendered
cap_drop: [ALL]
security_opt: ["no-new-privileges:true"]
read_only: true
tmpfs: ["/tmp:size=32m,mode=1777"]
restart: unless-stopped
cpus: 0.25
mem_limit: 128m
networks: [backend]
openbao-agent-worker:
<<: *runtime-hardening
image: ghcr.io/openbao/openbao:2.7.3
user: "0:0"
command: ["agent", "-config=/openbao/config/agent.hcl"]
environment:
BAO_SKIP_DROP_ROOT: "true"
SKIP_CHOWN: "true"
depends_on:
openbao:
condition: service_started
@@ -135,11 +158,15 @@ services:
- ./secrets/openbao/worker:/run/bootstrap:ro
- worker-runtime-secrets:/run/rendered
cap_drop: [ALL]
security_opt: ["no-new-privileges:true"]
read_only: true
tmpfs: ["/tmp:size=32m,mode=1777"]
restart: unless-stopped
cpus: 0.25
mem_limit: 128m
networks: [backend]
postgres:
<<: *runtime-hardening
image: postgres:16.4-bookworm
environment:
POSTGRES_DB: ${DATABASE_NAME:-messenger}
@@ -161,11 +188,13 @@ services:
timeout: 2s
retries: 30
restart: unless-stopped
cap_drop: [AUDIT_WRITE, MKNOD, NET_RAW, SETFCAP, SETPCAP, SYS_CHROOT]
cpus: 1.0
mem_limit: 768m
networks: [backend]
redis:
<<: *runtime-hardening
image: redis:7.4-bookworm
environment:
REDIS_API_PASSWORD: ${REDIS_API_PASSWORD:?Set REDIS_API_PASSWORD in .env}
@@ -186,11 +215,13 @@ services:
timeout: 2s
retries: 30
restart: unless-stopped
cap_drop: [AUDIT_WRITE, MKNOD, NET_RAW, SETFCAP, SETPCAP, SYS_CHROOT]
cpus: 0.25
mem_limit: 256m
networks: [backend]
rabbitmq:
<<: *runtime-hardening
image: rabbitmq:4.1.4-management-alpine
environment:
RABBITMQ_DEFAULT_USER: ${RABBITMQ_ADMIN_USER:-operator}
@@ -208,11 +239,13 @@ services:
start_period: 60s
retries: 12
restart: unless-stopped
cap_drop: [AUDIT_WRITE, MKNOD, NET_RAW, SETFCAP, SETPCAP, SYS_CHROOT]
cpus: 0.5
mem_limit: 1024m
networks: [backend]
rabbitmq-init:
<<: *runtime-hardening
image: curlimages/curl:8.12.1
depends_on:
rabbitmq:
@@ -232,18 +265,26 @@ services:
auth="$$ADMIN_USER:$$ADMIN_PASSWORD"
curl --fail --silent --show-error --cacert /run/ca.crt -u "$$auth" -H content-type:application/json -X PUT "$$base/users/messenger-api" -d "{\"password\":\"$$API_PASSWORD\",\"tags\":\"\"}"
curl --fail --silent --show-error --cacert /run/ca.crt -u "$$auth" -H content-type:application/json -X PUT "$$base/users/messenger-worker" -d "{\"password\":\"$$WORKER_PASSWORD\",\"tags\":\"\"}"
curl --fail --silent --show-error --cacert /run/ca.crt -u "$$auth" -H content-type:application/json -X PUT "$$base/permissions/%2F/messenger-api" -d '{"configure":"^attachments\\..*","write":"^attachments\\..*","read":"^$"}'
curl --fail --silent --show-error --cacert /run/ca.crt -u "$$auth" -H content-type:application/json -X PUT "$$base/permissions/%2F/messenger-api" -d '{"configure":"^attachments\\..*","write":"^attachments\\..*","read":"^attachments\\.(v1|dlx\\.v1)$"}'
curl --fail --silent --show-error --cacert /run/ca.crt -u "$$auth" -H content-type:application/json -X PUT "$$base/permissions/%2F/messenger-worker" -d '{"configure":"^attachments\\..*","write":"^attachments\\..*","read":"^attachments\\..*"}'
restart: "no"
cap_drop: [ALL]
read_only: true
tmpfs: ["/tmp:size=16m,mode=1777"]
cpus: 0.25
mem_limit: 128m
networks: [backend]
minio:
<<: *runtime-hardening
image: bitnamilegacy/minio:2025.4.22-debian-12-r1@sha256:d7cd0e172c4cc0870f4bdc3142018e2a37be9acf04d68f386600daad427e0cab
environment:
MINIO_ROOT_USER: ${MINIO_ROOT_USER:?Set MINIO_ROOT_USER in .env}
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?Set MINIO_ROOT_PASSWORD in .env}
MINIO_SCHEME: https
MINIO_CERTS_DIR: /certs
# The dedicated minio-init service owns bucket/user/policy bootstrap.
MINIO_SKIP_CLIENT: "yes"
volumes:
- minio-data:/bitnami/minio/data
- ./secrets/pki/minio/tls.crt:/certs/public.crt:ro
@@ -255,16 +296,19 @@ services:
timeout: 2s
retries: 30
restart: unless-stopped
cap_drop: [AUDIT_WRITE, MKNOD, NET_RAW, SETFCAP, SETPCAP, SYS_CHROOT]
cpus: 0.75
mem_limit: 512m
networks: [backend]
minio-init:
<<: *runtime-hardening
image: bitnamilegacy/minio-client:2025.4.16-debian-12-r0@sha256:28e38f52642c06a2344fa974cf55786b4c52601b6b9b0676c68d40ee0876a5bc
depends_on:
minio:
condition: service_healthy
environment:
MC_CONFIG_DIR: /tmp/mc
MINIO_ROOT_USER: ${MINIO_ROOT_USER:?Set MINIO_ROOT_USER in .env}
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?Set MINIO_ROOT_PASSWORD in .env}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
@@ -274,19 +318,21 @@ services:
S3_WORKER_SECRET_ACCESS_KEY: ${S3_WORKER_SECRET_ACCESS_KEY:?Set S3_WORKER_SECRET_ACCESS_KEY in .env}
volumes:
- ./infra/minio-api-policy.json:/policy/minio-api-policy.json:ro
- ./secrets/pki/service-ca/ca.crt:/root/.mc/certs/CAs/service-ca.crt:ro
- ./secrets/pki/service-ca/ca.crt:/run/ca.crt:ro
entrypoint: ["/bin/sh", "-c"]
command:
- |
set -eu
mkdir -p "$$MC_CONFIG_DIR/certs/CAs"
cp /run/ca.crt "$$MC_CONFIG_DIR/certs/CAs/service-ca.crt"
sed "s/__S3_BUCKET__/$$S3_BUCKET/g" /policy/minio-api-policy.json > /tmp/minio-api-policy.json
attempt=0
until mc alias set local https://minio:9000 "$$MINIO_ROOT_USER" "$$MINIO_ROOT_PASSWORD" \
until mc alias set -- local https://minio:9000 "$$MINIO_ROOT_USER" "$$MINIO_ROOT_PASSWORD" \
&& mc mb --ignore-existing "local/$$S3_BUCKET" \
&& mc anonymous set none "local/$$S3_BUCKET" \
&& (mc admin user add local "$$S3_ACCESS_KEY_ID" "$$S3_SECRET_ACCESS_KEY" \
&& (mc admin user add -- local "$$S3_ACCESS_KEY_ID" "$$S3_SECRET_ACCESS_KEY" \
|| mc admin user info local "$$S3_ACCESS_KEY_ID") \
&& (mc admin user add local "$$S3_WORKER_ACCESS_KEY_ID" "$$S3_WORKER_SECRET_ACCESS_KEY" \
&& (mc admin user add -- local "$$S3_WORKER_ACCESS_KEY_ID" "$$S3_WORKER_SECRET_ACCESS_KEY" \
|| mc admin user info local "$$S3_WORKER_ACCESS_KEY_ID") \
&& (mc admin policy create local messenger-attachments-rw /tmp/minio-api-policy.json \
|| mc admin policy info local messenger-attachments-rw) \
@@ -301,30 +347,39 @@ services:
sleep 2
done
restart: "no"
cap_drop: [ALL]
read_only: true
tmpfs: ["/tmp:size=32m,mode=1777"]
cpus: 0.25
mem_limit: 128m
networks: [backend]
private-bucket-check:
<<: *runtime-hardening
profiles: ["verification"]
image: bitnamilegacy/minio-client:2025.4.16-debian-12-r0@sha256:28e38f52642c06a2344fa974cf55786b4c52601b6b9b0676c68d40ee0876a5bc
depends_on:
minio-init:
condition: service_completed_successfully
environment:
MC_CONFIG_DIR: /tmp/mc
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:?Set S3_ACCESS_KEY_ID in .env}
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:?Set S3_SECRET_ACCESS_KEY in .env}
volumes:
- ./secrets/pki/service-ca/ca.crt:/root/.mc/certs/CAs/service-ca.crt:ro
- ./secrets/pki/service-ca/ca.crt:/run/ca.crt:ro
entrypoint: ["/bin/sh", "-c"]
command:
- |
set -eu
mc alias set anonymous https://minio:9000
mkdir -p "$$MC_CONFIG_DIR/certs/CAs"
cp /run/ca.crt "$$MC_CONFIG_DIR/certs/CAs/service-ca.crt"
mc alias set -- anonymous https://minio:9000
if mc ls "anonymous/$$S3_BUCKET" >/dev/null 2>&1; then
echo "FAIL: anonymous bucket listing succeeded" >&2
exit 1
fi
mc alias set restricted https://minio:9000 "$$S3_ACCESS_KEY_ID" "$$S3_SECRET_ACCESS_KEY"
mc alias set -- restricted https://minio:9000 "$$S3_ACCESS_KEY_ID" "$$S3_SECRET_ACCESS_KEY"
mc ls "restricted/$$S3_BUCKET" >/dev/null
if mc admin info restricted >/dev/null 2>&1; then
echo "FAIL: API credentials have administrative access" >&2
@@ -332,10 +387,16 @@ services:
fi
echo "PASS: bucket is private and API credentials are non-admin"
restart: "no"
cap_drop: [ALL]
read_only: true
tmpfs: ["/tmp:size=16m,mode=1777"]
cpus: 0.25
mem_limit: 128m
networks: [backend]
# One-shot Flyway runner. It is invoked explicitly before API replicas.
migrate:
<<: *runtime-hardening
image: hla-lab7-app:local
build:
context: .
@@ -360,11 +421,15 @@ services:
volumes:
- ./secrets/pki:/run/pki:ro
restart: "no"
cap_drop: [ALL]
read_only: true
tmpfs: ["/tmp:size=128m,mode=1777"]
cpus: 0.75
mem_limit: 768m
networks: [backend]
api:
<<: *runtime-hardening
image: hla-lab7-app:local
build:
context: .
@@ -411,11 +476,15 @@ services:
init: true
stop_grace_period: 12s
restart: unless-stopped
cap_drop: [ALL]
read_only: true
tmpfs: ["/tmp:size=128m,mode=1777"]
cpus: 0.75
mem_limit: 768m
networks: [backend]
worker:
<<: *runtime-hardening
image: hla-lab7-app:local
environment:
<<: *api-environment
@@ -423,7 +492,7 @@ services:
MESSENGER_ROLE: worker
OTEL_SERVICE_NAME: messenger-worker
SPRING_MAIN_WEB_APPLICATION_TYPE: none
JAVA_TOOL_OPTIONS: -XX:MaxRAMPercentage=70
JAVA_TOOL_OPTIONS: -XX:MaxRAMPercentage=70 -Djava.io.tmpdir=/tmp/native
DATABASE_USER: messenger_worker
CLIENT_TLS_CERTIFICATE: /run/pki/worker-client/tls.crt
CLIENT_TLS_PRIVATE_KEY: /run/pki/worker-client/tls.key
@@ -455,11 +524,18 @@ services:
init: true
stop_grace_period: 20s
restart: unless-stopped
cap_drop: [ALL]
read_only: true
tmpfs:
- /tmp:size=192m,mode=1777
# webp-imageio extracts its native codec here; keep executable temp scoped to the worker.
- /tmp/native:size=64m,mode=1777,exec
cpus: 1.0
mem_limit: 1024m
networks: [backend]
nginx:
<<: *runtime-hardening
image: nginx:1.27.4-bookworm
ports:
- "${PUBLIC_BIND_ADDRESS:-127.0.0.1}:${PUBLIC_HTTPS_PORT:-8443}:8443"
@@ -476,11 +552,20 @@ services:
retries: 15
stop_grace_period: 10s
restart: unless-stopped
user: "101:101"
cap_drop: [ALL]
read_only: true
tmpfs:
- /var/cache/nginx:size=32m,mode=1777
- /var/log/nginx:size=16m,mode=1777
- /var/run:size=4m,mode=1777
- /tmp:size=16m,mode=1777
cpus: 0.25
mem_limit: 128m
networks: [backend]
otel-collector:
<<: *runtime-hardening
image: otel/opentelemetry-collector-contrib:0.165.0
command: ["--config=/etc/otelcol-contrib/collector.yaml"]
volumes:
@@ -494,14 +579,16 @@ services:
tempo:
condition: service_started
restart: unless-stopped
cap_drop: [ALL]
read_only: true
tmpfs: ["/tmp:size=32m,mode=1777"]
cpus: 0.5
mem_limit: 512m
networks: [backend]
prometheus:
<<: *runtime-hardening
image: prom/prometheus:v3.14.0
ports:
- "127.0.0.1:${PROMETHEUS_PORT:-9090}:9090"
command:
- --config.file=/etc/prometheus/prometheus.yml
- --storage.tsdb.path=/prometheus
@@ -515,39 +602,47 @@ services:
- ./secrets/pki:/run/pki:ro
- prometheus-data:/prometheus
restart: unless-stopped
cap_drop: [ALL]
read_only: true
tmpfs: ["/tmp:size=32m,mode=1777"]
cpus: 0.5
mem_limit: 512m
networks: [backend]
loki:
<<: *runtime-hardening
image: grafana/loki:3.7.8
command: ["-config.file=/etc/loki/local-config.yaml"]
ports:
- "127.0.0.1:${LOKI_PORT:-3100}:3100"
volumes:
- ./infra/otel/loki.yaml:/etc/loki/local-config.yaml:ro
- ./secrets/pki:/run/pki:ro
- loki-data:/loki
restart: unless-stopped
cap_drop: [ALL]
read_only: true
tmpfs: ["/tmp:size=32m,mode=1777"]
cpus: 0.5
mem_limit: 512m
networks: [backend]
tempo:
<<: *runtime-hardening
image: grafana/tempo:3.1.1
command: ["-config.file=/etc/tempo/tempo.yaml"]
ports:
- "127.0.0.1:${TEMPO_PORT:-3200}:3200"
volumes:
- ./infra/otel/tempo.yaml:/etc/tempo/tempo.yaml:ro
- ./secrets/pki:/run/pki:ro
- tempo-data:/var/tempo
restart: unless-stopped
cap_drop: [ALL]
read_only: true
tmpfs: ["/tmp:size=32m,mode=1777"]
cpus: 0.5
mem_limit: 512m
networks: [backend]
grafana:
<<: *runtime-hardening
image: grafana/grafana:13.3.0
ports:
- "127.0.0.1:${GRAFANA_PORT:-3000}:3000"
@@ -576,6 +671,9 @@ services:
tempo:
condition: service_started
restart: unless-stopped
cap_drop: [ALL]
read_only: true
tmpfs: ["/tmp:size=64m,mode=1777"]
cpus: 0.5
mem_limit: 512m
networks: [backend]