158 lines
4.0 KiB
YAML
158 lines
4.0 KiB
YAML
# Compose-стенд для балансировки и отказоустойчивости API.
|
|
services:
|
|
# Nginx балансирует запросы между несколькими API-репликами.
|
|
nginx:
|
|
image: nginx:1.27.2-alpine
|
|
ports:
|
|
- "8080:8080"
|
|
volumes:
|
|
- ./infra/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
|
|
depends_on:
|
|
api-1:
|
|
condition: service_healthy
|
|
api-2:
|
|
condition: service_healthy
|
|
restart: unless-stopped
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: '0.5'
|
|
memory: 128M
|
|
reservations:
|
|
cpus: '0.1'
|
|
memory: 64M
|
|
|
|
# Первая реплика API.
|
|
api-1:
|
|
build: .
|
|
environment:
|
|
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/messenger
|
|
SPRING_DATASOURCE_USERNAME: messenger
|
|
SPRING_DATASOURCE_PASSWORD: messenger
|
|
SPRING_DATA_REDIS_HOST: redis
|
|
SESSION_TTL_SECONDS: 3600
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
redis:
|
|
condition: service_healthy
|
|
migration:
|
|
condition: service_completed_successfully
|
|
healthcheck:
|
|
test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/health/ready"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
retries: 3
|
|
start_period: 30s
|
|
restart: unless-stopped
|
|
stop_grace_period: 30s
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: '1.0'
|
|
memory: 512M
|
|
reservations:
|
|
cpus: '0.5'
|
|
memory: 256M
|
|
|
|
# Вторая реплика API.
|
|
api-2:
|
|
build: .
|
|
environment:
|
|
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/messenger
|
|
SPRING_DATASOURCE_USERNAME: messenger
|
|
SPRING_DATASOURCE_PASSWORD: messenger
|
|
SPRING_DATA_REDIS_HOST: redis
|
|
SESSION_TTL_SECONDS: 3600
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
redis:
|
|
condition: service_healthy
|
|
migration:
|
|
condition: service_completed_successfully
|
|
healthcheck:
|
|
test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/health/ready"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
retries: 3
|
|
start_period: 30s
|
|
restart: unless-stopped
|
|
stop_grace_period: 30s
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: '1.0'
|
|
memory: 512M
|
|
reservations:
|
|
cpus: '0.5'
|
|
memory: 256M
|
|
|
|
# Отдельный контейнер для однократного выполнения миграций.
|
|
migration:
|
|
build: .
|
|
environment:
|
|
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/messenger
|
|
SPRING_DATASOURCE_USERNAME: messenger
|
|
SPRING_DATASOURCE_PASSWORD: messenger
|
|
SPRING_DATA_REDIS_HOST: redis
|
|
SPRING_MAIN_WEB_APPLICATION_TYPE: none
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
redis:
|
|
condition: service_healthy
|
|
restart: "no"
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: '0.5'
|
|
memory: 256M
|
|
reservations:
|
|
cpus: '0.1'
|
|
memory: 128M
|
|
|
|
# PostgreSQL хранит постоянные данные.
|
|
postgres:
|
|
image: postgres:16.4
|
|
environment:
|
|
POSTGRES_DB: messenger
|
|
POSTGRES_USER: messenger
|
|
POSTGRES_PASSWORD: messenger
|
|
volumes:
|
|
- pgdata:/var/lib/postgresql/data
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
retries: 10
|
|
restart: unless-stopped
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: '2.0'
|
|
memory: 1G
|
|
|
|
# Redis хранит Bearer-сессии.
|
|
redis:
|
|
image: redis:7.4.1
|
|
command: redis-server --appendonly yes
|
|
volumes:
|
|
- redisdata:/data
|
|
healthcheck:
|
|
test: ["CMD", "redis-cli", "ping"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
retries: 10
|
|
restart: unless-stopped
|
|
deploy:
|
|
resources:
|
|
limits:
|
|
cpus: '0.5'
|
|
memory: 256M
|
|
|
|
# Именованные тома для постоянного хранения данных.
|
|
volumes:
|
|
pgdata:
|
|
redisdata:
|