# Deploy to your own servers

On your own servers, the control plane runs as `kite-control`, one process
under systemd. It keeps the cluster in a SQLite file on the server's disk.
`kite deploy` sets up one server, or one server and standbys that take over
when it stops.

## What you need

| Requirement                   | Value                                                      | Note                                                                                                                                                                  |
| ----------------------------- | ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Your machine                  | Mac or Linux                                               | With `curl` and `kubectl`                                                                                                                                             |
| Server                        | Linux with systemd and glibc 2.28 or later, amd64 or arm64 | With `curl`, `gzip` and OpenSSL 3, and `root` login over SSH. Debian 12 and Ubuntu 22.04 or later include glibc and OpenSSL 3.                                        |
| DNS name                      | Such as `kite.example.com`                                 | Resolves from your machine and from your hosts. Nodes connect to the control plane on port 443.                                                                       |
| Host                          | Ubuntu 24.04 or Debian 13, amd64 or arm64                  | With `curl`, at least 512 MiB of memory, and `root` login over SSH. It becomes a node. [Manage nodes](/docs/manage-nodes/#what-you-need) lists what other hosts need. |
| Second server, if you add one | A Postgres database, or an S3 or R2 bucket                 | With an access key that can read and write the bucket                                                                                                                 |

## Install the CLI

```bash
curl -fsSL https://get.runkite.io | sh
```

If the installer tells you to, add `~/.local/bin` to your `PATH`.

## Deploy the control plane

<Steps>

1. Deploy to the server. `kite deploy` shows what it will create and asks
   before it changes anything:

   ```bash
   kite deploy --host root@kite.example.com
   ```

   It installs `kite-control` with the release's installer and starts it. Then
   it writes a kubeconfig on your machine that trusts the cluster's own
   certificate authority:

   ```text
   ==> installing release 0.2850.0 on root@kite.example.com
   installed kite-control 0.2850.0 and restarted kite-control.service
   its settings are in /etc/kite/control.env, and journalctl -u kite-control shows its log
   sudo kite-control kubeconfig prints a kubeconfig for https://kite.example.com; --server <url> sets another address
       root@kite.example.com  serves
   ==> deployed
       api     https://kite.example.com
       kubectl export KUBECONFIG=/Users/you/.kite/deployments/kite-control/kubeconfig-break-glass

     join a host:        kite node add --deployment kite-control root@<host>
     teammates sign in:  not set up, so only the admin token works. Set KITE_OIDC_ISSUER in /Users/you/.kite/deployments/kite-control/deploy.env and deploy again.
     back up:            /Users/you/.kite/deployments/kite-control/deploy.env holds the only copy of the key to the cluster's Secrets
   ```

   Keep a copy of `deploy.env` somewhere safe: without it, nobody can read the
   cluster's Secrets.

   By default, the cluster's address is the host name of the SSH target, on
   port 443. To use another address, pass `--address https://<name>:<port>`.

2. Check that the API responds:

   ```bash
   export KUBECONFIG=~/.kite/deployments/kite-control/kubeconfig-break-glass
   kubectl get nodes
   ```

   ```text
   No resources found
   ```

</Steps>

<Aside type="caution" title="Back up ~/.kite/deployments">
  `~/.kite/deployments/kite-control/deploy.env` contains the key that encrypts the cluster's
  Secrets. `ca.crt`, in the same directory, is the certificate authority that your machine and your
  nodes trust. Keep a private copy of the directory.
</Aside>

## Join a host

<Steps>

1. Add the host. `--deployment kite-control` tells `kite` to use the cluster
   that you deployed, with its admin token and certificate authority. The
   command also copies the certificate authority to the host, where the node
   agent uses it:

   ```bash
   kite node add --deployment kite-control root@<host>
   ```

   ```text
   joined wss://kite.example.com/connect as node-1
   bootstrapped node-1. Update this host with 'kite-agent update' from now on.
   kite-node-name=node-1
   waiting for node-1 to report Ready
   node-1
   ```

2. Check the node:

   ```bash
   kubectl get nodes
   ```

   ```text
   NAME     STATUS   ROLES    AGE   VERSION
   node-1   Ready    <none>   1s    v1.37.0+kite
   ```

</Steps>

## Sign people in

With an OIDC provider in `deploy.env`, `kite deploy` prints the command that
signs a person in:

```text
  teammates sign in:  kite login https://kite.example.com --ca-hash sha256:72db8560…
```

Send it to the people who use the cluster. `--ca-hash` pins the cluster's
certificate authority, so they need no file from you. See
[Sign in to a cluster](/docs/sign-in/).

## Run a workload

```bash
kubectl create deployment web --image=nginx:1.27-alpine --replicas=2
kubectl expose deployment web --port=80
kubectl rollout status deployment/web
kubectl get pods -o wide
```

## Add a second server

The second server is a standby: it serves the cluster only when the first one
stops. Both servers keep the cluster in a shared Postgres database, or in a
copy in an S3 or R2 bucket that the serving server writes and the standby
reads.

| Backend         | Settings                                  | Takeover after a crash                  | After a clean stop      | Writes lost on a crash                                 | Cost                                                            |
| --------------- | ----------------------------------------- | --------------------------------------- | ----------------------- | ------------------------------------------------------ | --------------------------------------------------------------- |
| Postgres        | `KITE_DATABASE`, `KITE_DATABASE_PASSWORD` | 15 to 20 seconds after the last renewal | Within 5 seconds        | None that the server acknowledged                      | What the database costs                                         |
| S3 or R2 bucket | `KITE_COPY` and its access key            | 30 to 40 seconds after the last renewal | Within about 10 seconds | Up to the last second, or none with `KITE_COPY_SYNC=1` | Per request: see [Control plane costs](/docs/cost/#self-hosted) |

The serving server holds a lease in the database or the bucket, and renews it
every 5 seconds in Postgres and every 10 seconds in a bucket.

<Tabs>
<TabItem label="Postgres">

```bash
KITE_DATABASE=postgres://<user>@<host>/<database>
KITE_DATABASE_PASSWORD=<password>
```

</TabItem>
<TabItem label="S3">

```bash
KITE_COPY=s3://<bucket>/kite-control
KITE_COPY_ACCESS_KEY_ID=<access key id>
KITE_COPY_SECRET_ACCESS_KEY=<secret access key>
KITE_COPY_REGION=us-east-1
```

</TabItem>
<TabItem label="R2">

```bash
KITE_COPY=s3://<bucket>/kite-control
KITE_COPY_ACCESS_KEY_ID=<access key id>
KITE_COPY_SECRET_ACCESS_KEY=<secret access key>
KITE_COPY_REGION=auto
KITE_COPY_ENDPOINT_URL=https://<account id>.r2.cloudflarestorage.com
```

</TabItem>
</Tabs>

<Steps>

1. Add the settings for your database or bucket, from the tabs above, to
   `~/.kite/deployments/kite-control/deploy.env`.

2. Deploy again:

   ```bash
   kite deploy
   ```

   With `KITE_DATABASE`, the first server moves the cluster from its disk into
   the database. The database must be empty. With `KITE_COPY`, the first server
   starts to copy the cluster to the bucket.

3. Add the second server:

   ```bash
   kite deploy --host root@kite-2.example.com
   ```

   ```text
   ==> installing release 0.2850.0 on root@kite-2.example.com
       root@kite-2.example.com  stands by
   ```

4. Point `kite.example.com` at a TCP load balancer in front of both servers,
   with TLS passed through and a health check on port 443.

</Steps>

<Aside type="note" title="Why the load balancer passes TLS through">
  Each server's certificate comes from the cluster's own certificate authority, which your
  kubeconfig and your nodes trust. Only the serving server listens on port 443, so the load balancer
  sends every connection to it. When the standby takes over, it starts to listen on port 443, and
  the load balancer sends connections to it instead.
</Aside>

<Aside type="caution">
  Postgres must keep every committed transaction through its own failover, as synchronous
  replication does. A replica promoted from asynchronous replication drops the last transactions,
  and the cluster loses the writes in them.
</Aside>

With a bucket, the serving server acknowledges a write once the write is on
its disk, and uploads it within about a second. The standby applies each
segment of writes as it is uploaded, and applies the rest before it serves.
With `KITE_COPY_SYNC=1`, the server acknowledges a write only after the write
is in the bucket.

## Upgrade

`kite deploy` upgrades every server: the standby first, and the serving server
last. It does not restart a server that already runs the release with the same
settings, so adding a server does not restart the serving one.
`kite deploy wait` shows which server is serving.

## Move a cluster from a Worker

A Worker's cluster comes to your server through the Worker's copy in a bucket.
[Move a cluster](/docs/move/#move-from-a-worker-to-your-own-server) has the steps.

## Install with curl

`kite deploy` runs the release's installer on the server over SSH. To run it on
the server yourself instead:

<Steps>

1. On the server, install the control plane. Set `KITE_ADDRESS` to the address
   that your machine and your hosts use to connect to it:

   ```bash
   curl -fsSL https://get.runkite.io/control | KITE_ADDRESS=https://kite.example.com sh
   ```

   ```text
   installed kite-control 0.2832.0 and restarted kite-control.service
   its settings are in /etc/kite/control.env, and journalctl -u kite-control shows its log
   sudo kite-control kubeconfig prints a kubeconfig for https://kite.example.com; --server <url> sets another address
   ```

   The installer writes new secrets to `/etc/kite/control.env`. `kite-control`
   listens on the port in the address, 443 by default, with a certificate for
   the host name in the address. Without `KITE_ADDRESS`, it listens on 443 with
   a certificate for the server's host name.

   <Aside type="tip" title="Change the address later">
     Edit `KITE_ADDRESS` and `KITE_CONTROL_ARGS` in `/etc/kite/control.env`, then run `sudo
     systemctl restart kite-control`.
   </Aside>

2. On your machine, save the control plane's kubeconfig and check that the API
   responds:

   ```bash
   ssh root@kite.example.com kite-control kubeconfig > kite.yaml
   kubectl --kubeconfig kite.yaml get nodes
   ```

   ```text
   No resources found
   ```

3. Join a host with the same file:

   ```bash
   kite node add --kubeconfig kite.yaml root@<host>
   ```

   ```text
   joined wss://kite.example.com/connect as node-1
   bootstrapped node-1. Update this host with 'kite-agent update' from now on.
   kite-node-name=node-1
   waiting for node-1 to report Ready
   node-1
   ```

</Steps>

`kite.yaml` contains the admin token. Keep it private, as you would
`~/.kite/deployments`.

## If something goes wrong

| What you see                                          | What to do                                                                                                        |
| ----------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| `<program> listens on port 443 on <host>`             | Stop that program on the server, or pass `--address https://<name>:<port>` with a free port. Nothing was changed. |
| `install OpenSSL 3 or Node.js to check the signature` | Install OpenSSL 3 on the server, then deploy again.                                                               |
| `kite-control needs glibc 2.28 or later`              | Use a server with a distribution that ships glibc 2.28 or later, such as Debian 12 or Ubuntu 22.04.               |
| `kite-control neither serves nor stands by`           | On the server, run `journalctl -u kite-control`.                                                                  |
| `a second host needs KITE_DATABASE or KITE_COPY`      | Add the database or the bucket to `deploy.env`, as in [Add a second server](#add-a-second-server).                |
| The node isn't `Ready`                                | On the host, run `kite-agent status`, then `kite-agent logs`.                                                     |

## Next

- [Manage nodes](/docs/manage-nodes/): add, upgrade, and remove hosts.
- [CLI reference](/docs/reference/cli/#deploy-to-your-own-hosts): every setting of a deployment on your own servers.
- [How it works](/docs/how-it-works/): how the control plane stores and copies a cluster. The
  control plane can also run as a Raft group of 3 or 5 servers, but `kite deploy` does not set one
  up.
- [Move a cluster](/docs/move/): move a cluster between a Worker and your own servers.
