Registry Mirror
Skip to content

Repository files navigation

cmlops

Infrastructure for shared ML work at CML. The current stack deploys an MLflow tracking server and serves it through a Cloudflare tunnel behind Access.

Path What it is
compose.yaml Starts MLflow and the tunnel connector
mlflow/ MLflow tracking server
tunnel/ Cloudflare tunnel connector
infra/ OpenTofu configuration for the tunnel, DNS, and Access
bin/mlflow-restore-test On-demand check of a live database snapshot
tests/ Shell tests run by just check
.env.example Example settings for Docker Compose and just

Deploying

The host needs Docker Engine with the Compose plugin 2.20 or later, just, OpenTofu, curl, and Bash 4 or later. just tofu runs on the host. just infra uses a container.

  1. cp .env.example .env and fill it in. Docker Compose and just both read it.
  2. Set up the tunnel once (below), so $CMLOPS_STATE/tunnel-token exists.
  3. just setup creates the MLflow store and the state dir as you, and checks the token file.
  4. docker compose up -d --wait. The services come back on their own after a crash or a reboot, until docker compose down.

To run MLflow before the tunnel exists, skip step 2: just setup then creates the dirs, says there is no token yet and exits 0, and docker compose up -d --wait mlflow starts MLflow alone. Run just setup and the full docker compose up -d --wait once the token is there.

Moving an existing setup

To take over a node that already runs MLflow and a tunnel from elsewhere:

  1. Set CMLOPS_MLFLOW_STORE to the existing store. Set CMLOPS_STATE to the existing tunnel state directory. Keep the existing Compose definitions for rollback.
  2. Run just tofu init -reconfigure, then review just tofu plan from the existing state. Stop if the plan shows creates, deletes, replacements, or unexpected updates. Preserve existing hostnames in services in terraform.tfvars. Set team_name if the existing variables file doesn't have it.
  3. Back up the database. Stop the old MLflow server and connector before starting the new ones.
  4. Run just setup and docker compose up -d --wait. Verify local health and access through the existing URL. If verification fails, stop the new services before restarting the old ones.

Every service binds to 127.0.0.1 only; the tunnel with Access is the only way in from outside. The service images in the compose files are pinned by tag and digest, and Dependabot bumps them; the tool images in the justfile are pinned the same way and bumped by hand.

MLflow

One tracking server for every project on the node, on 127.0.0.1:${CMLOPS_MLFLOW_PORT:-5000}. A client talks HTTP and never opens the database itself, so a project's locked MLflow version need not match the store's schema: only the server's version does.

The store is $CMLOPS_MLFLOW_STORE, mounted at /mlflow: mlflow.db and artifacts/. Back it up from the host. Copy the database through SQLite's backup API, never with cp while the server runs, which can copy a half-written file:

docker compose exec mlflow python -c "import sqlite3; sqlite3.connect('/mlflow/mlflow.db').backup(sqlite3.connect('/mlflow/mlflow.db.bak'))"

just mlflow-restore-test checks a fresh database snapshot on demand. It takes the snapshot from a read-only mount and checks SQLite integrity. A temporary MLflow server uses the same image as the deployed stack. The check compares the snapshot's run and experiment counts with the live server's. It does not read an existing backup or restore artifacts. Test recovery of the off-host database and artifacts separately.

To upgrade MLflow, follow the comment at the top of mlflow/compose.yaml.

export MLFLOW_TRACKING_URI=http://127.0.0.1:5000
curl -s http://127.0.0.1:5000/health   # OK

Leave the artifact location to the server: a new experiment gets mlflow-artifacts:/<id>, and the client uploads artifacts through the server. A local artifact_location would record a path only one checkout has. mlflow-skinny is enough on the client side.

To delete runs for good, back up mlflow.db as above, then name the runs:

docker compose exec mlflow mlflow gc --backend-store-uri sqlite:////mlflow/mlflow.db \
  --artifacts-destination /mlflow/artifacts --run-ids <ids>

