# Run a pod in a sandboxed runtime

Add a sandboxed runtime to a NodeClass, create a RuntimeClass for it, then run
a pod with that RuntimeClass. The pod runs on its own kernel, not the node's.
A privileged container in the pod gets none of the node's devices.

| Runtime  | Handler     | The pod runs on                                     | The machine needs                |
| -------- | ----------- | --------------------------------------------------- | -------------------------------- |
| `gvisor` | `runsc`     | gVisor's kernel                                     | Nothing more                     |
| `kata`   | `kata-qemu` | A Linux kernel, in a QEMU VM of its own             | `/dev/kvm`, which Kite sets up   |
| `kata`   | `kata-clh`  | A Linux kernel, in a Cloud Hypervisor VM of its own | `/dev/kvm`, and an amd64 machine |

On amd64, a node with the `kata` runtime has both Kata handlers. On arm64, it
has only `kata-qemu`. You choose the hypervisor for each pod with its
RuntimeClass.

To keep warm sandboxes for agents or builds, give this RuntimeClass to a
[sandbox pool](/docs/sandbox-pools/).

## What you need

- `kubectl` signed in to the cluster with permission to edit a NodeClass and
  create a RuntimeClass.
- A node pool. See [Set up a node pool](/docs/set-up-node-pools/). For a host you
  joined yourself, see
  [Add a runtime to a host you joined](#add-a-runtime-to-a-host-you-joined).
- Outbound access from the pool's machines to the sites that the runtimes
  download from at boot: `storage.googleapis.com` for gVisor, and `github.com`
  for Kata. The Kata release is about 1 GB. A machine whose image already
  includes gVisor does not download it.

## Give a node pool a runtime

Add `runtimes` to the pool's NodeClass:

```yaml
apiVersion: kiteplane.dev/v1alpha1
kind: NodeClass
metadata:
  name: gce
spec:
  runtimes: [gvisor]
  machineTypes: [e2-standard-4]
  gcp:
    project: my-project
    zones: [us-central1-a]
```

From then on, each machine that a pool creates from the class installs the
runtime, with the handler in the table above. Its node has the label
`runtime.kiteplane.dev/<runtime>: "true"`, such as
`runtime.kiteplane.dev/gvisor: "true"`. A node that the pool created from the
class before the change has drifted, so the pool replaces it with one that has
the runtime, as fast as its disruption budgets allow. See
[Class changes](/docs/node-pools/#class-changes).

When a class lists `kata`, Kite gives each machine `/dev/kvm`, which the pods'
VMs need:

- On GCE, Kite turns on nested virtualization. Google offers it on `n4d` and on
  Intel machine series other than `e2` and the memory-optimized series, such as
  `c3-standard-8`. Google refuses to create any other machine type with nested
  virtualization turned on.
- On EC2, Kite turns on nested virtualization for an instance type that offers
  it, such as `m8i.2xlarge`. It launches a metal type as it is, because a metal
  machine has `/dev/kvm` already. It refuses to launch any other type for the
  class.

A machine that still has no `/dev/kvm`, such as a host you joined, stops at
boot and does not join the cluster. Its boot log says
`this machine has no /dev/kvm`.

A class without `runtimes` installs neither runtime, and its machines do not
download either one.

## Create the RuntimeClass

For gVisor:

```yaml
apiVersion: node.k8s.io/v1
kind: RuntimeClass
metadata:
  name: gvisor
handler: runsc
scheduling:
  nodeSelector:
    runtime.kiteplane.dev/gvisor: "true"
```

For Kata under QEMU, set `overhead` to the memory and CPU that each pod's VM
uses on top of its containers:

```yaml
apiVersion: node.k8s.io/v1
kind: RuntimeClass
metadata:
  name: kata
handler: kata-qemu
overhead:
  podFixed:
    cpu: 250m
    memory: 350Mi
scheduling:
  nodeSelector:
    runtime.kiteplane.dev/kata: "true"
```

The node adds the overhead to the pod's limits. 350Mi covers a pod that limits
its memory to 16 GiB or less, in a VM of 8 vCPUs or fewer. Above that, add:

| For each                    | Add   |
| --------------------------- | ----- |
| GiB of memory limit over 16 | 6 MiB |
| vCPU over 8                 | 2 MiB |

The memory overhead covers room for the guest's kernel and Kata's agent (96
MiB, and 2 MiB for each of the VM's vCPUs), the VMM, virtiofsd and the shim,
and the page tables that map the VM's memory, about 6 MiB per GiB of the pod's
memory limit.

For Kata under Cloud Hypervisor, on amd64 nodes, with the same overhead:

```yaml
apiVersion: node.k8s.io/v1
kind: RuntimeClass
metadata:
  name: kata-clh
handler: kata-clh
overhead:
  podFixed:
    cpu: 250m
    memory: 350Mi
scheduling:
  nodeSelector:
    runtime.kiteplane.dev/kata: "true"
    kubernetes.io/arch: amd64
```

```sh
kubectl apply -f runtimeclass.yaml
```

The `nodeSelector` places each pod that uses the RuntimeClass on a node that
has the runtime. When no such node has enough free capacity, the pool creates
one.

## Run a pod in the sandboxed runtime

Set the RuntimeClass in the pod's `runtimeClassName`:

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: sandboxed
spec:
  runtimeClassName: gvisor
  restartPolicy: Never
  containers:
    - name: work
      image: busybox:1.37
      command: ["sh", "-c", "uname -r; dmesg | head -1"]
```

```sh
kubectl apply -f pod.yaml
kubectl logs sandboxed
```

The pod prints gVisor's kernel, not the node's:

```
4.19.0-gvisor
[   0.000000] Starting gVisor...
```

Under `runtimeClassName: kata` or `kata-clh`, it prints the VM's kernel, such
as `6.18.35`.

## Add a runtime to a host you joined

Add the host with `--runtime`:

```sh
kite node add --runtime gvisor root@node-1
```

`kite node add` installs the runtime with the rest of the node's software, and
gives the node the label `runtime.kiteplane.dev/<runtime>=true` once it
registers. `--runtime kata` requires a host with `/dev/kvm`.

For a host that is already a node, run the prepare script on the host with the
runtime in its URL, then label the node:

```sh
curl -fsSL 'https://kite.example.com/prepare?runtime=gvisor' | sudo bash
kubectl label node node-1 runtime.kiteplane.dev/gvisor=true
```

The script installs the runtime and skips anything that the host already has.
It restarts containerd if it changes containerd's configuration.

:::note
The script also changes the host's transparent huge page settings,
`shmem_enabled` and, for gVisor, `defrag`, under
`/sys/kernel/mm/transparent_hugepage`, so a sandbox's memory uses 2 MiB pages.
It keeps a value that you set. For Kata, it installs Kite's builds of:

- Cloud Hypervisor, on amd64, so a process in a `kata-clh` VM starts faster on
  a host that is itself a VM.
- Kata's guest kernel, with transparent huge pages, and with WireGuard so a pod
  can make a WireGuard link.
- Kata's shim, so containers that mount an emptyDir read-only and read-write
  share its disk.
  :::

## Limits

The cluster refuses a pod whose RuntimeClass does not exist, so the pod never
runs under the node's default runtime instead. A pod waits in `Pending` while
no node has its runtime and no pool's class lists it.

### gVisor

- A pod under gVisor cannot set `hostNetwork`, because gVisor gives such a pod
  no network interface.

### Kata

| Limit                          | `kata-qemu`                                                             | `kata-clh`                                                                                              |
| ------------------------------ | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- |
| Node architecture              | amd64 and arm64                                                         | amd64 only                                                                                              |
| Memory the guest frees         | The VM keeps the most memory that its guest has used until the pod ends | Goes back to the node within seconds, on a node whose `shmem_enabled` gives the VM's memory 2 MiB pages |
| Space freed on a disk emptyDir | Counts as used on the node until the pod ends                           | The node gets it back                                                                                   |

Under either handler:

- The VM is the pod's memory limit, or 2 GiB without one, plus the room in the
  [overhead](#create-the-runtimeclass). The guest's kernel also takes memory
  that grows with the VM's memory, and that memory comes out of the limit: a
  pod with a 1536 MiB limit has about 1490 to 1515 MiB available.
- A pod with a CPU limit gets that limit plus 0.2 vCPU, rounded up. Without a
  CPU limit, its VM has a vCPU for each CPU that the pod requests, rounded up,
  at least 8 and at most every CPU on the node. The pod shares those CPUs with
  other pods in proportion to its CPU request, as it would under runc.
- A pod starts once its VM boots.

| A Kata container's image        | On a node whose kernel has `erofs`                      | On any other node                                                         |
| ------------------------------- | ------------------------------------------------------- | ------------------------------------------------------------------------- |
| The image                       | A set of read-only EROFS disks in the VM                | Read from the node through virtiofs, which is slower for many small files |
| The container's writes          | A 10 GiB ext4 disk in the VM                            | —                                                                         |
| containerd                      | Kite runs 2.3.7 if the node's containerd is older       | —                                                                         |
| The first container of an image | Starts once containerd has converted the image's layers | —                                                                         |

| emptyDir               | Under Kata                                                                                                                                                                                                                                                                                                                       |
| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `medium: Memory`       | A tmpfs inside the pod's VM                                                                                                                                                                                                                                                                                                      |
| Any other              | A separate disk in the VM, which every container that mounts it shares. It is as large as the `sizeLimit`, or the node's filesystem without one. The VM writes it without the node's page cache, so the writes do not count against the pod's memory. A pod that fills it gets `No space left on device` rather than an eviction |
| Mounted with `subPath` | Stays on the node, and the VM reaches it through virtiofs. Every mount shows the same files, but a program started from it takes longer to start than one started from a disk. To give it a disk, mount it without `subPath` in every container                                                                                  |

## Next

- [Sandbox pools](/docs/sandbox-pools/): keep warm capacity under this
  RuntimeClass, for Pods and sandboxes that start without waiting for a machine.
