Many hosts behind one address
This page is for a person who runs boxfork on more than one machine, or in more than one cluster or cloud.
It tells you how to put all these daemons behind one address with the control service, boxfork-control.
At the end, you can set up the control service, join daemons and Kubernetes clusters to it, and select the cluster of a new sandbox.
Without the control service, each daemon has its own address, its own API key and its own list of sandboxes. A client must then keep the address and the key of each daemon, and it must know which daemon has which sandbox. With the control service, a client has one address, one API key and one list of sandboxes. The control service then sends each request to the correct daemon. The CLI, the Python SDK, the TypeScript SDK, the MCP server and the tool kit work with it as with one daemon.
internals/control.md tells you how the control service works.
In short
Section titled “In short”- Each daemon opens the connections to the control service, so the machine of a daemon needs no open port.
- A cluster is one daemon or the API service of one Kubernetes cluster. Both join with a join token.
- A client selects a cluster with a placement, for example
placement={"cloud": "aws"}. A daemon ignores the placement. - When the control service stops, the sandboxes continue to run, but the requests through it fail.
- Keep each join token as secret as an API key: a program with the token gets the requests of that cluster.
- No run used two machines, a real network or a real GPU.
What the control service does
Section titled “What the control service does”Each daemon joins the control service: it opens connections to the control service and keeps them open. The control service then sends the requests for that daemon through those connections. Thus the machine of a daemon needs no open port, and the control service needs no address of a daemon.
Each daemon is in one cluster, which has a name and labels, for example region=eu-west-1 and cloud=aws.
A cluster is one daemon or the API service of one Kubernetes cluster, and the two join in the same way.
The control service keeps its data in one SQLite file, control.db, in its data directory:
- each cluster, with the hash of its join token, its labels and the GPUs that it reported;
- the cluster of each sandbox, snapshot, pool and volume.
With these records, the control service knows where to send each request. A create puts the new sandbox in a cluster, and a later request for that sandbox goes to the same cluster. A list asks each cluster and merges the answers.
Set up the control service
Section titled “Set up the control service”- Build the binary.
make controlwritesbin/boxfork-control. To run it as a container, use the imageghcr.io/mvplab-ai/boxfork-control:<version>of a release, or build it with control/Dockerfile. The file tells you how to run it. - Make a join token for each cluster:
boxfork-control token --data-dir /var/lib/boxfork-control --label cloud=aws --label region=eu-west-1 cpu-aws. - Keep the token that the command prints. It shows the token only one time, and the file keeps only its hash.
- Start the service:
boxfork-control serve --listen 0.0.0.0:7443 --data-dir /var/lib/boxfork-control --tls-cert cert.pem --tls-key key.pem. - Read the API key of the control service from the file
api-keyin the data directory.
Caution: Use HTTPS if the control service listens on an address that other machines can reach. Without HTTPS, the API key, the join tokens and all data go across the network as plain text.
The name of a cluster has 1 to 63 lower-case letters, digits and -.
If you run token again for a cluster, the new token replaces the old one.
Use --label to fix the labels of a cluster.
With --label, the daemon of the cluster must report exactly these labels, or the control service refuses it.
Without --label, the daemon can report all labels, and the control service trusts them.
This is important because the labels select the cluster for a placement (see Security).
Each token command sets the labels again, so a command without --label removes the rule.
Commands of boxfork-control
Section titled “Commands of boxfork-control”| Command | Function |
|---|---|
boxfork-control serve [flags] |
Run the control service. |
boxfork-control token [--data-dir <dir>] [--label <key=value>]... <name> |
Make a join token for a cluster and print it. Add the cluster if it is new. With --label, the daemon must report exactly these labels. |
boxfork-control forget [--data-dir <dir>] <name> |
Remove a cluster and the records of its objects. Its daemon cannot join again. |
boxfork-control version |
Print the version. |
Flags of boxfork-control serve
Section titled “Flags of boxfork-control serve”| Flag | Default | Meaning |
|---|---|---|
--listen <host:port> |
127.0.0.1:7080 |
The address for the API and for the connections of the daemons. |
--data-dir <dir> |
BOXFORK_CONTROL_DATA_DIR, then /var/lib/boxfork-control for root, then ~/.boxfork-control |
The directory for the API key and the SQLite file. |
--tls-cert <file> |
none | The certificate file, in PEM format with its chain. It needs --tls-key. |
--tls-key <file> |
none | The private key file, in PEM format. |
--wait <duration> |
30s |
The maximum time that a request waits for a cluster without an open connection. |
Join a daemon
Section titled “Join a daemon”Give the join flags to boxfork serve on the machine of the daemon:
export BOXFORK_JOIN_TOKEN=bfj_... # the token that "boxfork-control token cpu-aws" printedboxfork serve --join https://control.example.com:7443 --cluster-name cpu-aws \ --cluster-label cloud=aws --cluster-label region=eu-west-1The daemon continues to serve its own API on --listen. The join only adds the connections to the control service.
reference/configuration.md gives each join flag.
Join a Kubernetes cluster
Section titled “Join a Kubernetes cluster”The API service of the Helm chart joins as one cluster.
It takes the same join flags as boxfork serve, and the chart gives them from the values join.*.
The join token and the certificate authority come from Secrets, so no value of the chart holds the token.
-
Make the join token on the machine of the control service, for example
boxfork-control token --label cloud=gcp k8s-eu. -
Put the token in a Secret in the namespace of the release:
Terminal window kubectl -n boxfork create secret generic bf-join --from-file=token=k8s-eu.token -
If the system does not trust the certificate of the control service, put its certificate authority in a Secret:
Terminal window kubectl -n boxfork create secret generic bf-join-ca --from-file=ca.crt=ca.pem -
Install the chart, or upgrade it, with the join values:
Terminal window helm upgrade --install bf operator/chart -n boxfork --reuse-values \--set join.control=https://control.example.com:7443 \--set join.clusterName=k8s-eu --set join.labels.cloud=gcp \--set join.tokenSecret.name=bf-join --set join.caSecret.name=bf-join-ca -
Find the line “joined the control service” in the log of the API service:
kubectl -n boxfork logs deploy/bf-api | grep joined.
install.md tells how to install the chart, k3s.md makes a cluster from a few Linux machines, and internals/kubernetes.md gives each value.
The cluster needs no open port and no Ingress for the control service. The API service opens the connections, and its network policy does not limit what goes out. The API service reports no GPUs, so the control service sends a sandbox with a GPU only to a daemon. internals/kubernetes.md gives the reason.
A drain of a node in a joined cluster moves the sandboxes of the node to a different node of the same cluster (see install.md). The ID of a sandbox does not change, so the control service continues to send its requests to the same cluster. A command waits while its sandbox moves. We did not try a drain through a control service.
Make sure that a cluster joined
Section titled “Make sure that a cluster joined”Ask the control service for its clusters:
curl -H "Authorization: Bearer $CONTROL_KEY" https://control.example.com:7443/v1/clustersThe answer has one item for each cluster. connected is true when the cluster has an open connection.
gpus gives the GPUs that the daemon reported when its last connection opened.
Use the control service
Section titled “Use the control service”Set the address and the API key of the control service for the client:
export BOXFORK_URL=https://control.example.com:7443export BOXFORK_API_KEY=abx_... # from the file api-key of the control serviceTo put a sandbox in a cluster, give its labels as a placement:
from boxfork import Sandbox
cpu = Sandbox.create(placement={"cloud": "aws"})gpu = Sandbox.create(gpu=1, placement={"region": "europe-west4"})print(cpu.run("uname -a").stdout)boxfork create --placement cloud=awsA daemon ignores the placement, so the same code works with one daemon and with the control service.
How the control service selects a cluster
Section titled “How the control service selects a cluster”The control service first finds the clusters that can take the sandbox. It looks only at the clusters that joined at least one time, and a cluster must agree with all of these rules:
- The cluster has each label of the placement, with the same value.
- For a sandbox with a GPU, the cluster reported that number of GPUs of the model. The free GPUs are not a rule, because the report can be old.
- For a sandbox from a snapshot, the cluster has the snapshot.
- For a sandbox with volumes, the cluster has each volume that the control service knows.
Then the control service puts these clusters in a sequence and tries them one after the other:
- Clusters with an open connection come first.
- For a sandbox with a GPU, the cluster with the most free GPUs comes first.
- For a sandbox without a GPU, the cluster with the fewest GPUs comes first. Then the cluster with the fewest sandboxes.
- If a cluster does not connect in the wait or has no free GPU, the next cluster gets the request.
A fork stays in the cluster of its sandbox, because the fork needs the files of that sandbox.
If no cluster agrees with the rules, the answer is no_cluster, or gpu_unavailable for a GPU.
What stops when the control service stops
Section titled “What stops when the control service stops”The control service holds no sandbox: all sandboxes are on the daemons. Thus the sandboxes continue to run when it stops, but the requests through it stop:
| Item | While the control service is stopped |
|---|---|
| Sandboxes | They continue to run. Each daemon applies the timeouts and the idle suspend. |
| A request through the control service | It fails at once: the client cannot connect. The Python SDK raises DaemonUnreachable. |
| A command that runs through the control service | It stops, as when a client closes its connection. A command in the background continues. |
| New sandboxes through the control service | None. |
| A request directly to a daemon | It works, with the address and the API key of that daemon. |
| The daemons | They try to connect again, first after 250 ms, then up to each 5 seconds. |
When the control service starts again, it reads its SQLite file and sends each request to the cluster of the object, as before. A request that comes before a cluster is connected again waits for that cluster for a maximum of 30 seconds. A sandbox that ended while the control service was stopped is not in the next list, and the control service then forgets it.
The control service writes the cluster of a new object before it answers the client. The file can lose that record, for example after a power failure. Then the control service asks each cluster for the object at the next request for it.
A cluster that goes away
Section titled “A cluster that goes away”When a cluster does not answer, the control service continues to work with the other clusters:
- A list has the field
unreachablewith the names of the clusters that did not answer in 3 seconds. The list has the objects of the other clusters. - A request for an object of that cluster waits for the cluster. Then the answer is
cluster_unavailablewith the status 503. Send the request again later. - A create goes to a different cluster that agrees with the rules.
- The control service keeps the records of the cluster. When the cluster connects again, its requests work again.
Remove a cluster
Section titled “Remove a cluster”- Stop the daemon, or remove the join flags from its command line and start it again.
- Run
boxfork-control forget <name>on the machine of the control service.
forget removes the cluster and the records of its objects, but it does not remove the sandboxes on the daemon.
Security
Section titled “Security”The control service adds two kinds of secret, its API key and the join tokens, and it trusts some of what the daemons report. Each item below gives one risk and, where one exists, the way to limit it:
- A person with the API key of the control service can use all sandboxes of all clusters.
- A request that comes through the connection of a daemon does not need the API key of that daemon. The join token made the connection trusted.
- A program with the join token of a cluster can join in the place of that cluster. It then gets the requests for that cluster: commands, files and their contents. Keep each join token as secret as an API key.
- The labels of a cluster select the cluster for a placement. Without
--labelontoken, the control service trusts the labels that the daemon reports. A daemon can then report the labels of a different cluster and get its new sandboxes. Give--labelto prevent this. - The control service also trusts the GPUs that a daemon reports. A daemon can thus report GPUs that it does not have, and then get the sandboxes that ask for a GPU. The control service uses the GPU report only to select a cluster.
- A cluster cannot get the requests for a sandbox, a snapshot or a pool of a different cluster. The first cluster that has an id keeps it, also if a different cluster lists the id. If two clusters say that they have an unknown object, the answer is
cluster_conflict, and the request goes to neither. - A new join token for a cluster, or
forget, ends the connections that the old token opened. The control service sends no request through them, and it closes the idle ones in a maximum of 5 minutes. - With an
httpsaddress, the daemon checks the certificate of the control service.--join-cagives a certificate authority of your own. - On Kubernetes, the join token and the certificate authority come from Secrets. A person who can read the Secret of the token in the namespace of the release can join in the place of the cluster.
- The daemon refuses an
http://address of--joinon a different machine, because the join token goes across the network as plain text. Only--join-insecurepermits it.
Limits
Section titled “Limits”- The control service has one instance, so the requests through it stop when it stops. See What stops when the control service stops.
- A pool is in one cluster. A create of a sandbox with the same shape uses the pool only if the control service selects that cluster.
- A request for a pool goes first to a cluster that has a pool with each field of the shape that the request gives. That daemon then changes the size of its pool, as one daemon does.
- The GPUs of a cluster are as the daemon reported them when its last connection opened. The daemon of the cluster makes the last decision.
- The API service of a Kubernetes cluster reports no GPUs, because it cannot make a sandbox with a GPU yet. A sandbox with a GPU thus goes only to a daemon.
- The tool kit and the MCP server cannot give a placement, so their
sandbox_creategoes to a cluster by the GPUs only. - The TypeScript SDK has
placementonly forSandbox.create.
Not proven
Section titled “Not proven”- The control service ran with daemons on the test engine, and with the API services of two kind clusters, all on one computer. See internals/control.md for the checks.
- No run used a real GPU. The GPUs were stand-ins.
- No run used two machines, a real network or a cloud cluster.
- No run used the MCP server or the CLI through the control service. The tests used the Python SDK, the Go client and the HTTP API through it.
- A test stopped a streamed command when its client closed the connection. No test stopped the control service while a command ran.