# How it works

## Components
Figure 1: Clients call the Kubernetes API on the control plane. Each node opens one WebSocket to it, and nodes reach each other over WireGuard. (drawn at https://runkite.io/docs/how-it-works/)

The control plane serves the Kubernetes API and runs no containers. Each node
runs containerd and the node agent, which does the work of kubelet,
kube-proxy, the network add-on and CoreDNS. The node agent opens one WebSocket
to the control plane. The control plane never connects to a host, so a host
needs only outbound TCP 443. Pod traffic between nodes goes over WireGuard and
never passes through the control plane.

The control plane runs on Cloudflare or on your own servers. Both work the
same way, and the two sections below say what differs.

## Commit log

The control plane keeps the cluster in a commit log, and a single writer
appends to it. Each committed write gets the next revision, which clients see
as `resourceVersion`. The commit log does the job that etcd does in upstream
Kubernetes.

- Every write goes through the writer, which checks it against the log before
  it appends it. A write that names the `resourceVersion` it read fails with
  409 Conflict when the object has changed since.
- The scheduler and the controllers work from a copy of the log, so they do not
  hold up the writer. Each batch of their writes carries the revisions it read,
  and the writer refuses the batch when one of them has changed.
- Nodes connect through relays, which hold the nodes' WebSockets for the
  writer.

## How nodes connect
  <NodeProtocol />

After the first, an assignment carries only what changed since the one the node
last applied. Logs, exec, port-forwarding and webhook calls use the same
WebSocket. A log or exec stream moves only as fast as the client reads it.

## Ports and connections
(a table of the part's ports, drawn at https://runkite.io/docs/how-it-works/)

## Serverless

<div class="only-v1">
Figure 2: On Cloudflare: a Worker in front of the cluster's Durable Object, which is the writer, and the replica objects that relay the nodes. (drawn at https://runkite.io/docs/how-it-works/)
</div>

- A Worker receives every API request and forwards it to the cluster's Durable
  Object. That object is the writer, and it keeps the log in its own SQLite.
- Each node connects to one of the relays, Durable Objects called replica
  objects, which Kite chooses from the node's name. The relay forwards the
  node's messages to the cluster's object, so the nodes stay connected when
  that object restarts. Cloudflare restarts the object when it reaches its
  memory limit, and closes the object's own connections when a burst of new
  pods overloads it.
- A relay sends each of a node's reports to the object as a request, and
  Cloudflare bills a message on a node's own connection at a twentieth of a
  request. So an idle cluster with no relays costs less.
  [Keep nodes connected through a restart](/docs/configure/#keep-nodes-connected-through-a-restart)
  sets the number of relays.

## Self-hosted

<div class="only-v2">
Figure 3: On your own servers: a Raft group of 3, where the leader is the writer and the followers relay the nodes. (drawn at https://runkite.io/docs/how-it-works/)
</div>

- `kite-control` is one process under systemd, with the scheduler and the
  controllers on a second thread. It keeps the log in SQLite on the server's
  disk.
- One server has no failover. For failover, add a standby that keeps the log
  in a Postgres database or copies it to an S3 or R2 bucket, and takes over
  when the server stops. See
  [Add a second server](/docs/deploy-servers/#add-a-second-server).
- Or run a Raft group of 3 or 5 servers, which elect the server that writes.
  The followers are the relays. `kite deploy` does not set up a Raft group.

(a table of the part's ports, drawn at https://runkite.io/docs/how-it-works/)

## Memory

`KITE_MEMORY_MB` sets the memory that the control plane may use. Each part
below uses at most its share of it at a time:

| Part                                                                               | Share           |
| ---------------------------------------------------------------------------------- | --------------- |
| The cache of node state                                                            | A sixteenth     |
| Sending each change to every node                                                  | A sixteenth     |
| Reading the pods of the ReplicaSets and Deployments that the controllers reconcile | A sixteenth     |
| The cache of recently read pods                                                    | A thirty-second |

In a large cluster, a part that needs more than its share works in rounds:

- The control plane sends a change that is bigger than its share in several
  rounds, so the last nodes get it a moment after the first ones.
- The controllers reconcile the last Deployments a moment after the first ones.

More memory lets each of these finish in one step.
[Size the cluster for its memory](/docs/configure/#size-the-cluster-for-its-memory)
sets it.

On your own servers, the scheduler and the controllers keep their own caches
with the same limits, so the control plane can use up to twice
`KITE_MEMORY_MB`.

## Limits

- **Each cluster has one writer.** Every write goes through it.
- **The control plane runs no containers.** Your workloads run on nodes.

## Cluster address

Your kubeconfig contains the address as its server URL, such as
`https://kite.example.com`. Every node connects to the control plane at this
address too.

:::caution
Choose the address before you join nodes. If it changes, everyone needs a new
kubeconfig and every node must rejoin.
:::

To set the address, see [Use your own hostname](/docs/configure/#use-your-own-hostname).

## Namespaces or a second cluster

To separate teams in one cluster, use namespaces, RBAC, resource quotas and
[NetworkPolicy](/docs/networking/#networkpolicy). Deploy a second cluster
when workloads need their own API, credentials or nodes.

Two clusters in one Cloudflare account or cloud project need different
`KITE_CLUSTER` names. `kite deploy` gives a new cluster the name of its
deployment, so two deployments get different names. See
[Share a cloud project between clusters](/docs/configure/#share-a-cloud-project-between-clusters).

## Responsibilities

| Area             | Kite provides                         | You provide                                      |
| ---------------- | ------------------------------------- | ------------------------------------------------ |
| Control plane    | Kubernetes API, RBAC, scheduling      | A Cloudflare account, or Linux servers           |
| Nodes            | The node agent                        | Linux hosts with systemd, apt and outbound HTTPS |
| Workload network | WireGuard overlay, DNS, Service rules | UDP between nodes, ranges that do not overlap    |
| Storage          | Volume binding, CSI node operations   | CSI drivers, StorageClasses, storage credentials |

You also own each host's operating system, firewall and capacity.
[Control plane costs](/docs/cost/) lists what the control plane costs on Cloudflare.

[Scale and performance](/docs/scale/) has the cluster's results against its
targets, and [Testing](/docs/testing/) says how Kite is tested and what happens when
a part fails.
