Scaling and high availability
Werbot scales horizontally and runs highly available in the Community edition. This guide covers topology, load balancing, PostgreSQL, recordings, gateway pools, Kubernetes, resources, metrics and a runnable cluster example.
Topology
Section titled “Topology”- N replicas of
werbot serverbehind a load balancer. - One PostgreSQL primary.
- S3-compatible storage for session recordings.
- Gateway pools: several
werbot gatewayinstances per pool. - The UI is embedded in
werbot server(default), or it runs on its own host and domain as replicas ofwerbot web(WERBOT_WEB_EMBEDDED=falseon the server,WERBOT_WEB_API_URLonwerbot web; see web.md). SetWERBOT_WEB_URLon the server to the UI address (links in emails) andWERBOT_TRUSTED_PROXIESto thewerbot webaddress (client IPs in sessions and audit).
All state lives in PostgreSQL:
- Signals between replicas use
LISTEN/NOTIFY. - One-time start-up work runs under an advisory lock.
- The River leader schedules periodic jobs and any replica runs them.
Redis and NATS are not needed. The default all-in-one mode (werbot server, one address for API and UI) is the single-replica case of the same design. There is no Helm chart in v0.x. A one-host example is in Cluster example.
Load balancing
Section titled “Load balancing”| Port | Balancer | Notes |
|---|---|---|
WERBOT_HTTP_ADDR (UI and API) |
L7, any replica | Sticky sessions are not needed: sessions live in PostgreSQL. Health check GET /readyz. Put the balancer addresses in WERBOT_TRUSTED_PROXIES. |
WERBOT_WEB_ADDR (UI and API through werbot web, when the UI is separate) |
L7, any werbot web replica |
Terminate TLS on the balancer. Health check GET /readyz of web (it does not depend on the server). Put the balancer address in WERBOT_TRUSTED_PROXIES of web and the web subnet in WERBOT_TRUSTED_PROXIES of the server. |
WERBOT_GATEWAY_ADDR (gateway tunnel) |
L4 TCP passthrough only | No TLS termination (mTLS ends on the replica) and no PROXY protocol. The external port must equal the port of WERBOT_GATEWAY_ADDR, because a gateway dials https://<host of WERBOT_PUBLIC_URL>:<port of WERBOT_GATEWAY_ADDR>. Long streams rebalance by themselves: a replica closes a stream after 30 minutes ±20% and the gateway reconnects at once without dropping SSH sessions. |
WERBOT_GATEWAY_SSH_ADDR (SSH to a pool) |
L4 with PROXY protocol v2 | HAProxy send-proxy-v2, nginx proxy_protocol on or AWS NLB proxy protocol v2, plus WERBOT_GATEWAY_PROXY_PROTOCOL=true on the instances. Expose the port to the balancer only. Health check GET /readyz on WERBOT_GATEWAY_HTTP_ADDR. Without a balancer use DNS round-robin: all instances of a pool serve one host key. |
WERBOT_GATEWAY_WS_ADDR (SSH over WebSocket, optional) |
L7, any instance of the pool | TLS on the balancer or on the gateway (WERBOT_GATEWAY_WS_TLS_CERT, WERBOT_GATEWAY_WS_TLS_KEY). Pass Upgrade and Connection and set X-Forwarded-For; put the balancer address in WERBOT_GATEWAY_TRUSTED_PROXIES. Health check GET /readyz on WERBOT_GATEWAY_HTTP_ADDR. See gateway.md. |
The L4 idle timeout of the balancer must be longer than WERBOT_GATEWAY_IDLE_TIMEOUT (default 5m). The gateway ends idle sessions by itself; a shorter balancer timeout cuts them first and without the idle_timeout reason. It must also exceed the idle_timeout of a resource, which can be set up to 24 h. The example nginx.cluster.conf uses proxy_timeout 1h, which covers only resources with idle_timeout of 1 h or less; for longer ones raise proxy_timeout in the stream block (for example 25h) and reload nginx.
WebSocket streams
Section titled “WebSocket streams”The web terminal and the live view use WebSockets on /api/terminal and /api/sessions/<id>/live (web-terminal.md). The L7 balancer in front of the server (or of werbot web) must:
- pass
UpgradeandConnection(nginx:proxy_http_version 1.1;,proxy_set_header Upgrade $http_upgrade;,proxy_set_header Connection $connection_upgrade;withmap $http_upgrade $connection_upgrade { default upgrade; '' close; }); - keep an idle connection at least 60 seconds: the server pings every browser connection every 30 seconds (nginx:
proxy_read_timeout 1h;, as in the examplenginx.cluster.conf); - pass
Originand the session cookie unchanged.Originmust equal the origin ofWERBOT_WEB_URL.
Any replica may take the WebSocket; no sticky sessions are needed.
Internal listener
Section titled “Internal listener”When a browser lands on replica A and the gateway holds its tunnel on replica C, A bridges the stream to C over the internal listener.
WERBOT_INTERNAL_ADDR(default:8444) is an mTLS-only listener (TLS 1.3, HTTP/2). It accepts only client certificates that the internal CA issued to a registered replica. Keep it on the private network; it needs no balancer.WERBOT_INTERNAL_URLis the address the other replicas dial. Its host is in the certificate of the listener, and it must resolve and be reachable from every replica. The default ishttps://<os hostname>:<port>. Replicas started by composedeploy.replicasreport the container id as hostname; it resolves on a user-defined network, so the example works as is. Set the variable explicitly when the hostname does not resolve from the other replicas, and always on Kubernetes: use the pod IP, for examplehttps://$(POD_IP):8444.- With one replica nothing crosses the network, and no replica-to-replica connectivity is needed.
- Each replica holds at most 1000 streams (browser WebSockets plus bridges for other replicas). A browser whose own replica is full gets close code
4008; when only the replica bridging the stream is full, the browser waits and gets1011(error) after 10 s. - PostgreSQL connections do not change: the signals use the existing
LISTENconnection.
The balancer also needs enough file descriptors for the sessions it relays (the example sets worker_rlimit_nofile 65536 and ulimits: { nofile: 65536 }). Werbot v0.1.x has no per-IP limit for unauthenticated SSH connections: limit connections per source IP on the balancer or firewall (security.md).
PostgreSQL
Section titled “PostgreSQL”Connect directly, or through PgBouncer in session mode only: transaction and statement mode break LISTEN and River.
- Pool size per replica:
pool_max_connsinWERBOT_DATABASE_URL. - Connection budget:
replicas × (pool_max_conns + 2) ≤ max_connections(the 2 are theLISTENconnection and the start-up work). Example: 3 × (20 + 2) = 66 ≤ 100. statement_timeoutdefaults to30s; override it in the URL. An exhausted pool answersUnavailableinstead of hanging.jitdefaults tooff.- High availability is the operator’s job (Patroni, CloudNativePG). A failover needs no Werbot restart.
- Read replicas are not used.
Session recordings
Section titled “Session recordings”The file system (WERBOT_DATA_DIR/recordings, used while WERBOT_RECORDINGS_S3_BUCKET is empty) is for one server replica or a ReadWriteMany volume. With more than one replica use S3-compatible storage (AWS S3, R2, SeaweedFS) through WERBOT_RECORDINGS_S3_*. Otherwise chunks of one session land on different nodes and Verify reports chunk_missing.
An unreachable S3 does not drop sessions: chunks wait in the gateway spool and upload later.
Gateway pools
Section titled “Gateway pools”A pool is a name in the UI and the gateway_id of a resource; its processes are instances.
- Instances join with the reusable token of the pool (limits and handling: security.md). For autoscaling set
max_usesto the number of instances and a short lifetime, keep the token in the orchestrator secret store and revoke it after the rollout. - Remove a retired instance with Delete instance (its tunnel closes). Instances whose certificate expired more than 24 h ago and tokens that expired more than 7 days ago are deleted automatically.
- The pool has one SSH host key for all instances.
- Pool connectors are the intersection of the connectors of its online instances; that is how specialised pools work (connectors.md).
- The embedded gateway (
server --with-gateway) needs no join token from you: gateway.md. - Steps in the UI: gateway.md. Threats: security.md.
Kubernetes
Section titled “Kubernetes”- web: a
Deploymentwithout PVC and secrets.readinessProbehttpGet /readyzandlivenessProbehttpGet /healthzon the port ofWERBOT_WEB_ADDR. - server: a
Deployment.readinessProbehttpGet /readyzandlivenessProbehttpGet /healthzon the port ofWERBOT_HTTP_ADDR(liveness does not ping the database, so a PostgreSQL failover does not restart pods). Uselifecycle.preStop.sleep.seconds: 10(Kubernetes 1.30 or newer; the distroless image has no shell, soexec: sleepdoes not work) so the endpoint leaves the balancer first, andterminationGracePeriodSeconds: 40(preStop 10 s + HTTP shutdown 10 s + River 10 s + margin). - gateway: a
StatefulSetwithvolumeClaimTemplatesonWERBOT_GATEWAY_DATA_DIR(instance identity, pool host key, recording spool). Without a PVC a restart loses unsent chunks: that recording failsVerifyand the UI shows it. A recreated pod with the same PVC does not spend a token. SetWERBOT_GATEWAY_HTTP_ADDRand usereadinessProbehttpGet /readyzon it, with the samepreStop.terminationGracePeriodSecondsmust be at least 10 +WERBOT_GATEWAY_DRAIN_TIMEOUT+ 10 (60 with the default30s). - Services: SSH is a
LoadBalancerwith L4 and PROXY protocol; the server tunnel is L4 without it.
Resources
Section titled “Resources”GOMEMLIMIT= 90% of the container memory limit (a 1 GiB limit givesGOMEMLIMIT=920MiB).- Open files:
ulimit -nat least 4 ×WERBOT_GATEWAY_MAX_SESSIONS(default 10000, so 40000) plus 32 × the expected UDP sessions (up to 32 sockets per UDP session). Go raises the soft limit to the hard limit, so the hard limit matters: composeulimits: { nofile: 65536 }, systemdLimitNOFILE=65536. - Budgets checked by the load test: gateway instance RSS ≤ 200 MB + 256 KB × active sessions; server replica RSS ≤ 300 MB + 64 KB × tunnel streams; PostgreSQL connections per replica ≤
pool_max_conns+ 2. - Known limit: the exec decision of a command (classifier, path rules, policy) costs time in proportion to the bindings. The p99 stays under 5 ms at 1 000 bindings (tested on every run of the gateway tests). At 10 000 bindings and 200 command rules it measures 8–11 ms p99 and 3–4 ms p50 (person and agent, development machine). Splitting the policies per resource is planned for phase 3; until then keep an organization under about 1 000 bindings if every command must decide in 5 ms.
- The stand, method and results are in test/load/README.md (Linux only, manual).
Metrics
Section titled “Metrics”Prometheus text format, no histograms. The listeners are not published outside:
- server:
WERBOT_METRICS_ADDRserves/metrics(empty: disabled); - gateway:
WERBOT_GATEWAY_HTTP_ADDRserves/readyzand/metrics; the embedded gateway (--with-gateway) writes into the server registry; - web: its own
WERBOT_METRICS_ADDR.
| Metric | Process | Meaning |
|---|---|---|
werbot_tunnel_streams |
server | Open gateway tunnel streams on this replica (gauge). |
werbot_relay_streams |
server | Browser streams and bridges for other replicas held by this replica (gauge; limit 1000). |
werbot_relay_rejected_total{reason} |
server | Browser streams refused by this replica; reason is origin, limit, timeout, ticket or unavailable. |
werbot_snapshot_pushes_total |
server | Policy snapshots sent to gateways. |
werbot_db_pool_acquired_conns |
server | Database pool connections in use (gauge). |
werbot_db_pool_total_conns |
server | Database pool connections open (gauge). |
werbot_user_certs_issued_total |
server | SSH user certificates issued to werbot CLI sessions. |
werbot_gateway_sessions_active |
gateway | Sessions holding a WERBOT_GATEWAY_MAX_SESSIONS slot (gauge). |
werbot_gateway_sessions_rejected_total{reason} |
gateway | Refused new sessions; reason is busy, spool_full or not_ready. |
werbot_gateway_observer_drops_total |
gateway | Live view events an observer lost because it fell behind. |
werbot_gateway_panics_total |
gateway | Connector panics recovered by the pipeline. |
werbot_gateway_spool_bytes |
gateway | Unsent recording bytes in the spool (gauge). |
werbot_gateway_tunnel_connected |
gateway | 1 while the tunnel stream is open and its latest snapshot compiled, else 0. |
werbot_gateway_session_bytes_total{direction} |
gateway | Session payload bytes; direction is up (client to target) or down. |
werbot_gateway_datagrams_total{direction} |
gateway | Session datagrams; direction is up or down. |
werbot_gateway_datagrams_dropped_total{reason} |
gateway | Datagrams dropped by the udp connector; reason is flows (more than 32 flows) or size (over 8192 bytes). |
werbot_gateway_health_checks_total{result} |
gateway | Resource health checks; result is ok or fail. |
werbot_gateway_denials_dropped_total |
gateway | Refused connections not reported: more than 100 per heartbeat. |
werbot_web_upstream_errors_total |
web | Proxied requests that failed to reach the API server (answered 503). |
Upgrades
Section titled “Upgrades”In v0.x upgrade only with all replicas and instances stopped. Order: upgrading.md.
Cluster example
Section titled “Cluster example”deploy/docker-compose.cluster.yaml with deploy/nginx.cluster.conf (project werbot-cluster) starts:
- 2
serverreplicas without a published HTTP port; - 2
webreplicas; lb(nginx) with ports 8080 (toweb), 8443 (tunnel) and 2222 (SSH) following Load balancing;gateway-1andgateway-2in the profilegateways;- PostgreSQL and SeaweedFS with the bucket
werbot-recordings.
It needs Docker Compose 2.23.1 or newer. The example and the test/cluster test stand share the project name and host ports: run one at a time.
- Download
docker-compose.cluster.yaml,nginx.cluster.confand.env.exampleof your tag fromhttps://raw.githubusercontent.com/werbot/werbot/<tag>/deploy/into one directory; copy.env.exampleto.env. - Fill
WERBOT_VERSION(your tag),WERBOT_SECRET_KEYandPOSTGRES_PASSWORDin.envas in install.md. - Set
WERBOT_PUBLIC_URLin.envto a DNS name or IP of this host that containers can reach, for examplehttp://<host-ip>:8080.localhostdoes not work: gateways dial the tunnel at that host on port 8443. Find the IP withhostname -I(Linux) oripconfig getifaddr en0 || ipconfig getifaddr en1(macOS). - Append the S3 keys:
echo "WERBOT_RECORDINGS_S3_ACCESS_KEY=$(openssl rand -hex 12)" >> .envandecho "WERBOT_RECORDINGS_S3_SECRET_KEY=$(openssl rand -hex 24)" >> .env. - Start:
docker compose -f docker-compose.cluster.yaml up -d --wait. - Open the UI (on this host
http://localhost:8080; from another machine see install.md) and complete Setup. - Gateways, Create pool, Create join token with
max usesof 2 or more. AppendWERBOT_GATEWAY_JOIN_TOKEN=<token>to.env. - Start the gateways:
docker compose -f docker-compose.cluster.yaml --profile gateways up -d --wait. Both instances show online. - Connect:
ssh -p 2222 <login>@<host>.
Limits of the example:
- One host shows the topology but gives no high availability.
- SeaweedFS on one disk is not for production.
- The default network uses
172.28.0.0/24; change it in the file if it is taken. Dynamic container addresses come from172.28.0.128/25. The server trustsX-Forwarded-Foronly from that range (thewebreplicas);webtrusts onlylb(172.28.0.10). Gateways sit in the separateedgenetwork and reach onlylb, so they cannot spoof client IPs to the server and never seeWERBOT_SECRET_KEY, the database password or the S3 keys. In production keep onlylband thewebreplicas in the server network. - Enrollment uses
--insecure-enrollon the privateedgenetwork of one host; across hosts use anhttps://server URL. - Scale with
docker compose -f docker-compose.cluster.yaml up -d --scale server=3after checking the formula in PostgreSQL, or--scale web=3.