mirror of
https://github.com/taylanbakircioglu/haproxy-openmanager.git
synced 2026-09-16 15:45:11 +00:00
23257b02cf
A user running the API on a 2-core/4GB host reported slow-feeling API responses (Issue #35 follow-up). Review of the hot paths found no pathological defect; the dominant factors are the single uvicorn worker (one core serves all requests) and the constant agent-poll baseline (4 requests per agent every 30s). Two zero-risk improvements: - backend/Dockerfile: CMD now honors UVICORN_WORKERS, falling back to WEB_CONCURRENCY and then 1. Flagless uvicorn natively honors WEB_CONCURRENCY, so the fallback keeps any deployment that relied on it byte-for-byte compatible; with the final default of 1 worker uvicorn runs in-process exactly as before. >1 enables the multiprocess supervisor so multi-core hosts can use all cores. Background tasks are already multi-replica safe (FOR UPDATE SKIP LOCKED / advisory locks), as exercised by the k8s HPA deployment (2-10 replicas). `exec` keeps uvicorn as PID 1 (clean SIGTERM, verified ~1s docker stop with 2 workers). docker-compose.yml passes UVICORN_WORKERS through as empty-when-unset so a user-set WEB_CONCURRENCY is never overridden; .env.template documents it. - routers/agent.py heartbeat (by-name endpoint): the agent's status/version/upgrade_status were read with three separate single-column SELECTs against the same row; now one SELECT. Identical values and None semantics (single consistent snapshot instead of three reads); saves two round-trips per heartbeat per agent every 30s. The legacy by-id heartbeat endpoint is untouched; the heartbeat API contract is unchanged for agents of every version. - README: new "Performance Tuning" section (worker/replica scaling, and how to use the X-Response-Time header plus "Slow request detected" logs to pinpoint slow endpoints). Verification: full backend suite in docker green (1063 passed, 151 skipped; also re-run by the runtime image build); worker-count expansion matrix (unset->1, UVICORN_WORKERS=2->2, WEB_CONCURRENCY=3->3, both->UVICORN_WORKERS, empty->fallback) all correct; default run confirmed single-process with uvicorn as PID 1 and healthy API; UVICORN_WORKERS=2 confirmed parent + 2 workers, healthy API, clean shutdown; live heartbeats verified for register + existing-agent paths AND degraded agents (no stats socket / haproxy stopped / garbage stats CSV / unknown backend in server_statuses): all return 200, agent row updates correctly, zero backend errors. No schema, API, or agent changes.
168 lines
4.8 KiB
YAML
168 lines
4.8 KiB
YAML
services:
|
|
# PostgreSQL Database
|
|
postgres:
|
|
image: postgres:15-alpine
|
|
container_name: haproxy-openmanager-db
|
|
environment:
|
|
POSTGRES_DB: haproxy_openmanager
|
|
POSTGRES_USER: haproxy_user
|
|
POSTGRES_PASSWORD: haproxy_pass
|
|
PGDATA: /var/lib/postgresql/data/pgdata
|
|
volumes:
|
|
- postgres_data:/var/lib/postgresql/data
|
|
ports:
|
|
- "5432:5432"
|
|
networks:
|
|
- haproxy-network
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "pg_isready -U haproxy_user -d haproxy_openmanager"]
|
|
interval: 10s
|
|
timeout: 5s
|
|
retries: 5
|
|
|
|
# Redis Cache
|
|
redis:
|
|
image: redis:7-alpine
|
|
container_name: haproxy-openmanager-redis
|
|
command: redis-server --maxmemory 2gb --maxmemory-policy volatile-lru --save ""
|
|
ports:
|
|
- "6379:6379"
|
|
networks:
|
|
- haproxy-network
|
|
healthcheck:
|
|
test: ["CMD", "redis-cli", "ping"]
|
|
interval: 10s
|
|
timeout: 3s
|
|
retries: 5
|
|
|
|
# Backend API - pulls from Docker Hub by default
|
|
# To build locally instead, uncomment the build section and run: docker compose build backend
|
|
backend:
|
|
image: taylanbakircioglu/haproxy-openmanager-backend:latest
|
|
# build:
|
|
# context: ./backend
|
|
# dockerfile: Dockerfile
|
|
container_name: haproxy-openmanager-backend
|
|
environment:
|
|
- DATABASE_URL=postgresql://haproxy_user:haproxy_pass@postgres:5432/haproxy_openmanager
|
|
- REDIS_URL=redis://redis:6379
|
|
- SECRET_KEY=your-secret-key-change-this-in-production
|
|
- DEBUG=False
|
|
- LOG_LEVEL=INFO
|
|
- PUBLIC_URL=http://localhost:8080
|
|
- MANAGEMENT_BASE_URL=http://localhost:8080
|
|
# Empty when unset on the host: the image CMD then falls back to
|
|
# WEB_CONCURRENCY (uvicorn's native env) and finally to 1.
|
|
- UVICORN_WORKERS=${UVICORN_WORKERS:-}
|
|
volumes:
|
|
- haproxy_configs:/etc/haproxy
|
|
expose:
|
|
- "8000"
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
redis:
|
|
condition: service_healthy
|
|
networks:
|
|
- haproxy-network
|
|
healthcheck:
|
|
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/api/health')"]
|
|
interval: 30s
|
|
timeout: 10s
|
|
retries: 3
|
|
start_period: 40s
|
|
|
|
# Frontend React App - pulls from Docker Hub by default
|
|
# To build locally instead, uncomment the build section and run: docker compose build frontend
|
|
#
|
|
# NOTE: REACT_APP_* env vars are BUILD-time only for Create-React-App. The
|
|
# runtime container (serve -s build) does NOT consume them. The frontend
|
|
# uses same-origin (window.location) for /api/* and is routed by the nginx
|
|
# service below to the backend container. No env vars are required here.
|
|
frontend:
|
|
image: taylanbakircioglu/haproxy-openmanager-frontend:latest
|
|
# build:
|
|
# context: ./frontend
|
|
# dockerfile: Dockerfile
|
|
container_name: haproxy-openmanager-frontend
|
|
expose:
|
|
- "3000"
|
|
depends_on:
|
|
- backend
|
|
networks:
|
|
- haproxy-network
|
|
healthcheck:
|
|
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000"]
|
|
interval: 30s
|
|
timeout: 10s
|
|
retries: 3
|
|
|
|
# Nginx Reverse Proxy
|
|
nginx:
|
|
# Pinned to a patched release for the nginx "poolslip" advisory
|
|
# (mainline <=1.31.0 affected; fixed in mainline 1.31.1+ / stable 1.30.2+).
|
|
image: nginx:1.31.1-alpine
|
|
container_name: haproxy-openmanager-nginx
|
|
volumes:
|
|
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
|
|
ports:
|
|
- "8080:8080"
|
|
depends_on:
|
|
- frontend
|
|
- backend
|
|
networks:
|
|
- haproxy-network
|
|
healthcheck:
|
|
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8080/health"]
|
|
interval: 30s
|
|
timeout: 10s
|
|
retries: 3
|
|
|
|
# HAProxy Instance (for testing)
|
|
haproxy:
|
|
image: haproxy:2.8-alpine
|
|
container_name: haproxy-instance
|
|
volumes:
|
|
- ./haproxy/haproxy-simple.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
|
ports:
|
|
- "80:80"
|
|
- "8404:8404"
|
|
networks:
|
|
- haproxy-network
|
|
|
|
# Remote HAProxy Instance 1 (Production)
|
|
haproxy-remote1:
|
|
image: haproxy:2.8-alpine
|
|
container_name: haproxy-remote1
|
|
volumes:
|
|
- ./haproxy/haproxy-simple.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
|
ports:
|
|
- "8001:80"
|
|
- "8405:8404"
|
|
networks:
|
|
- haproxy-network
|
|
environment:
|
|
- HAPROXY_ENV=production
|
|
|
|
# Remote HAProxy Instance 2 (Staging)
|
|
haproxy-remote2:
|
|
image: haproxy:2.8-alpine
|
|
container_name: haproxy-remote2
|
|
volumes:
|
|
- ./haproxy/haproxy-simple.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
|
ports:
|
|
- "8002:80"
|
|
- "8406:8404"
|
|
networks:
|
|
- haproxy-network
|
|
environment:
|
|
- HAPROXY_ENV=staging
|
|
|
|
volumes:
|
|
postgres_data:
|
|
haproxy_configs:
|
|
|
|
networks:
|
|
haproxy-network:
|
|
driver: bridge
|