Without --run-ids, gc purges every soft-deleted run in the store.

Tunnel

A Cloudflare tunnel publishes the node's web UIs behind Cloudflare Access, so a browser opens them without ssh -L. infra/ holds the tunnel, its ingress rules (ending in a 404), the DNS records, one Access application per hostname and one shared policy for the allowed emails. Access sends a one-time PIN to those addresses only; a session lasts 24 hours. Each ingress rule also makes cloudflared check the Access token for its own application, so a request without one never reaches the service. The connector in tunnel/compose.yaml only dials out, and binds only a metrics port on 127.0.0.1:20241 for its healthcheck.

just tofu runs OpenTofu with everything private in $CMLOPS_STATE (mode 700): the state, its .terraform dir and terraform.tfvars. just token writes the connector's token there.

First setup

  1. In the Cloudflare dashboard (My Profile, API Tokens, Create Custom Token), make a token scoped to the account and the zone, with: Account, Cloudflare Tunnel, Edit; Account, Access: Apps and Policies, Edit; Zone, DNS, Edit.

  2. Open the zone's Overview page: the API box in the right sidebar lists the Zone ID and the Account ID. The Zero Trust dashboard's settings show the team name (team_name), the first part of <team_name>.cloudflareaccess.com.

  3. Fill in the variables (set -a; . ./.env; set +a puts CMLOPS_STATE in the shell):

    mkdir -p -m 700 "$CMLOPS_STATE"
    cp infra/terraform.tfvars.example "$CMLOPS_STATE/terraform.tfvars"
    $EDITOR "$CMLOPS_STATE/terraform.tfvars"
  4. Apply, with the API token in the environment for this shell only, then write the connector token:

    read -rs CLOUDFLARE_API_TOKEN && export CLOUDFLARE_API_TOKEN
    just tofu init
    just tofu apply
    just token

Access sends the PIN only if the account has the One-time PIN login method (Zero Trust, Settings, Authentication).

Adding a hostname

Add subdomain = "http://127.0.0.1:<port>" to services in terraform.tfvars (the default is in infra/variables.tf), then just tofu apply. The ingress rule, the DNS record and the Access application all follow from that map, and the tests fail if a hostname would lack Access. The connector picks up the new route on its own, since the tunnel is managed from Cloudflare.

Threats

  • MLflow has no login. Access is its only gate, so every hostname gets an Access application, and tofu test checks that.

  • Keep the services bound to 127.0.0.1. Then the local network cannot reach them directly, and the only way in from outside is through Access. services refuses anything else.

  • The tunnel token lets whoever holds it run a connector for this tunnel, and so serve its hostnames. It lives only in $CMLOPS_STATE/tunnel-token (mode 600, checked by just setup) and in the state file next to it. To rotate it, replace the tunnel and its token:

    just tofu apply -replace=cloudflare_zero_trust_tunnel_cloudflared.node
    just token
    docker compose up -d --force-recreate cloudflared

    The new tunnel gets a new ID, and the DNS records follow it in the same apply.

Development

just check     # compose config, shell lint, secrets in history, hygiene, tofu fmt/validate/test and tests/
prek install   # the git hooks: file checks and linters on commit, the message on commit, `just check` on push
just hooks     # every git hook over the whole repo, as the checks workflow does

The linters and OpenTofu run in pinned containers, so just check needs Docker, and network access for the images and for tofu init to fetch the provider. The hooks need prek. A test that cannot run on the machine (no Docker, not Linux) prints SKIP and does not fail, except when CI is set, as GitHub Actions does: there a skip fails the run.

Licence

MIT. Per-file licensing is in REUSE.toml.

About

The ML stack of a single GPU node at CML: a GPU queue, model serving behind a standard chat-completions HTTP API, one MLflow tracking server, and a Cloudflare tunnel behind Access.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages