> ## Documentation index
> Fetch the complete documentation index at: https://k7d.org/llms.txt
> Ask it with GET https://k7d.org/ask?q=<keywords or question>. Short keywords rank best.
> Use these to discover all available pages before exploring further.

# Architecture

> How the CLI, API, K3s, Kata, k7d, and the four VM backends fit together

Katakate is a thin orchestration layer over a curated stack of battle-tested infrastructure. This page explains what the moving parts are and how they talk to each other.

## High-level diagram

```mermaid
flowchart TB
    subgraph Client["Client (your machine)"]
        direction LR
        CLI["k7 CLI"]
        SDK["Python SDK<br/>k7-sdk"]
        Curl["curl / your<br/>HTTP client"]
    end

    subgraph Node["K7 node — Ubuntu + K3s"]
        direction TB

        subgraph ControlPlane["K3s control plane"]
            API["k7-api ×2<br/>FastAPI + Caddy TLS sidecar<br/>kube-system · NodePort 31007 (HTTPS)<br/>roles · tenants · OIDC · metering leader"]
            K3sAPI["K3s API server"]
            Sched["scheduler<br/>(nodeSelector by backend)"]
            API -- "kubernetes_asyncio" --> K3sAPI
            K3sAPI --- Sched
        end

        subgraph DataPlane["Data plane<br/>per node"]
            direction LR
            Cilium["Cilium CNI<br/>NetworkPolicy +<br/>CiliumNetworkPolicy +<br/>cluster-wide platform deny"]
            Containerd["K3s containerd<br/>+ runtime shims<br/>(kata / kata-qemu / k7 / k7-fc)"]
            Agent["k7-agent<br/>DaemonSet pod, non-privileged<br/>(k7d VM ops · storage stats · usage sampler)"]
            K7dD["k7d daemon<br/>/run/k7d/k7d.sock"]
        end

        subgraph Sandbox["Sandbox VM"]
            VMM["Firecracker (+ jailer),<br/>QEMU, k7d native VMM,<br/>or k7d-driven Firecracker"]
            UserCnt["user container"]
            VMM --- UserCnt
        end

        Storage[("Storage backends<br/>LVM thin-pool · Longhorn PVC · k7d disk pool<br/>+ optional Docker graph disk")]

        K3sAPI -- "schedules pod" --> Containerd
        Cilium -. "wires netns" .- Containerd
        Containerd -- "launches" --> VMM
        API -. "k7d pause/resume/fork<br/>on remote nodes" .-> Agent
        Agent -. "proxies to" .- K7dD
        K7dD -- "owns k7d VMs" --> VMM
        VMM -. "rootfs" .- Storage
    end

    CLI -- "local: K7Core direct<br/>remote: HTTPS + API key or OIDC JWT" --> API
    SDK -- "HTTPS · API key / token_provider" --> API
    Curl -- "HTTPS · X-API-Key / Bearer" --> API

    classDef client fill:#0f1a2a,stroke:#3b82f6,color:#fff;
    classDef ctrl fill:#1a1a1a,stroke:#888,color:#fff;
    classDef data fill:#0b0b0b,stroke:#ef672b,color:#fff;
    classDef vm fill:#2a1a05,stroke:#ef672b,color:#fff;
    classDef store fill:#0b1a0b,stroke:#22c55e,color:#fff;
    class Client,CLI,SDK,Curl client;
    class ControlPlane,API,K3sAPI,Sched ctrl;
    class DataPlane,Cilium,Containerd,Agent,K7dD data;
    class Sandbox,VMM,UserCnt vm;
    class Storage store;
```

## Components

### CLI — `k7`

A Typer-based CLI distributed as a Debian package (`apt install k7`). Sandbox-management commands (`create`, `list`, `fork`, …) talk to the **K7 API** by default, with an API key or an OIDC login (`k7 login`). Node-local commands (`install`, `shell`, `top`, `tenant`, `nodes`, and the key commands unless `--api` is passed) still run on the cluster with kubectl. Hidden `--core` bypasses the API and calls `K7Core` in-process. Interactive `k7 shell` shells out to `kubectl exec`.

### API — `k7-api`

