From ca89d54513920fa407d7341f99f2ea2d57fbd7cf Mon Sep 17 00:00:00 2001 From: grillazz Date: Tue, 2 Sep 2025 13:14:50 +0200 Subject: [PATCH] add simple hs pattern --- .env | 4 +-- compose-ha.yml | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ nginx/Dockerfile | 3 ++ nginx/nginx.conf | 12 +++++++ 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 compose-ha.yml create mode 100644 nginx/Dockerfile create mode 100644 nginx/nginx.conf diff --git a/.env b/.env index a447112..13f0864 100644 --- a/.env +++ b/.env @@ -2,14 +2,14 @@ PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 # Postgres -POSTGRES_HOST=localhost +POSTGRES_HOST=postgres POSTGRES_PORT=5432 POSTGRES_DB=devdb POSTGRES_USER=devdb POSTGRES_PASSWORD=secret # Redis -REDIS_HOST=localhost +REDIS_HOST=redis REDIS_PORT=6379 REDIS_DB=2 diff --git a/compose-ha.yml b/compose-ha.yml new file mode 100644 index 0000000..bb323d2 --- /dev/null +++ b/compose-ha.yml @@ -0,0 +1,94 @@ +services: + api1: + container_name: panettone_api1 + build: . + environment: + - PYTHONPATH=/panettone + env_file: + - .env + - .secrets + command: bash -c " + uvicorn app.main:app + --host 0.0.0.0 --port 8080 + --lifespan=on --use-colors --loop uvloop --http httptools + " + volumes: + - ./app:/panettone/app + - ./tests:/panettone/tests + - ./templates:/panettone/templates + - ./alembic:/panettone/alembic + ports: + - "8081:8080" + depends_on: + - postgres + - redis + + api2: + container_name: panettone_api2 + build: . + environment: + - PYTHONPATH=/panettone + env_file: + - .env + - .secrets + command: bash -c " + uvicorn app.main:app + --host 0.0.0.0 --port 8080 + --lifespan=on --use-colors --loop uvloop --http httptools + " + volumes: + - ./app:/panettone/app + - ./tests:/panettone/tests + - ./templates:/panettone/templates + - ./alembic:/panettone/alembic + ports: + - "8082:8080" + depends_on: + - postgres + - redis + + + postgres: + container_name: panettone_postgres + build: + context: ./db + dockerfile: Dockerfile + volumes: + - panettone_postgres_data:/var/lib/postgresql/data + env_file: + - .env + ports: + - 5432:5432 + environment: + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set} + - POSTGRES_USER=${POSTGRES_USER?Variable not set} + - POSTGRES_DB=${POSTGRES_DB?Variable not set} + healthcheck: + test: + [ + "CMD-SHELL", "pg_isready -d $POSTGRES_DB -U $POSTGRES_USER" + ] + interval: 5s + timeout: 5s + retries: 5 + + redis: + image: redis:latest + container_name: panettone_redis + ports: + - "6379:6379" + env_file: + - .env + entrypoint: redis-server --appendonly yes + + loadbalancer: + container_name: panettone_loadbalancer + build: ./nginx + ports: + - "8080:80" + depends_on: + - api1 + - api2 + +volumes: + panettone_postgres_data: {} \ No newline at end of file diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 0000000..e22e136 --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,3 @@ +FROM nginx +RUN rm /etc/nginx/conf.d/default.conf +COPY nginx.conf /etc/nginx/conf.d/default.conf \ No newline at end of file diff --git a/nginx/nginx.conf b/nginx/nginx.conf new file mode 100644 index 0000000..0357378 --- /dev/null +++ b/nginx/nginx.conf @@ -0,0 +1,12 @@ +upstream loadbalancer { + server api1:8080 weight=1; + server api2:8080 weight=1; +} + +server { + listen 80; + + location / { + proxy_pass http://loadbalancer; + } +}