forked from mtusi2026/mtusi2026lab3
feat(infra): add replicated API and readiness-aware load balancing
Provide resource profiles, persistent storage, parallel Nginx health checks and graceful restart hooks.
This commit is contained in:
+6
-8
@@ -1,13 +1,11 @@
|
|||||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
# Variables used by compose.yaml for this local lab stack.
|
||||||
APP_HOST=0.0.0.0
|
|
||||||
APP_PORT=8080
|
APP_PORT=8080
|
||||||
APP_ENV=lab
|
APP_ENV=lab
|
||||||
|
POSTGRES_DB=chat
|
||||||
|
POSTGRES_USER=app
|
||||||
|
POSTGRES_PASSWORD=change_for_local_lab
|
||||||
|
DB_POOL_MAX=15
|
||||||
|
CACHE_PREFIX=messenger:cache:v1
|
||||||
SESSION_TTL_SECONDS=3600
|
SESSION_TTL_SECONDS=3600
|
||||||
# Choose credentials locally. File paths contain no secret values.
|
|
||||||
DATABASE_URL_FILE=/run/secrets/database_url
|
|
||||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
|
||||||
SESSION_STORE=postgres
|
|
||||||
CACHE_URL_FILE=/run/secrets/cache_url
|
|
||||||
API_REPLICAS=2
|
|
||||||
IDEMPOTENCY_TTL_SECONDS=86400
|
IDEMPOTENCY_TTL_SECONDS=86400
|
||||||
|
|||||||
+58
@@ -0,0 +1,58 @@
|
|||||||
|
FROM node:24.20.0-bookworm-slim AS base
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
RUN apt-get update \
|
||||||
|
&& apt-get install -y --no-install-recommends openssl \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
RUN npm install --global pnpm@10.14.0
|
||||||
|
|
||||||
|
FROM base AS dependencies
|
||||||
|
|
||||||
|
COPY package.json pnpm-lock.yaml ./
|
||||||
|
RUN pnpm install --frozen-lockfile
|
||||||
|
|
||||||
|
FROM dependencies AS migrate
|
||||||
|
|
||||||
|
COPY prisma ./prisma
|
||||||
|
COPY prisma.config.ts ./
|
||||||
|
CMD ["pnpm", "db:deploy"]
|
||||||
|
|
||||||
|
FROM migrate AS build
|
||||||
|
|
||||||
|
COPY nest-cli.json tsconfig.json tsconfig.build.json ./
|
||||||
|
COPY src ./src
|
||||||
|
RUN pnpm build
|
||||||
|
|
||||||
|
FROM base AS production-dependencies
|
||||||
|
|
||||||
|
COPY package.json pnpm-lock.yaml ./
|
||||||
|
RUN pnpm install --prod --frozen-lockfile
|
||||||
|
|
||||||
|
FROM node:24.20.0-bookworm-slim AS runtime
|
||||||
|
|
||||||
|
RUN apt-get update \
|
||||||
|
&& apt-get install -y --no-install-recommends curl \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
ENV NODE_ENV=production \
|
||||||
|
APP_HOST=0.0.0.0 \
|
||||||
|
APP_PORT=8080
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
RUN groupadd --system --gid 10001 app \
|
||||||
|
&& useradd --system --uid 10001 --gid app --home-dir /app --shell /usr/sbin/nologin app
|
||||||
|
|
||||||
|
RUN mkdir -p /app/data && chown 10001:10001 /app/data
|
||||||
|
|
||||||
|
COPY --from=production-dependencies --chown=10001:10001 /app/node_modules ./node_modules
|
||||||
|
COPY --from=build --chown=10001:10001 /app/dist ./dist
|
||||||
|
COPY --from=build --chown=10001:10001 /app/package.json ./package.json
|
||||||
|
|
||||||
|
USER 10001:10001
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=15s --retries=3 \
|
||||||
|
CMD curl --fail --silent --show-error --max-time 2 --output /dev/null "http://127.0.0.1:${APP_PORT:-8080}/health/ready"
|
||||||
|
|
||||||
|
CMD ["node", "dist/main.js"]
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
# Teaching outline, not a runnable Dockerfile. Implement your own Dockerfile.
|
|
||||||
# 1. Choose a runtime/toolchain version and pin the base image tag/digest.
|
|
||||||
# 2. Copy dependency manifests first; install from the language lockfile.
|
|
||||||
# 3. Copy source and build. For compiled languages use a separate runtime stage.
|
|
||||||
# 4. Run the HTTP process as PID 1; listen on 0.0.0.0:8080.
|
|
||||||
# 5. Add non-root, signals and healthcheck according to the current rubric.
|
|
||||||
# 6. Pass runtime config/secrets at startup, never COPY .env or secret files.
|
|
||||||
# This file deliberately has no FROM/CMD: selecting/implementing them is the task.
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
# Topology worksheet, deliberately NOT a runnable stack (no application supplied).
|
|
||||||
# Implement compose.yaml or stack.yaml; do not submit this empty file as deployment.
|
|
||||||
# Service names below are logical roles, not imposed container names.
|
|
||||||
x-lab-number: 3
|
|
||||||
x-required-roles:
|
|
||||||
- api
|
|
||||||
- postgres
|
|
||||||
- load_balancer
|
|
||||||
# TODO: implement real images/builds, network wiring, volumes, healthchecks,
|
|
||||||
# credentials and bootstrap. Details: contracts/infrastructure.json and README.md.
|
|
||||||
services: {}
|
|
||||||
+113
@@ -0,0 +1,113 @@
|
|||||||
|
services:
|
||||||
|
api:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
target: runtime
|
||||||
|
restart: unless-stopped
|
||||||
|
init: true
|
||||||
|
cpus: '1.0'
|
||||||
|
mem_limit: 512m
|
||||||
|
deploy:
|
||||||
|
replicas: 2
|
||||||
|
command: ['sh', '-c', 'INSTANCE_ID=$$(hostname) exec node dist/main.js']
|
||||||
|
environment:
|
||||||
|
NODE_ENV: production
|
||||||
|
APP_ENV: ${APP_ENV:-lab}
|
||||||
|
APP_HOST: 0.0.0.0
|
||||||
|
APP_PORT: 8080
|
||||||
|
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||||
|
DB_POOL_MAX: ${DB_POOL_MAX:-15}
|
||||||
|
CACHE_URL: redis://redis:6379/0
|
||||||
|
CACHE_PREFIX: ${CACHE_PREFIX:-messenger:cache:v1}
|
||||||
|
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||||
|
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||||
|
depends_on:
|
||||||
|
migrate:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
healthcheck:
|
||||||
|
test:
|
||||||
|
- CMD
|
||||||
|
- curl
|
||||||
|
- --fail
|
||||||
|
- --silent
|
||||||
|
- --show-error
|
||||||
|
- --max-time
|
||||||
|
- '2'
|
||||||
|
- --output
|
||||||
|
- /dev/null
|
||||||
|
- http://127.0.0.1:8080/health/ready
|
||||||
|
interval: 10s
|
||||||
|
timeout: 3s
|
||||||
|
start_period: 20s
|
||||||
|
retries: 5
|
||||||
|
stop_grace_period: 30s
|
||||||
|
|
||||||
|
nginx:
|
||||||
|
image: nginx:1.30.6-alpine
|
||||||
|
restart: unless-stopped
|
||||||
|
cpus: '0.5'
|
||||||
|
mem_limit: 128m
|
||||||
|
ports:
|
||||||
|
- '127.0.0.1:${APP_PORT:-8080}:80'
|
||||||
|
volumes:
|
||||||
|
- ./infra/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||||
|
- ./infra/start-nginx.sh:/etc/nginx/start-nginx.sh:ro
|
||||||
|
entrypoint: ['/bin/sh', '/etc/nginx/start-nginx.sh']
|
||||||
|
depends_on:
|
||||||
|
api:
|
||||||
|
condition: service_healthy
|
||||||
|
|
||||||
|
migrate:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
target: migrate
|
||||||
|
cpus: '1.0'
|
||||||
|
mem_limit: 512m
|
||||||
|
environment:
|
||||||
|
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
|
||||||
|
postgres:
|
||||||
|
image: postgres:17.6-bookworm
|
||||||
|
restart: unless-stopped
|
||||||
|
cpus: '1.0'
|
||||||
|
mem_limit: 512m
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: ${POSTGRES_DB:-chat}
|
||||||
|
POSTGRES_USER: ${POSTGRES_USER:-app}
|
||||||
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-local_dev_password}
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql/data
|
||||||
|
healthcheck:
|
||||||
|
test: ['CMD-SHELL', 'pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"']
|
||||||
|
interval: 5s
|
||||||
|
timeout: 3s
|
||||||
|
start_period: 10s
|
||||||
|
retries: 10
|
||||||
|
stop_grace_period: 30s
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis:7.4.5-alpine
|
||||||
|
restart: unless-stopped
|
||||||
|
cpus: '0.5'
|
||||||
|
mem_limit: 256m
|
||||||
|
command:
|
||||||
|
['redis-server', '--appendonly', 'yes', '--appendfsync', 'everysec']
|
||||||
|
volumes:
|
||||||
|
- redis_data:/data
|
||||||
|
healthcheck:
|
||||||
|
test: ['CMD', 'redis-cli', 'ping']
|
||||||
|
interval: 5s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 10
|
||||||
|
stop_grace_period: 30s
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres_data:
|
||||||
|
redis_data:
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
services:
|
||||||
|
api:
|
||||||
|
environment:
|
||||||
|
DB_POOL_MAX: '8'
|
||||||
|
deploy:
|
||||||
|
replicas: 4
|
||||||
|
cpus: '1.0'
|
||||||
|
mem_limit: 512m
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
services:
|
||||||
|
api:
|
||||||
|
environment:
|
||||||
|
DB_POOL_MAX: '8'
|
||||||
|
deploy:
|
||||||
|
replicas: 4
|
||||||
|
cpus: '0.25'
|
||||||
|
mem_limit: 256m
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
services:
|
||||||
|
api:
|
||||||
|
environment:
|
||||||
|
DB_POOL_MAX: '8'
|
||||||
|
deploy:
|
||||||
|
replicas: 4
|
||||||
|
cpus: '0.5'
|
||||||
|
mem_limit: 256m
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
services:
|
||||||
|
api:
|
||||||
|
deploy:
|
||||||
|
replicas: 1
|
||||||
|
cpus: '1.0'
|
||||||
|
mem_limit: 1024m
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
services:
|
||||||
|
api:
|
||||||
|
deploy:
|
||||||
|
replicas: 1
|
||||||
|
cpus: '2.0'
|
||||||
|
mem_limit: 1024m
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
services:
|
||||||
|
api:
|
||||||
|
deploy:
|
||||||
|
replicas: 2
|
||||||
|
cpus: '0.5'
|
||||||
|
mem_limit: 512m
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
services:
|
||||||
|
api:
|
||||||
|
deploy:
|
||||||
|
replicas: 2
|
||||||
|
cpus: '1.0'
|
||||||
|
mem_limit: 512m
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
events {}
|
||||||
|
|
||||||
|
http {
|
||||||
|
upstream api {
|
||||||
|
least_conn;
|
||||||
|
zone api 64k;
|
||||||
|
include /etc/nginx/ready-upstreams.conf;
|
||||||
|
}
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
proxy_pass http://api;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
proxy_connect_timeout 2s;
|
||||||
|
proxy_read_timeout 30s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Executable
+71
@@ -0,0 +1,71 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
upstreams=/etc/nginx/ready-upstreams.conf
|
||||||
|
printf 'server 127.0.0.1:9 down;\n' > "$upstreams"
|
||||||
|
|
||||||
|
check_ready() {
|
||||||
|
state=/tmp/nginx-readiness.state
|
||||||
|
probes=/tmp/nginx-readiness-probes
|
||||||
|
mkdir -p "$probes"
|
||||||
|
: > "$state"
|
||||||
|
sleep 2
|
||||||
|
while :; do
|
||||||
|
next="${upstreams}.next"
|
||||||
|
previous="${upstreams}.previous"
|
||||||
|
: > "${state}.next"
|
||||||
|
printf 'server 127.0.0.1:9 down;\n' > "$next"
|
||||||
|
|
||||||
|
addresses=$(getent ahostsv4 api | awk '{print $1}' | sort -u)
|
||||||
|
for address in $addresses; do
|
||||||
|
(
|
||||||
|
if wget -q -T 1 -t 1 -O /dev/null "http://${address}:8080/health/ready"; then
|
||||||
|
printf '1\n'
|
||||||
|
else
|
||||||
|
printf '0\n'
|
||||||
|
fi > "$probes/$address"
|
||||||
|
) &
|
||||||
|
done
|
||||||
|
wait
|
||||||
|
|
||||||
|
for address in $addresses; do
|
||||||
|
record=$(awk -v address="$address" '$1 == address {print $2, $3, $4}' "$state")
|
||||||
|
set -- ${record:-0 0 0}
|
||||||
|
ready=$1
|
||||||
|
successes=$2
|
||||||
|
failures=$3
|
||||||
|
if [ "$(cat "$probes/$address")" = 1 ]; then
|
||||||
|
failures=0
|
||||||
|
if [ "$successes" -lt 2 ]; then successes=$((successes + 1)); fi
|
||||||
|
if [ "$successes" -ge 2 ]; then ready=1; fi
|
||||||
|
else
|
||||||
|
successes=0
|
||||||
|
if [ "$failures" -lt 2 ]; then failures=$((failures + 1)); fi
|
||||||
|
if [ "$failures" -ge 2 ]; then ready=0; fi
|
||||||
|
fi
|
||||||
|
printf '%s %s %s %s\n' "$address" "$ready" "$successes" "$failures" >> "${state}.next"
|
||||||
|
if [ "$ready" -eq 1 ]; then
|
||||||
|
printf 'server %s:8080 max_fails=2 fail_timeout=2s;\n' "$address" >> "$next"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
mv "${state}.next" "$state"
|
||||||
|
|
||||||
|
if cmp -s "$next" "$upstreams"; then
|
||||||
|
rm -f "$next"
|
||||||
|
else
|
||||||
|
cp "$upstreams" "$previous"
|
||||||
|
mv "$next" "$upstreams"
|
||||||
|
if nginx -t -q && nginx -s reload; then
|
||||||
|
rm -f "$previous"
|
||||||
|
else
|
||||||
|
mv "$previous" "$upstreams"
|
||||||
|
printf 'Could not reload Nginx readiness upstreams\n' >&2
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
sleep 2
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
check_ready &
|
||||||
|
exec nginx -g 'daemon off;'
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
if defined GIT_BASH goto run
|
||||||
|
set "GIT_BASH=%ProgramFiles%\Git\bin\bash.exe"
|
||||||
|
if exist "%GIT_BASH%" goto run
|
||||||
|
echo Git Bash was not found. Set GIT_BASH to the full path to bash.exe. 1>&2
|
||||||
|
exit /b 2
|
||||||
|
:run
|
||||||
|
"%GIT_BASH%" "%~dp0restart.sh"
|
||||||
|
exit /b %ERRORLEVEL%
|
||||||
Executable
+23
@@ -0,0 +1,23 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
project_dir=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||||
|
cd "$project_dir"
|
||||||
|
|
||||||
|
for service in api nginx postgres redis; do
|
||||||
|
if [ -z "$(docker compose ps --all --quiet "$service")" ]; then
|
||||||
|
printf '%s\n' "Missing container for $service; start the Compose stack first." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
replicas=$(docker compose ps --all --quiet api | wc -l | tr -d ' ')
|
||||||
|
if [ "$replicas" -lt 1 ]; then
|
||||||
|
printf 'No API replicas found.\n' >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
docker compose stop --timeout 30 nginx api postgres redis
|
||||||
|
docker compose up --detach --no-recreate --no-build --no-deps --wait --wait-timeout 45 postgres redis
|
||||||
|
docker compose up --detach --no-recreate --no-build --no-deps --scale "api=$replicas" --wait --wait-timeout 45 api
|
||||||
|
docker compose up --detach --no-recreate --no-build --no-deps nginx
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
cd /d "%~dp0.."
|
||||||
|
if not defined TARGET_INSTANCE_ID (
|
||||||
|
echo TARGET_INSTANCE_ID is required 1>&2
|
||||||
|
exit /b 2
|
||||||
|
)
|
||||||
|
for /f "delims=" %%I in ('docker compose ps --all --quiet api') do (
|
||||||
|
for /f "delims=" %%H in ('docker inspect --format "{{.Config.Hostname}}" %%I') do (
|
||||||
|
if "%%H"=="%TARGET_INSTANCE_ID%" (
|
||||||
|
docker stop --time 2 %%I >nul
|
||||||
|
if errorlevel 1 exit /b 1
|
||||||
|
exit /b 0
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
echo No API container matches X-Instance-Id 1>&2
|
||||||
|
exit /b 2
|
||||||
Executable
+24
@@ -0,0 +1,24 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
project_dir=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||||
|
cd "$project_dir"
|
||||||
|
|
||||||
|
target=${TARGET_INSTANCE_ID:?test provides target API instance ID}
|
||||||
|
|
||||||
|
containers=$(docker compose ps --all --quiet api)
|
||||||
|
if [ -z "$containers" ]; then
|
||||||
|
printf 'No API containers found\n' >&2
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
for entry in $(docker inspect --format '{{.Config.Hostname}}:{{.Id}}' $containers); do
|
||||||
|
instance=${entry%%:*}
|
||||||
|
container=${entry#*:}
|
||||||
|
if [ "$instance" = "$target" ]; then
|
||||||
|
docker stop --time 30 "$container" >/dev/null
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
printf 'No API container matches X-Instance-Id\n' >&2
|
||||||
|
exit 2
|
||||||
Reference in New Issue
Block a user