A FastAPI service that exposes a REST surface for sandbox lifecycle, exec, snapshots, identity (keys, role bindings), tenants and usage. The API runs as a **K3s `Deployment`** in `kube-system` — **2 replicas** spread across nodes with a PodDisruptionBudget, uid 1000, all capabilities dropped — behind a `NodePort` Service (31007), with an in-cluster `ServiceAccount` and a scoped `ClusterRole`. It is stateless: API keys live in the `kube-system/k7-api-keys` Secret (hashes only, snapshot refreshed every 5 s), role bindings in ConfigMap `k7-role-bindings`, tenants as `Tenant` custom resources, and OIDC tokens are validated against the issuer's JWKS with no sessions. The replica holding the `k7-tenant-status` / `k7-metering-leader` Leases reconciles `Tenant.status.usage` and samples storage usage. The image is `ghcr.io/katakate/k7-api` deployed **by digest** from the release's `images.lock.yaml` (cosign-signed); `--api-image local` builds it from a checkout instead.

The pod carries a **Caddy TLS sidecar**: FastAPI listens on `:8000` inside the pod, Caddy terminates HTTPS on the NodePort with a cluster CA the installer writes to `/etc/k7/tls/ca.crt` (or a Let's Encrypt / operator-supplied certificate; the Let's Encrypt overlay pins back to one replica on the first master). On Cilium clusters `k7 install --api-allow-cidr` adds a `CiliumNetworkPolicy` (`k7-api-ingress`) restricting who can reach the port. See [API transport](/k7/concepts/security-model#api-transport-tls). Temporarily stop it with `k7 api disable` (scale to 0) and bring it back with `k7 api enable`.

The entire `K7Core` is async (`kubernetes_asyncio` + `httpx`), so the API can serve concurrent requests without thread-pool workarounds. Both k7-api and k7-agent serve Prometheus metrics on `:9464` and log JSON lines with a request id that follows each request from the API into the agent ([Observability](/k7/guides/observability-metering)).

### Per-node agent — `k7-agent`

A **DaemonSet** in `kube-system` (same image as `k7-api`, running `k7.api.agent:app`) that exposes node-local operations the central API can't do remotely: k7d VM ops (`pause` / `resume` / `fork` / `lookup`, which need the node's `/run/k7d/k7d.sock` and containerd socket), storage-pool utilization (`GET /agent/v1/storage`), and the **usage sampler** that meters every sandbox pod on the node into a local spool. The API always goes through the agent for k7d ops (`K7_K7D_REMOTE_ONLY=1`), authenticated with a shared token provisioned by the installer (`/etc/k7/agent_token`) and locked down by a CiliumNetworkPolicy so only the k7-api pod can reach the agents. If no agent is Ready on the target node, the operation fails loudly — never a silent no-op.

The agent is **not privileged**: uid 0 plus the `k7d` group (gid 770 on every node, so the DaemonSet carries one static gid), all capabilities dropped, read-only root filesystem, `RuntimeDefault` seccomp, no ServiceAccount token. Its only host mounts are the containerd socket directory, `/run/k7d`, `/var/lib/k7d` (ro), `/run/k7` (ro) and the usage spool.

### Python SDK — `k7-sdk`

A small `requests`/`httpx` wrapper published to PyPI as **`k7-sdk`** (`from k7_sdk import Client`). Both `Client` (sync) and `AsyncClient` (async, `pip install "k7-sdk[async]"`) speak the same JSON envelope as the API, authenticate with an `api_key` or a `token_provider` callable (OIDC), and cover keys, tenants, role bindings, node dedication, usage and remote install as well as the sandbox lifecycle.

### K3s

The CNCF-certified Kubernetes distribution we install on every node (pinned `v1.36.5+k3s1`; `--k3s-version` overrides before the first install). K3s is single-binary, supports embedded etcd for HA, and ships its own containerd. Katakate **does not touch your existing Docker or containerd** — K3s installs its own runtime so installation is non-disruptive, and since 0.5.0 the installer needs no Docker at all.

