{"article":{"title":"Containers & multiple nodes","breadcrumb":["Upstream channels","Containers & multiple nodes"],"body":"**What this solves**: Run the Local Node in Docker, docker compose or Kubernetes, and run as many nodes as you need behind your own load balancer. Installation, registration tokens and channel settings are covered in [Local Nodes](local-nodes).\n\nThe image and manifests here need node 0.4.40 or later.\n\n### Choosing a deployment\n\n| Option | Fits | Depends on |\n|---|---|---|\n| A single container | One machine, one node | Nothing |\n| docker compose | Several nodes on one machine, nginx in front | Optional Redis |\n| Kubernetes | Any number of replicas in a cluster | Optional Redis |\n\nRedis is the only component nodes can share, and it is optional. One node does not need it. Several nodes work without it too; the difference is described under \"Load balancing and shared state\". Nodes need no database and no message queue.\n\n### Building the image\n\nThe image is built from a released node version. The build files are at `https://www.jamerly.ai/tare-local-node/docker/`:\n\n```bash\nmkdir tare-node && cd tare-node\nfor f in Dockerfile compose.yaml compose.dlp.yaml nginx.conf kubernetes.yaml Dockerfile.dlp-sidecar; do\n  curl -fsSLO \"https://www.jamerly.ai/tare-local-node/docker/$f\"\ndone\n\nVERSION=0.4.40\ndocker build --build-arg VERSION=$VERSION -t tare-local-node:$VERSION .\n```\n\nThe build downloads that version's release from `s.jamerly.ai`, checks the Ed25519 signature of the release manifest and the SHA-256 of the package, and fails if either does not match. The signature is checked against the same public key the node uses for automatic updates.\n\n- The image tag is the node version. Upgrade by changing the tag.\n- To push to your own registry for both amd64 and arm64: `docker buildx build --platform linux/amd64,linux/arm64 --build-arg VERSION=$VERSION -t <your registry>/tare-local-node:$VERSION --push .`\n- The process runs as a non-root user (uid 10001). Everything it writes is under `/var/lib/tare-local-node`.\n- Automatic updates are off in the image (`TARE_AUTO_UPDATE=off`).\n\nVariables already set in the image:\n\n| Variable | Value in the image |\n|---|---|\n| `TARE_CONTROL_BASE_URL` | `https://tare.jamerly.ai` |\n| `TARE_TUNNEL_ADDR` | `tare-grpc.jamerly.ai:443` |\n| `TARE_NODE_LISTEN` | `0.0.0.0:8090` |\n| `TARE_NODE_CREDENTIALS_PATH` | `/var/lib/tare-local-node/credentials.json` |\n| `TARE_NODE_SHUTDOWN_GRACE_SECONDS` | `300` |\n| `TARE_AUTO_UPDATE` | `off` |\n\n### A single container\n\n```bash\ndocker volume create tare-node-1\n\ndocker run -d --name tare-node-1 --restart unless-stopped \\\n  --stop-timeout 330 \\\n  -v tare-node-1:/var/lib/tare-local-node \\\n  -p 127.0.0.1:8090:8090 \\\n  -e TARE_API_KEY=<an inference key of your account> \\\n  -e TARE_REGISTRATION_TOKEN=<registration token> \\\n  tare-local-node:$VERSION\n```\n\nOn first start the node redeems the registration token for its identity and writes it to `credentials.json` in the volume. Later restarts and new images read that file, so the token can be dropped from the command. The node is registered when the log shows `tunnel connected as node …` and **Local Nodes** in the console shows it **Online**.\n\nPoint the business at `http://<this machine>:8090/v1` with its own Tare API key. `-p 127.0.0.1:8090:8090` allows only the local machine; change the address to serve other machines. Callers that reach the port still need a valid API key.\n\nThe reason for `--stop-timeout 330` is under \"Shutdown and upgrades\". Docker waits only 10 seconds by default, which cuts long streaming calls.\n\n### What must persist\n\n`/var/lib/tare-local-node` needs a persistent volume, one per node, never shared.\n\n| File | Holds | If it is lost |\n|---|---|---|\n| `credentials.json` | The node identity (node id and secret) | The node does not start. It needs a new registration token and becomes a new node; the old one stays offline in the console until you revoke it in **Local Nodes** |\n| `usage-wal/` | Usage not yet confirmed by Tare | Those calls never appear in Call Traces or the bill and do not count against the key's limit. A normal shutdown sends them first; leftovers only come from a power cut or a forced kill |\n| `keys.json` | Keys kept only on the node | Channels using those keys are skipped on this node |\n| `peer-identity.pem` | The certificate for node-to-node (P2P) connections | Generated again at start; until the other side has the new certificate, direct calls through this node go through Tare |\n| `logs/` | The local request log | Only local troubleshooting is affected |\n\nWhen two node processes start on the same state directory, the second one exits with `another tare-local-node process is using …`. Two nodes sharing one identity keep pushing each other's connection off.\n\n### Several nodes\n\nEvery node is its own node identity with its own volume and registers once. Each one gets its own row in the console.\n\nA registration token is single-use and valid for 24 hours. When several nodes share one configuration, give them several tokens, separated by commas or newlines:\n\n```bash\nTARE_REGISTRATION_TOKEN=\"<token 1>,<token 2>,<token 3>\"\n```\n\nOn first start each node goes down the list, skips the tokens other nodes have already used, and redeems the next one. The tokens can also live in a file (`TARE_REGISTRATION_TOKEN_FILE`, one per line), which is how a Kubernetes Secret is mounted. A node that already has an identity does not read the tokens; spent tokens can stay in the list.\n\nAdding node N+1: generate a new token, add it to the list, start a new node with a new volume. The other nodes are not touched.\n\nRemoving one: stop it, revoke it in **Local Nodes**, then delete its volume.\n\n### docker compose\n\n`compose.yaml` starts three nodes, nginx and Redis:\n\n```bash\nexport TARE_NODE_VERSION=0.4.40\nexport TARE_API_KEY=<an inference key of your account>\nexport TARE_REGISTRATION_TOKENS=\"<token 1>,<token 2>,<token 3>\"\ndocker compose up -d --build\n```\n\nThe business calls `http://<this machine>:8090/v1`; nginx spreads the calls over the three nodes.\n\n- Each node has its own volume (`node-1`, `node-2`, `node-3`).\n- The nodes take the client address from nginx's `X-Forwarded-For` (`TARE_NODE_CLIENT_IP_MODE=proxy`, trusting only addresses in the compose network), so keys with an IP allowlist keep working.\n- `stop_grace_period: 330s`; see \"Shutdown and upgrades\".\n- Adding a node: copy a `node-N` block and its volume, add it to the `upstream` in `nginx.conf`, add a new token to the list, then `docker compose up -d`.\n- Without Redis: remove the `redis` service, `TARE_NODE_REDIS_URL` and `depends_on`, and switch `nginx.conf` to the line that hashes on the key.\n\n### Kubernetes\n\n`kubernetes.yaml` is a StatefulSet with a PersistentVolumeClaim per replica, so every replica keeps its own identity and usage records.\n\n1. Generate one registration token per replica and fill in `registration-tokens` (one per line) and `api-key` in the Secret.\n2. Set `image` to the image you pushed to your own registry.\n3. `kubectl apply -f kubernetes.yaml`.\n\nWhat the manifest already sets:\n\n| Item | Value | Purpose |\n|---|---|---|\n| `readinessProbe` | `GET /readyz` | No traffic before the first connection to Tare, and none while shutting down |\n| `livenessProbe` | `GET /healthz` | Restart a stuck process |\n| `preStop` | `sleep 10` | The Service and ingress take the pod out before the node stops accepting |\n| `terminationGracePeriodSeconds` | `340` | 10 + 300 + 20 + margin |\n| PodDisruptionBudget | `maxUnavailable: 1` | Node maintenance evicts one replica at a time |\n| `securityContext` | uid / gid / fsGroup 10001 | Non-root; the volume belongs to the node process |\n\nAdding a replica: add a new token line to the Secret, then `kubectl scale sts/tare-local-node --replicas=<N+1>`. The new pod registers with an unused token.\n\nRemoving a replica: scale down first, revoke the extra node, then delete its PVC (`node-state-tare-local-node-<ordinal>`).\n\nThe Service exposes only 8090. Add an Ingress or load balancer the way your cluster does it, with timeouts long enough for streaming calls that run for minutes and with request and response buffering off.\n\n### Load balancing and shared state\n\nBehind one load balancer, calls with the same key land on different nodes. For the following, one copy per node is correct:\n\n| State | With several nodes |\n|---|---|\n| Session stickiness (a session stays on one channel) | A hash of session and channel; every node computes the same result, so upstream prompt caches keep hitting |\n| Channel circuit breaking and health | Decided by Tare and pushed to every node |\n| Key checks, balance, limits | Every node checks with Tare and caches the answer; changes are pushed to every node |\n| Latency figures for `provider.sort: latency` | Each node ranks by the latency it measured itself |\n| Rate limiting of invalid keys per source | Counted per node, so N nodes allow N times as much |\n\nOne thing needs Redis or a load balancer setting to work across nodes: **replay after a client disconnects**. When a client drops mid-call (a network glitch, a proxy timeout), the node does not cut the upstream call; it reads it to the end and bills it in full. If the same key sends an identical request within 30 seconds (the client's retry), the node hands it the response of that call instead of calling the upstream again. The replay is billed the service fee once more on the original call's tokens, with zero upstream cost.\n\nOn one node this happens in the node's memory. With several nodes the retry may land on another node, which does not have the original call and calls the upstream again from scratch. Two ways to handle it:\n\n- **Use Redis** (recommended). Set the same `TARE_NODE_REDIS_URL` on every node. A call whose client left writes its response to Redis, and the retry is replayed on whichever node it reaches. The load balancer can then use least-connections or round-robin.\n- **No Redis**: have the load balancer hash on the API key (the `Authorization` and `x-api-key` headers) so a key always reaches the same node. A key with very heavy traffic then uses only one node.\n\n| Variable | Default | Notes |\n|---|---|---|\n| `TARE_NODE_REDIS_URL` | empty | `redis://[user:password@]host:port/db`, or `rediss://…` for TLS |\n| `TARE_NODE_REDIS_PREFIX` | `tare-node:` | Key prefix, for a Redis shared with other applications |\n\nWhat goes into Redis:\n\n- The response of a call whose client left, encrypted, expiring 30 seconds after the call ends. The decryption key is derived from the tenant, the caller's API key and the original request; Redis never holds it, so whoever can read Redis cannot read the responses. Channels' upstream keys never go into Redis.\n- Per-second counters for upstreams with a calls-per-second limit (currently Alibaba Cloud Bailian image generation), expiring after 2 seconds. With Redis, all nodes together stay under the upstream's limit; without it each node keeps to a fixed share.\n\nRedis needs no persistence, and a few hundred MB of memory is enough. `compose.yaml` runs it with `--save \"\" --appendonly no --maxmemory 512mb --maxmemory-policy volatile-ttl`. While Redis is unreachable the nodes keep serving and these two features fall back to each node's own behaviour; the log gets at most one `shared state: …` line a minute.\n\n### Keys kept only on the node\n\nFor channels with **Keep the API key only on my node**, the key is in the node's `keys.json`. Tare hands such a channel to any online node of the account, so every node's `keys.json` needs the entry.\n\n- docker compose: mount the same `keys.json` read-only into every node and point `TARE_NODE_HELD_KEYS` at it.\n- Kubernetes: create a Secret with one key, `keys.json`, and mount it with the commented-out `held-keys` lines in the manifest. Nodes reread it after the Secret changes, without a restart.\n\nChannels pinned to one hosting node ([subscription channels](subscription-supply), [shared local models](local-models)) run only on that node; other nodes pass calls to it over P2P or through Tare. While that node is down the channel is skipped. Keep such channels on a fixed machine, not in replicas that get recreated or scaled down.\n\n### Data protection on the node\n\nWhen the account has data protection (DLP) on, direct calls are scanned by a scanning service next to the node. If the node cannot reach it, the node refuses the call (`503`) instead of forwarding unscanned text. Without data protection the scanning service is not needed.\n\nThe scanning service listens on `127.0.0.1` only, so it has to share the node's network namespace, one per node:\n\n- Kubernetes: a second container in the same pod; the manifest has a commented-out `dlp-sidecar`. Also turn on `shareProcessNamespace: true`: its `preStop` waits for the node process to exit first, so calls are still scanned while the node finishes.\n- docker compose: `compose.dlp.yaml`. Each node joins its scanner's network with `network_mode: service:dlp-N` and depends on it, so shutdown stops the node before the scanner. Ports and the network belong to the scanner container.\n\nBuild the scanner image from `Dockerfile.dlp-sidecar` (`docker build -f Dockerfile.dlp-sidecar -t tare-dlp-sidecar:stable .`); it is linux/amd64 only. Give each scanner 1 GB of memory, or 2 GB with person, place and organization detection on (`TARE_DLP_SIDECAR_HEAP=2g`). Those three types use a model of about 180 MB that is downloaded to `/tmp` on first use; a volume on `/tmp` saves the download on every recreate.\n\n### Health checks\n\n| Path | Answers | Used for |\n|---|---|---|\n| `GET /healthz` | `200` while the process is up | Liveness probe |\n| `GET /readyz` | `200` once the node has connected to Tare; `503` before the first connection and after a shutdown signal, with the reason in the body | Readiness probe, load balancer health check |\n| `GET /metrics` | Prometheus metrics on a separate port (`TARE_NODE_METRICS_LISTEN`, e.g. `0.0.0.0:9464`) | Monitoring |\n\nAfter the first connection, `/readyz` stays `200` even if the connection to Tare drops: the node keeps serving from the last push and its cache (see \"The node is up, but it cannot reach Tare\" in [Local Nodes](local-nodes)). The `tare_node_tunnel_connected` metric is `0` at that point; alert on it.\n\nThe image has a Docker `HEALTHCHECK` that runs `tare-local-node healthcheck` (it asks the local `/readyz`), so `docker ps` shows `healthy`. `/healthz` and `/readyz` need no API key and `/metrics` has no authentication; keep them off the public internet.\n\n### Shutdown and upgrades\n\nOn `SIGTERM` the node exits in this order:\n\n1. `/readyz` answers `503` at once.\n2. With `TARE_NODE_DRAIN_DELAY_SECONDS` set, it keeps accepting new requests for that many seconds so the load balancer can take it out.\n3. It stops accepting new connections. Calls already running continue for up to `TARE_NODE_SHUTDOWN_GRACE_SECONDS` (300 in the image); whatever is still running then is cut off.\n4. It sends usage not yet confirmed by Tare, for up to 20 seconds. Anything left stays in `usage-wal/` and is sent on the next start.\n5. It disconnects from Tare and exits.\n\nWith no calls running, step 3 ends at once and the node exits within seconds.\n\nThe platform's kill deadline has to be longer than all of these together:\n\n| Platform | Setting | Value in the examples |\n|---|---|---|\n| `docker run` | `--stop-timeout` | `330` |\n| docker compose | `stop_grace_period` | `330s` (5 + 300 + 20 + margin) |\n| Kubernetes | `terminationGracePeriodSeconds` | `340` (`preStop` 10 + 300 + 20 + margin) |\n\n`TARE_NODE_DRAIN_DELAY_SECONDS` is for load balancers that take nodes out by polling `/readyz`; the compose example uses 5. In Kubernetes the `sleep 10` in `preStop` does the same job, so leave it unset there. To wait longer for long calls, raise `TARE_NODE_SHUTDOWN_GRACE_SECONDS` and the platform deadline together.\n\nUpgrading means changing the image tag:\n\n- Kubernetes: change the StatefulSet `image`. Replicas roll one at a time; the next one is replaced only after the previous one is ready.\n- docker compose: change `TARE_NODE_VERSION`, rebuild, and replace the nodes one at a time so they do not all stop together: `docker compose build`, then `docker compose up -d --no-deps node-1`, wait for it to be `healthy`, then `node-2`, and so on.\n- A single container: start a new container from the new image on the same volume after stopping the old one.\n\nThe node identity and usage records are in the volume, so a new image does not register again. Do not turn automatic updates on inside the image: they replace the file inside the container, and the next recreate goes back to the image's version.\n\n### Sizing\n\nA node mostly waits on upstreams and uses little CPU. Memory and disk go to:\n\n- **Memory**: responses waiting to be replayed after a client left stay in memory, at most 32 MB each and 512 MB in total, normally close to zero. The examples request 256 MB per node with a 1 GB limit.\n- **Disk**: the local request log takes up to 2048 MB by default; the examples lower it to 1024 MB (`TARE_NODE_LOG_MAX_TOTAL_MB`) on a 2 GB volume. The log and the usage records share the volume, so the log cap has to leave room for the usage records.\n- **Scanning service**: 1 to 2 GB of memory each, see above.\n- **Redis**: 512 MB is enough.\n\n### Troubleshooting\n\n| Symptom | Cause and fix |\n|---|---|\n| Exits at start: `no credentials at … and TARE_REGISTRATION_TOKEN is not set` | The volume has no identity and no token was given. Give a token; for a node that registered before, check that the volume is mounted |\n| Exits at start: `none of the N registration token(s) was accepted` | All tokens are used or expired (24 hours). Generate a new one and add it to the list |\n| Exits at start: `another tare-local-node process is using …` | Two nodes mount the same volume. Give each node its own |\n| Exits at start: `cannot store credentials at …` | The volume is not writable by uid 10001. In Kubernetes set `fsGroup: 10001`; in Docker use a named volume or `chown 10001:10001` the host directory |\n| `/readyz` stays `503 waiting for the first connection to Tare` | The node cannot reach `tare-grpc.jamerly.ai:443`. Check outbound firewall rules and proxies, and the `tunnel connection ended: …` lines in the log |\n| One node flips between online and offline in the console | Two processes use the same `credentials.json` (copied from one volume, for example). Every node needs its own identity |\n| After a retry the upstream was called twice and billed twice | Replay is not shared between the nodes. Set `TARE_NODE_REDIS_URL`, or hash on the API key in the load balancer |\n| `shared state: …` in the log | Redis is unreachable or slow. Nodes keep serving; replay and rate limiting fall back to each node's own behaviour for now |\n| A channel with a key kept only on the node returns `503` now and then | Some nodes' `keys.json` lacks the entry; their log has `channel N skipped`. Mount the same `keys.json` on every node |\n| Every call is `503` after turning on data protection | The scanning service is missing, or not in the node's network namespace. See \"Data protection on the node\" |\n| Streaming calls are cut during a rolling upgrade | The platform's kill deadline is shorter than `TARE_NODE_SHUTDOWN_GRACE_SECONDS` plus 20 seconds, or Docker is on its default 10 seconds. Adjust per the table in \"Shutdown and upgrades\" |"}}