mirror of
https://github.com/mudler/LocalAI.git
synced 2026-07-30 09:57:57 -04:00
fix(worker): give the worker a real health endpoint (#10987) The image bakes in a single HEALTHCHECK that curls http://localhost:8080/readyz, but the same image also runs `local-ai worker`, which serves HTTP on the gRPC base port minus one and never binds 8080. Every worker container was therefore permanently `unhealthy` (43 consecutive failures observed on a production node), which is worse than having no healthcheck: a genuinely broken worker and a perfectly good one both report `unhealthy`, so the signal carries no information and orchestration that keys on it misbehaves. The worker already served /readyz on that port via the file-transfer server, but as a constant 200 — it only proved the listener was bound, which is precisely the failure mode at issue. Readiness now tracks the live NATS connection: all of a worker's actual work (backend lifecycle events, inference dispatch, file staging) arrives over NATS, so a worker whose link is dead is up and useless. Registration is already implied, since the server only starts after registration succeeds. This reports something the controller cannot already see. The node registry's status/last_heartbeat is fed by an HTTP heartbeat to the frontend, a different network path from NATS — a worker can keep heartbeating while its NATS connection is dead and still look healthy in the registry. /healthz stays a constant 200: liveness must not follow readiness, or a NATS blip becomes a cluster-wide restart storm. The HEALTHCHECK is now a script that derives its endpoint from the mode the container is actually running plus the env vars that configure the bind address, so a frontend moved off 8080 with LOCALAI_ADDRESS (broken the same way) and a worker on a non-default base port are both probed correctly. Modes with no HTTP surface (agent-worker, one-shot commands) report healthy rather than false-unhealthy. HEALTHCHECK_ENDPOINT remains as an explicit override, so the workaround shipped in docker-compose.distributed.yaml keeps working; both overrides in that file are now unnecessary and have been removed. Also fixes the latent --start-period gap. Since #10949 a frontend's startup preload materializes HuggingFace artifacts before the HTTP server binds (31 GB observed on a live cluster), so a healthy replica can legitimately fail probes for a long time. --start-period is Docker's knob for exactly this: failures inside it leave the container `starting` instead of burning retries, and it ends early on the first success, so a generous 60m costs a fast-starting container nothing. --timeout drops from 10m to 10s — it is a per-probe deadline, and a localhost curl that has not answered in 10s is itself the fault being detected. Assisted-by: Claude Code:claude-opus-4-8[1m] [Read] [Edit] [Bash] Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
225 lines
8.0 KiB
YAML
225 lines
8.0 KiB
YAML
# Docker Compose for LocalAI Distributed Mode
|
|
#
|
|
# Starts a full distributed stack: PostgreSQL, NATS, a LocalAI frontend,
|
|
# and one llama-cpp backend node.
|
|
#
|
|
# Model files are transferred from the frontend to backend nodes via HTTP
|
|
# — no shared volumes needed between frontend and backends.
|
|
#
|
|
# Usage:
|
|
# docker compose -f docker-compose.distributed.yaml up
|
|
#
|
|
# See docs: https://localai.io/features/distributed-mode/
|
|
|
|
services:
|
|
# --- Infrastructure ---
|
|
|
|
postgres:
|
|
image: quay.io/mudler/localrecall:v0.5.5-postgresql # PostgreSQL with pgvector
|
|
environment:
|
|
POSTGRES_DB: localai
|
|
POSTGRES_USER: localai
|
|
POSTGRES_PASSWORD: localai
|
|
volumes:
|
|
- postgres_data:/var/lib/postgresql
|
|
healthcheck:
|
|
test: ["CMD-SHELL", "pg_isready -U localai"]
|
|
interval: 5s
|
|
timeout: 3s
|
|
retries: 10
|
|
|
|
nats:
|
|
image: nats:2-alpine
|
|
ports:
|
|
- "4222:4222" # Client connections
|
|
- "8222:8222" # HTTP monitoring (optional, useful for debugging)
|
|
command: ["--js", "-m", "8222"] # Enable JetStream + monitoring
|
|
|
|
# --- LocalAI Frontend ---
|
|
# Stateless API server that routes requests to backend nodes.
|
|
# Add more replicas behind a load balancer for HA.
|
|
|
|
localai:
|
|
# image: localai/localai:latest-cpu
|
|
build:
|
|
context: .
|
|
dockerfile: Dockerfile
|
|
args:
|
|
- IMAGE_TYPE=core
|
|
- BASE_IMAGE=ubuntu:24.04
|
|
ports:
|
|
- "8080:8080"
|
|
environment:
|
|
# Distributed mode
|
|
LOCALAI_DISTRIBUTED: "true"
|
|
LOCALAI_NATS_URL: "nats://nats:4222"
|
|
LOCALAI_AGENT_POOL_EMBEDDING_MODEL: "granite-embedding-107m-multilingual"
|
|
LOCALAI_AGENT_POOL_VECTOR_ENGINE: "postgres"
|
|
LOCALAI_AGENT_POOL_DATABASE_URL: "postgresql://localai:localai@postgres:5432/localai?sslmode=disable"
|
|
LOCALAI_REGISTRATION_TOKEN: "changeme" # Change this in production!
|
|
# Shared-models mode (optional): set when every node mounts the SAME
|
|
# models directory at the SAME path (see "Shared Volume Mode" below).
|
|
# The router then skips gRPC file staging and workers load models
|
|
# directly from the shared volume instead of re-downloading them.
|
|
# LOCALAI_DISTRIBUTED_SHARED_MODELS: "true"
|
|
# Auth (required for distributed mode — must use PostgreSQL)
|
|
LOCALAI_AUTH: "true"
|
|
LOCALAI_AUTH_DATABASE_URL: "postgresql://localai:localai@postgres:5432/localai?sslmode=disable"
|
|
# Force pure-Go DNS resolver. The default cgo resolver follows the
|
|
# container's nsswitch.conf and ends up forwarding to host
|
|
# systemd-resolved (127.0.0.53), which isn't reachable from inside
|
|
# the container — failing every postgres/nats hostname lookup at
|
|
# boot. The pure-Go path reads /etc/resolv.conf directly and uses
|
|
# Docker's embedded DNS at 127.0.0.11.
|
|
GODEBUG: "netdns=go"
|
|
# Paths
|
|
MODELS_PATH: /models
|
|
volumes:
|
|
- frontend_models:/models
|
|
- frontend_data:/data
|
|
depends_on:
|
|
postgres:
|
|
condition: service_healthy
|
|
nats:
|
|
condition: service_started
|
|
|
|
# --- Worker Node ---
|
|
# A generic worker that self-registers with the frontend.
|
|
# The same LocalAI image is used — no separate image needed.
|
|
# The SmartRouter dynamically tells workers which backend to install via NATS.
|
|
#
|
|
# Model files are transferred from the frontend via HTTP file staging.
|
|
# The worker has its own independent models volume.
|
|
|
|
worker-1:
|
|
# image: localai/localai:latest-cpu
|
|
build:
|
|
context: .
|
|
dockerfile: Dockerfile
|
|
args:
|
|
- IMAGE_TYPE=core
|
|
- BASE_IMAGE=ubuntu:24.04
|
|
command:
|
|
- worker
|
|
# No HEALTHCHECK_ENDPOINT override is needed: the image's healthcheck
|
|
# detects worker mode and derives the port from LOCALAI_SERVE_ADDR below
|
|
# (gRPC base port - 1 = 50050). The worker's /readyz reports 503 while its
|
|
# NATS connection is down, so `unhealthy` here means the worker genuinely
|
|
# cannot receive work.
|
|
environment:
|
|
LOCALAI_SERVE_ADDR: "0.0.0.0:50051"
|
|
LOCALAI_ADVERTISE_ADDR: "worker-1:50051"
|
|
LOCALAI_ADVERTISE_HTTP_ADDR: "worker-1:50050"
|
|
DEBUG: "true"
|
|
LOCALAI_REGISTER_TO: "http://localai:8080"
|
|
LOCALAI_NODE_NAME: "worker-1"
|
|
LOCALAI_REGISTRATION_TOKEN: "changeme" # Must match frontend token
|
|
LOCALAI_HEARTBEAT_INTERVAL: "10s"
|
|
LOCALAI_NATS_URL: "nats://nats:4222"
|
|
GODEBUG: "netdns=go" # See note in localai service
|
|
MODELS_PATH: /models
|
|
volumes:
|
|
- worker_1_models:/models
|
|
depends_on:
|
|
localai:
|
|
condition: service_started
|
|
nats:
|
|
condition: service_started
|
|
|
|
# --- GPU Support (NVIDIA) ---
|
|
# Uncomment the following and change the image to a CUDA variant
|
|
# (e.g., localai/localai:latest-gpu-nvidia-cuda-12) to enable GPU.
|
|
#
|
|
# NVIDIA_DRIVER_CAPABILITIES must include `utility` so nvidia-smi / NVML
|
|
# are available inside the container; without it the worker cannot report
|
|
# free VRAM and the Nodes page will show 0 free / total used.
|
|
# `init: true` avoids zombie-reap races that make nvidia-smi flaky.
|
|
#
|
|
# init: true
|
|
# environment:
|
|
# NVIDIA_DRIVER_CAPABILITIES: "compute,utility"
|
|
# deploy:
|
|
# resources:
|
|
# reservations:
|
|
# devices:
|
|
# - driver: nvidia.com/gpu
|
|
# count: all
|
|
# capabilities: [gpu, utility]
|
|
|
|
# --- Shared Volume Mode (optional) ---
|
|
# If all services run on the same Docker host, you can skip gRPC file transfer
|
|
# by sharing a single models volume. Replace the volumes above with:
|
|
#
|
|
# localai:
|
|
# volumes:
|
|
# - shared_models:/models
|
|
# - frontend_data:/data
|
|
#
|
|
# backend-llama-cpp:
|
|
# volumes:
|
|
# - shared_models:/models
|
|
#
|
|
# Then add to the volumes section:
|
|
# shared_models:
|
|
#
|
|
# With shared volumes the model files are already present on every worker at
|
|
# the same path. Set LOCALAI_DISTRIBUTED_SHARED_MODELS=true on the frontend
|
|
# (see its environment above) so the router skips gRPC file staging and the
|
|
# worker loads the model directly from the shared path instead of
|
|
# re-downloading it into a per-model subdirectory.
|
|
|
|
# --- Adding More Workers ---
|
|
# Copy the worker-1 service above and change:
|
|
# - Service name (e.g., worker-2)
|
|
# - LOCALAI_NODE_NAME (must be unique)
|
|
# - LOCALAI_ADVERTISE_ADDR (must match service name)
|
|
#
|
|
# Workers are generic — no backend type needed. The SmartRouter
|
|
# will dynamically install the required backend via NATS when
|
|
# a model request arrives.
|
|
|
|
# --- Agent Worker ---
|
|
# Dedicated process for agent chat execution.
|
|
# Receives chat jobs from NATS, runs cogito LLM calls via the LocalAI API,
|
|
# and publishes results back via NATS for SSE delivery.
|
|
# No database access needed — config and skills are sent in the NATS payload.
|
|
|
|
agent-worker-1:
|
|
# image: localai/localai:latest-cpu
|
|
build:
|
|
context: .
|
|
dockerfile: Dockerfile
|
|
args:
|
|
- IMAGE_TYPE=core
|
|
- BASE_IMAGE=ubuntu:24.04
|
|
# Install Docker CLI and start agent-worker.
|
|
# The Docker socket is mounted from the host so that MCP stdio servers
|
|
# using "docker run" commands can spawn containers on the host Docker.
|
|
entrypoint: ["/bin/sh", "-c"]
|
|
command:
|
|
- |
|
|
apt-get update -qq && apt-get install -y -qq docker.io >/dev/null 2>&1
|
|
exec /entrypoint.sh agent-worker
|
|
# The agent worker is NATS-only — no HTTP server to probe. The image's
|
|
# healthcheck detects that mode and reports healthy rather than probing a
|
|
# port that will never bind, so no override is needed here.
|
|
environment:
|
|
LOCALAI_NATS_URL: "nats://nats:4222"
|
|
LOCALAI_REGISTER_TO: "http://localai:8080"
|
|
LOCALAI_NODE_NAME: "agent-worker-1"
|
|
LOCALAI_REGISTRATION_TOKEN: "changeme" # Must match frontend token
|
|
GODEBUG: "netdns=go" # See note in localai service
|
|
volumes:
|
|
- /var/run/docker.sock:/var/run/docker.sock
|
|
depends_on:
|
|
localai:
|
|
condition: service_started
|
|
nats:
|
|
condition: service_started
|
|
|
|
volumes:
|
|
postgres_data:
|
|
frontend_models:
|
|
frontend_data:
|
|
worker_1_models:
|