Already running Kubernetes? The **Helm chart** (`deploy/helm/k7`) installs k7-api, k7-agent, the k7-node-installer, RuntimeClasses, snapshot GC and the Cilium policies onto RKE2, kubeadm or EKS, and never installs Kubernetes, the CNI or the CSI driver. Node preparation on those clusters is done by the `k7-node-installer` image (the playbook's node-prep role, `k7 install --node-prep-only`). See [Multi-node clusters](/k7/guides/multi-node#helm-chart-for-existing-clusters).

### Tenants

A `Tenant` custom resource (`tenants.k7.katakate.org/v1alpha1`, cluster-scoped) groups namespaces and carries dedicated nodes, a default sandbox size, quotas and per-VM limits. k7-api resolves the namespace's tenant on every request, enforces quotas under a per-tenant Lease, and stamps every object it creates with the tenant and the creating key id. The ValidatingAdmissionPolicy `k7-runtime-annotations` makes sure only k7-api can set the `k7d.katakate.org/*` and `io.katacontainers.*` pod annotations the runtimes act on. See [Tenants, roles and OIDC](/k7/guides/tenants-and-roles).

### Cilium

Cilium is the default CNI plugin. It enables **FQDN-based egress** (e.g. allow `api.openai.com` instead of a CIDR) via `CiliumNetworkPolicy`. Cilium runs with `kubeProxyReplacement=true` for a leaner stack. Pass `--cni flannel` at install time to fall back to K3s' default CNI (CIDR-only egress).

### Kata Containers

Kata is the OCI runtime that turns each pod into a lightweight VM. We install both runtime classes:

- `kata` — Firecracker microVMs (used by `kata-firecracker-devmapper`)
- `kata-qemu` — QEMU-based VMs (used by `kata-qemu-longhorn`)

Kata guest seccomp is enforced (`disable_guest_seccomp = false`) on both classes.

The other two runtime classes are not Kata. `k7` and `k7-fc` are both served by the [k7d](/k7d/index) containerd shim (`containerd-shim-k7-v1`) talking to the node-local `k7d` daemon; `k7-fc` differs only by a per-runtime `ConfigPath` (`/etc/k7d/shim-k7-fc.toml`, `backend = "firecracker"`) that tells k7d to drive stock Firecracker under the stock jailer instead of its own VMM. See [k7d backends](/k7d/concepts/backends).

### Backends

A **backend** is a tuple of (runtime class, storage driver). There are four:

| Backend | Alias | Runtime | Storage | Best for |
|---------|-------|---------|---------|----------|
| `kata-firecracker-devmapper` | `kfd` | Kata + Firecracker + jailer (`kata`) | LVM thin-pool snapshotter | Lightest footprint, fastest cold boot, ephemeral workloads |
| `kata-qemu-longhorn` | `kql` | Kata + QEMU | Longhorn CSI (replicated PVCs) | Persistent volumes, disk-level snapshot/pause/resume/fork |
| `k7d` | — | [k7d Rust VMM](/k7d/index) (`runtimeClassName: k7`) | erofs on virtio-blk + host-local state | ~5 ms warm fork, snapshot trees, RL / agent tree search |
| `k7d-fc` | `k7-fc` | k7d daemon driving stock Firecracker + jailer (`runtimeClassName: k7-fc`) | erofs on virtio-blk + host-local state | Firecracker isolation (jailer, seccomp) with k7d fork/tree/pool semantics; multi-tenant `--docker` |

A node can support **one or more** backends; on a multi-node cluster, different nodes can specialize. See [Backends](/k7/concepts/backends) for a deeper comparison.

### Storage — Longhorn (kata-qemu-longhorn only)

[Longhorn](https://longhorn.io/) provides cluster-wide replicated block storage via a Kubernetes CSI driver. Each `kata-qemu-longhorn` sandbox gets its own root PVC (`<sandbox>-root-lh`), backed by `numberOfReplicas` replicas spread across nodes. The default StorageClass uses `WaitForFirstConsumer` binding and `dataLocality: best-effort` so the volume is created on (and one replica stays on) the node where the pod is scheduled. VolumeSnapshots power `pause`/`resume`/`fork`.

### Storage — devmapper thin-pool (kata-firecracker-devmapper only)

A raw disk is provisioned as an LVM thin-pool. Containerd's devmapper snapshotter creates per-sandbox logical volumes from this pool, giving each microVM its own block device while sharing the underlying physical storage efficiently. `--docker` sandboxes on this backend get an ephemeral graph LV through the `k7-docker-lvm` StorageClass (OpenEBS LVM LocalPV).

### Storage — Docker graph disks (`--docker`)

A sandbox created with `--docker` gets a dedicated block device for Docker's `overlay2` graph: a Longhorn PVC `<name>-docker-lh` on `kql`, an LVM LocalPV on `kfd`, or a per-VM scratch disk on `k7d` / `k7d-fc` that is reflinked on fork. See [Docker in a sandbox](/k7/guides/docker).

## Request lifecycle: `k7 create`

1. **Client → API**: `POST /api/v1/sandboxes` with a `SandboxConfig` JSON body. The API authenticates the API key (SHA-256 hash lookup in the Secret snapshot) or OIDC JWT, resolves the role, checks that the route allows it and that the namespace belongs to the caller's tenant, applies the key's node scope, checks the image registry host against the public allowlist (SSRF guard), fills `limits` from the tenant's `defaultSize`, refuses limits the backend cannot enforce, takes the tenant's quota Lease and checks the quota, then forwards to `K7Core.create_sandbox`. One audit line is written whatever the outcome.
2. **K7Core**: builds a Kubernetes `Deployment` (1 replica, labelled with the tenant, key id and a fresh `sandbox-uid`), an optional `Secret` from `env_file`, an optional `PVC` (`<name>-root-lh`) on `kata-qemu-longhorn`, an optional Docker graph disk, an ingress `NetworkPolicy` (deny-all, or opened for `ingress_ports` / `ingress_from`), an optional NodePort Service for `expose_ports`, and either a CIDR `NetworkPolicy` or a `CiliumNetworkPolicy` for egress (whose DNS rule carries the allowlist's own matchers). The always-on `CiliumClusterwideNetworkPolicy` `k7-sandbox-platform-deny` (installed once per cluster) keeps every sandbox off nodes, the apiserver and platform namespaces regardless of these per-sandbox objects.
3. **K3s scheduler** picks a node based on `nodeSelector` (`k7.katakate.org/backend-<backend>=true`, set by the installer per host), the tenant's dedicated nodes (selector + toleration for the `k7.katakate.org/tenant` taint), and an explicit `node_name` pin if any.
4. **Kata or k7d** (via the appropriate containerd shim) launches a VM, mounts the rootfs (devmapper or Longhorn PVC), and starts the user container inside it.
5. **`before_script`** runs via `kubectl exec` while egress is still open. Once it completes, the egress NetworkPolicy is applied and the pod is marked Ready.
6. The API returns `201 Created` with a `Location` header.

## Source code map

```
src/
├── k7/
│   ├── api/        FastAPI service (Dockerfile + manifests deploy as a K3s Deployment)
│   │   ├── main.py          routes
│   │   ├── authz.py         roles, PERMISSIONS table, Principal
│   │   ├── oidc.py          JWT validation, JWKS cache, role bindings
│   │   ├── keystore.py      Secret-backed API key store (5 s snapshot)
│   │   ├── tenants.py       tenant admission on requests
│   │   ├── remote_install.py  POST /api/v1/install → k7-node-installer pool
│   │   ├── agent.py         k7-agent app (VM ops, storage, usage sampler)
│   │   └── snapshot_gc.py   the k7-snapshot-gc CronJob
│   ├── cli/        Typer CLI (k7.py, _oidc.py for login), packaged as the k7 .deb
│   ├── core/       K7Core: async sandbox lifecycle (Kubernetes + Longhorn + Cilium)
│   │   ├── core.py      orchestration
│   │   ├── models.py    SandboxConfig / SandboxInfo / ExecResult dataclasses
│   │   ├── tenancy.py   Tenant CR, quotas under a Lease, node dedication
│   │   ├── docker.py    --docker: graph disks, docker-vehicle (Kata) / k7d annotations
│   │   └── sidecar.py   legacy sidecar registry (kept for the deprecated --sidecar alias)
│   ├── metering/   usage sampler, spool, sinks (Prometheus / http / stdout), showback
│   └── deploy/     Ansible playbook, roles, manifests, Helm chart
│       ├── k7-install-node.yaml  the playbook
│       ├── roles/               node prep shared with the k7-node-installer
│       ├── manifests/           k7-api (Deployment, PDB, agent DaemonSet, Tenant CRD), k7-runtime-annotations admission policy
│       ├── helm/k7/             chart for existing clusters (RKE2, kubeadm, EKS)
│       ├── observability/       Grafana dashboards, alert rules, runbooks
│       ├── k7_api_tls.py        Caddy TLS sidecar rendering for k7-api
│       ├── k7_api_keys.py       key minting, roles, migration
│       └── k7d-fc/              install-firecracker.sh (pinned Firecracker + jailer for k7d-fc)
└── k7_sdk/         Python SDK (PyPI package k7-sdk)
```
