# Move a cluster

| From                   | To                     | Command                                                                                     |
| ---------------------- | ---------------------- | ------------------------------------------------------------------------------------------- |
| A Worker               | Another Worker         | `kite deploy move --name <from> --to <to>`                                                  |
| A Worker               | A Cloudflare Container | `kite deploy move --name <from> --to <to> --container`                                      |
| A Cloudflare Container | A Worker               | `kite deploy move --name <from> --to <to>`                                                  |
| A Worker               | Your own server        | `kite deploy move --name <from> --to kite-control --host root@<server> --address <address>` |
| Your own servers       | A Worker               | `kite deploy move --name <servers> --to <to>`                                               |

## Before you begin

The cluster must [keep a copy](/docs/configure/#keep-a-copy-of-the-cluster) in an S3
or R2 bucket. A cluster on your own servers keeps one when you set `KITE_COPY`,
as in [Add a second server](/docs/deploy-servers/#add-a-second-server).

## Move to another Worker

1. Put the settings that the new deployment changes, such as
   `KITE_JURISDICTION`, in its settings file.
2. Run the move:

   ```bash
   kite deploy move --name <from> --to <to>
   ```

The move deploys `<to>` with the cluster's credentials and issuer. `<to>`
starts as a standby: it restores the cluster from the copy, then reads each
change as it is written to the copy. Once `<to>` has caught up, the move:

1. Stops `<from>` cleanly.
2. Waits for `<to>` to take over the copy.
3. Routes `KITE_HOSTNAME` to `<to>`.

:::note
Writes pause while `<to>` applies the changes that were written to the copy
after its last read. Without a hostname, point nodes and clients at the
address that the move prints.
:::

### Keep the copy in another bucket

To have `<to>` keep the cluster in a different copy after the move, such as a
bucket in another account, put that copy's `KITE_COPY` and account settings in
`<to>`'s settings file. `<to>` first takes over `<from>`'s copy. Then it copies
the cluster to its own copy, and writes only there. From then on, `<from>`'s
copy stops changing, and no server takes it over.

### After the move

`<from>` stays stopped with its data, and returns 503 for every request.

| To                                                                    | Do                                                       |
| --------------------------------------------------------------------- | -------------------------------------------------------- |
| Serve from `<from>` again, before another cluster takes over the copy | Send `POST /kite/copy/resume` to it with its admin token |
| Remove `<from>`                                                       | Run `kite deploy destroy --name <from> --keep-machines`  |

Keep the machines, because the NodePools and their machines belong to `<to>`
after the move.

## Move to a Cloudflare Container

A cluster that outgrows a Worker's memory can move to `kite-control` in a
Cloudflare Container, behind a Worker of its own. The Worker keeps the address
and the nodes' connections, and forwards every request to the Container.

```bash
kite deploy move --name <from> --to <to> --container
```

The move deploys `<to>` as a Worker with a Container. `kite-control` in the
Container restores the cluster from the copy and follows it. Then the move
stops `<from>` and routes `KITE_HOSTNAME` to `<to>`, as a move to another Worker
does.

The Container loses its disk each time it starts again, so `kite-control`
restores the cluster from the copy as it starts. Until it has restored the
copy, the cluster returns 503. The Container is `standard-1`, with 4 GiB of
memory and half a vCPU. To choose another
[instance type](https://developers.cloudflare.com/containers/platform-details/limits/),
set `KITE_CONTAINER_INSTANCE_TYPE` in `<to>`'s settings file before the move.

The deploy token needs the `Workers Containers Write` permission, which
`kite deploy credentials` grants when the settings file sets
`KITE_BACKEND=container`.

### Run a standby Container

Set `KITE_CONTAINER_STANDBY=1` in `<to>`'s settings file to run a second
Container. `kite-control` in it follows the copy. When the Container that
serves stops, the standby takes the copy over, at once after a clean stop and
otherwise once the copy's lease runs out. The Worker sends each request to the
Container that serves.

To restart the `kite-control` of one Container, such as one that stops
responding, send `POST /kite/container/restart?object=control` or
`?object=standby` with the admin token. With a standby, the other Container
takes over, and the restarted one stands by.

### Grow into a Container on its own

A cluster on a Worker can move itself to a Container before its memory refuses
a pod. Set this in its settings file, and deploy:

```bash
KITE_AUTO_GROW=container
```

Each deploy then also pushes the Container's image and creates its
application, as a deploy with `KITE_BACKEND=container` does, but nothing starts
the Container. Auto-grow needs `KITE_COPY`, because the Container takes the
cluster over from its copy. The deploy token needs `Workers Containers Write`,
which `kite deploy credentials` grants when the settings file sets
`KITE_AUTO_GROW`.

Once the control plane's memory has stayed at `MemoryLow` or `MemoryFull` for
10 minutes, the cluster moves:

1. It starts the Container. `kite-control` there restores the copy and follows
   it, while the cluster keeps serving on the Worker.
2. Once `kite-control` follows the copy, the cluster stops cleanly, and
   `kite-control` takes the copy over at once. Writes pause for as long as
   that takes.
3. The Worker sends every request to the Container from then on. The address,
   the credentials and the nodes stay the same.

The cluster records the Warning Event `MovingToContainer` on the
[cluster summary](/docs/reference/resources/#read-the-cluster-summary) as it
starts, and `MovedToContainer` once the Container serves, with how long each
step took. During the move, the summary's `controlPlane.growing` says which
step it is on. If a step fails before the clean stop, such as a Container that
does not start or a copy that does not restore within 30 minutes, the cluster
keeps serving on the Worker, records `GrowFailed` with the reason, and tries
again an hour later.

The next `kite deploy` reads that the cluster moved and records
`KITE_BACKEND=container` in its settings file. Until the move, the Container
costs nothing. After it, the Container runs all the time and is billed at
[Cloudflare's Containers prices](https://developers.cloudflare.com/containers/pricing/).

To move the cluster back to its Worker, once it has shrunk:

```bash
kite deploy move --back --name <name>
```

1. The Worker empties its own copy of the cluster, restores the copy that
   `kite-control` writes and follows it, while the Container keeps serving.
2. It checks that the cluster's store holds at most half of what it held when
   the cluster grew, so it fits on the Worker with room to spare.
3. It stops the Container cleanly and takes the copy over. Writes pause for as
   long as that takes, then the Worker serves every request again.

If the cluster is still too large, or the Worker does not follow the copy
within 30 minutes, the cluster stays in its Container and the command says why.
If the Worker does not serve within 5 minutes of the Container's stop, the
Container starts again and takes the copy back. Once the Worker serves, the
command removes `KITE_BACKEND=container` from the settings file. The cluster
records `MovedToWorker` on the cluster summary with how long each step took,
and auto-grow can move it to its Container again later.

To move the cluster to another Worker instead, move it without `--container`:

```bash
kite deploy move --name <to> --to <worker>
```

## Move from a Worker to your own server

```bash
kite deploy move --name <worker deployment> --to kite-control \
  --host root@kite.example.com --address https://kite.example.com
```

The move installs `kite-control` on the server with the Worker's credentials,
as a standby that reads the Worker's copy. Once the server has restored the
copy and applies new writes as they arrive, the move stops the Worker cleanly.
Then it waits for the server to take over the copy and serve the cluster. When
the move finishes, point `kite.example.com` at the server.

:::caution
A node that joined the Worker trusts only public certificate authorities, not
the cluster's own. Serve the address with a public certificate, for example
from a load balancer, or the node cannot connect.
:::

## Move from your own servers to a Worker

Pass the deployment on your servers as `--name`:

```bash
kite deploy move --name <servers> --to <to>
```

The move stops every server, and stops the one that serves the cluster last.
Then point nodes and clients at the Worker's address, which the move prints.

## Next

- [Configure a deployment](/docs/configure/): the settings of a cluster on a Worker.
- [Deploy to your own servers](/docs/deploy-servers/): run the control
  plane on a Linux server of your own.
