How basecamp/kamal-proxy works
Project Type and Purpose
• This is a Go-based CLI tool plus HTTP proxy service (“Kamal Proxy”) designed to coordinate zero-downtime deployments.
• The project consists of two logical tiers:
– Control Plane (CLI commands)
– Data Plane (long-running HTTP proxy server)
High-Level File Structure Analysis
• cmd/kamal-proxy/main.go
– Entry point for the CLI binary. Parses flags/subcommands.
• internal/cmd
– Implements each CLI subcommand (run, deploy, remove, rollback, pause, resume, list, etc.).
– These commands invoke the server’s control API.
• internal/server
– Core HTTP proxy implementation: server.go, router.go, middleware, load_balancer, service_map, controllers (rollout, pause), TLS cert management, error pages.
– Exposes two APIs:
a. Control API (for CLI commands)
b. Proxy API (public-facing HTTP endpoints for application traffic)
• internal/metrics
– Collects and formats Prometheus-style metrics.
• internal/pages
– Embedded HTML templates for error pages (404, 502, etc.).
• example/
– Docker Compose setup demonstrating how to run Kamal Proxy and upstream services.
Key Architectural Insights from the README
• CLI “kamal-proxy run” launches the HTTP proxy server with default ports and options (overridable via flags or env vars).
• CLI “deploy/remove/pause/rollout” commands invoke health checks, update the in-memory service map, and orchestrate traffic shifting with zero downtime.
• Data plane: HTTP traffic is received on one or more ports, passed through a chain of middleware (logging, request buffering, request ID), routed by host/path to the appropriate service, passed through a load balancer to healthy targets, and proxied.
• Health checking runs in the background for each deployed target.
• Metrics are exposed for Prometheus and include controller metrics, latency, error rates.
• TLS management (auto and manual) is layered onto the proxy.
Main Components
A. Control Plane
1. CLI Binary (kamal-proxy)
2. Control API Server (internal/server/commands.go) – listens on Unix socket or HTTP port
3. Service Map & Controllers
• RolloutController – orchestrates zero-downtime swap of old→new
• PauseController – pauses/resumes services
4. Health Checker – polls targets and updates their status in the Service Map
B. Data Plane
1. HTTP Server
2. Middleware Chain
• RequestStart → RequestID → Logging → Buffering → ResponseBuffering → ErrorPages
3. Router
• Host-based routing
• Path-based routing (with optional prefix stripping)
4. Load Balancer – picks from healthy targets in service map
5. TLS Layer – automatic or manual certificate loader (via cert.go)
6. Error Page Templates (embedded in binary)
C. Observability
1. Metrics Collector (internal/metrics)
2. /metrics endpoint for Prometheus
D. External Services
1. Upstream Application Instances
2. Prometheus (for scraping metrics)
3. Docker / Kubernetes (deployment environments)
Relationships and Data Flows
CLI → Control API → Controllers → Service Map → Health Checker
Client HTTP Request → TLS Layer → Middleware Chain → Router → Load Balancer → Upstream Target → Response back through middleware → Client
• Metrics Flow: Server internals → Metrics Collector → /metrics → Prometheus
Architectural Patterns and Design Principles
• Separation of Concerns: clear split between Control Plane (CLI + API) and Data Plane (proxying).
• Middleware Pattern: each cross-cutting concern (logging, buffering, request IDs) is a standalone HTTP middleware.
• Controller Pattern: orchestrators (rollout/pause) manage in-memory state.
• Embedded Resources: error pages and static assets are compiled into the binary.
Putting It All Together
4. Host- and path-based routing as sub-components of the Router.
5. Zero-downtime flow: the RolloutController updates Service Map entries only after health checks pass, and Load Balancer shifts traffic seamlessly.