# Nodes and fleet

One machine runs Hermitbox on its own; more machines join it as nodes. The control service decides
what runs where, and each node runs only what control asks of it.

| Part | Where it runs | What it does |
| --- | --- | --- |
| `hermitboxd` | Every host, as a node | Runs sandboxes and owns their local files; never chooses what to run |
| Control | One service, with Postgres | Keeps desired state, projects and keys; places sandboxes; reaches nodes only through their mutual-TLS API |
| Edge | In front, facing clients | Routes TLS, HTTP and TCP to sandboxes; holds a connection while control wakes one |
| Object store | An S3 store; on one host, beside control | Holds uploaded snapshots for rest, moves and rebuilds |

## One host

Every installation is a fleet of one. `hermitbox up` runs control, the edge, `hermitboxd`, Postgres
and an object store on the same machine, and enrolls the local daemon as its first node.

- **User preset:** an ordinary account, no root and no Docker. Everything listens on loopback, port
  7331 by default.
- **Server preset:** root on a systemd host, with Docker, nftables and a domain. Control and the
  edge run as systemd units, and the API gets a renewed TLS certificate for the domain.

[Install Hermitbox](https://docs.hermitbox.dev/v0.2.4/get-started/install.md) covers each preset. A single host needs nothing
more: the rest of this page is about adding hosts.

## Add a node

A node joins with a token that an operator mints for a region. On a trusted host with the operator
credentials, run:

```sh
# Mint a join token for a region.
hermitbox node token <region>
```

On the new host, keep the token in the environment, never in the command's arguments:

```sh
export HERMITBOX_JOIN_TOKEN='<fleet-token>'

curl -fsSL https://hbx.sh | \
  sudo --preserve-env=HERMITBOX_JOIN_TOKEN HERMITBOX_PRESET=node sh
```

The joining host checks the signed bootstrap with the release keys built into the CLI. It trusts
the fleet's certificate authority only when it matches the token's hash, makes its own key, and
never sends that key anywhere. A new node allows no container images until the operator names some
with `--allow-image`.

Control, the edge and the nodes talk over a private network, such as WireGuard. Once
`hermitbox node ls` shows the node healthy, placement starts using it. Machine providers can rent
and release hosts for you: see [Machine providers](https://docs.hermitbox.dev/v0.2.4/guides/add-machines.md).

## What control decides

A node never decides what should run; control does.

- **Placement.** Control picks a node by region, tier, required guarantees and features, and free
  memory, CPU, disk and ports. A sandbox the fleet can't fit is refused with `no_room`.
- **Epochs.** Each placement has an epoch. Every request that changes a sandbox carries it, and a
  node refuses an older one, so one sandbox never runs as two copies.
- **Moves.** Draining a node moves its sandboxes where another node has room. A resting sandbox can
  wake on any node in its region.
- **Health.** Nodes send heartbeats. A silent node is suspect after 15 seconds, and gets no new
  sandboxes; after 45 seconds it is unavailable.
- **Upgrades.** Control offers each node the signed daemon release, one healthy node per region at
  a time. A node that can't reconcile on the new release rolls back.

No timer declares a host lost. When a host is gone for good, you fence it outside Hermitbox (power
it off, or cut it from the private network) and confirm with `hermitbox node lost`. Control then
rebuilds its sandboxes from their newest uploaded snapshots, where another node has room.

## The edge

The edge is the only part that faces the internet. It reads the TLS server name, the HTTP host or a
dedicated TCP port to find a sandbox, using routes it polls from control. When the sandbox is
asleep, the edge holds the client's first bytes, asks control to wake it, then relays them. The
application sees an ordinary connection.

## When a part fails

| Failure | What keeps going |
| --- | --- |
| Control or Postgres down | Running sandboxes keep running; nothing new is decided until it returns |
| `hermitboxd` crashes | Containers and process sandboxes keep running; wasm sandboxes stop and keep their files |
| A host reboots | Its sandboxes resume only once control authorizes them |
| An edge dies | Its connections close; other edges keep serving |
| The object store is down | Local data stays; rest and rebuilds wait |

## Next steps

- [Operate Hermitbox](https://docs.hermitbox.dev/v0.2.4/guides/operate.md): drain, fence, upgrade and observe nodes.
- [Machine providers](https://docs.hermitbox.dev/v0.2.4/guides/add-machines.md): acquire and release hosts automatically.
- [Architecture](https://docs.hermitbox.dev/v0.2.4/reference/architecture.md): epochs, failures and the full flow.
