# Nodes

A Kite node is a Linux host that runs containerd and the node agent,
`kite-agent`, in place of kubelet, kube-proxy, the network add-on and CoreDNS.
To join or remove one, see [Manage nodes](/docs/manage-nodes/).

## What the agent does

The agent runs the node's pods through containerd and reports their state to
the control plane. It sets up [pod networking, Services and DNS](/docs/networking/)
and [volumes](/docs/storage/). On the host, it manages the `cni0` bridge,
the `kitewg0` WireGuard device and the nftables rules for Services.

## Control connection

Each node keeps a WebSocket open to `wss://<cluster-address>/connect`. Pod
assignments, status, logs, exec and port-forwarding all use this connection.
The control plane never opens a connection to the host, so the host needs only
outbound TCP 443 to the control plane. On the serverless control plane, the
socket ends at a relay, which forwards the node's messages to the writer. See
[Keep nodes connected through a restart](/docs/configure/#keep-nodes-connected-through-a-restart).
In a self-hosted Raft group, it ends at a follower, which relays them to the
leader.

| When                                                                                        | Self-hosted control plane                                                              | Serverless control plane                                                                                                                                                    |
| ------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| You send to an exec's stdin, a port-forward or a sandbox's socket faster than the pod reads | Your client waits for the pod.                                                         | The stream ends with an error once the pod is 16 MiB behind.                                                                                                                |
| You read an exec's output, a log or a port-forward more slowly than the pod writes          | The pod's writes block, as under a kubelet, and the stream stays open.                 | A log read waits for you. An exec, a port-forward or a sandbox's socket keeps sending, and the control plane holds the output until you read it.                            |
| An exec, an attach or a forwarded connection has no traffic for 4 hours                     | It ends, as an exec does through the Kubernetes API server.                            | The same.                                                                                                                                                                   |
| You read none of a stream's output for 4 hours                                              | Any stream ends: an exec, an attach, a port-forward, a log read or a sandbox's socket. | A log read ends, up to a minute later. Your next read returns the output that had reached your connection, a message that says why the log stopped, and the end of the log. |

Traffic is what you send, what the pod writes, and the output you read. You
cannot change the 4 hours.

- An idle exec ends with an error, so `kubectl exec` exits non-zero.
- An idle forwarded connection closes as if the pod had closed it. The rest of
  your port-forward stays open.
- A log follow stays open for as long as you read it, however long its pod
  writes nothing. So does a sandbox's socket with no messages.

:::note[Symptoms]
A large `kubectl cp`, or a port-forwarded restore onto a slow disk, can fail on
the serverless control plane because the pod falls 16 MiB behind. On a
self-hosted control plane, a stream that ends because you read nothing closes
its connection before the end of the stream, so `kubectl logs` reports
`unexpected EOF`.
:::

## Pods calling the API

A pod reaches the API at the `kubernetes` Service, as in any cluster. The agent
on the pod's node answers on that address and forwards each request to the
control plane with the pod's own token. Its certificate comes from the
cluster's certificate authority, which every pod trusts through its
`ca.crt`.

That authority is valid for ten years, and Kite does not renew or replace it.
Kite renews each node's own certificate.

## Addresses and Ready

A Node's `InternalIP` is the gateway of its `cni0` bridge, not the host's own
address.

A node is Ready once it has set up all of these:

- its pod network and WireGuard,
- the Service rules and the NetworkPolicy rules,
- the rules that bind each pod to its own addresses,
- cluster DNS and the API address, and
- a peer map that includes the node itself. The peer map is the control
  plane's list of the cluster's nodes and how to reach each one over
  WireGuard.

If `nft` refuses one of these rules, the node is not Ready, and its Ready
condition shows the error from `nft`. Ready does not mean that every peer has
finished a WireGuard handshake.

## Capacity and allocatable

A Node's `status.capacity` is the CPU and memory that its machine has. Its
`status.allocatable` is what pods can request: the capacity minus 100m of CPU
and 256Mi of memory. The node reserves that CPU and memory for the agent,
containerd and the host's own processes. The scheduler places a pod only on a
node with enough allocatable CPU and memory left for the pod's requests.

The node itself also rejects a pod that requests more CPU or memory than its
allocatable, with `OutOfcpu` or `OutOfmemory`, as kubelet does. This check
also applies to a pod that sets `spec.nodeName` and so skips the scheduler.

To reserve other amounts, see
[Reserve CPU and memory for the host](/docs/manage-nodes/#reserve-cpu-and-memory-for-the-host).

## Disk space

The agent frees space on the disk that containerd keeps images on, and reports
when a disk runs low:

| Threshold                                                | What happens                                                                                                                       |
| -------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| The image disk is 85% full                               | The agent removes images that no container on the node uses, the one a container used longest ago first.                           |
| The image disk is 80% full                               | The agent stops removing images.                                                                                                   |
| Less than 15% of the image disk is free                  | The Node's `DiskPressure` condition is `True`, and the control plane adds the taint `node.kubernetes.io/disk-pressure:NoSchedule`. |
| Less than 10% of the disk that holds pod volumes is free | The same as for the image disk.                                                                                                    |
| Five minutes after the disk has room again               | The condition and the taint come off.                                                                                              |

The agent checks every five minutes and after each pull. It never removes:

- an image that a container runs or mounts, including a container that has
  exited
- an image of a pod or sandbox assigned to the node
- an image that containerd pins
- an image pulled in the last two minutes

While the node has the taint:

- The scheduler places a new pod on the node only if the pod tolerates the
  taint, as a DaemonSet's pods do.
- Kite evicts no pod for disk pressure, so the pods already on the node keep
  running.

These thresholds are kubelet's defaults, and you cannot change them.

:::note
A node that runs as a container, as the nodes of `kite local` do, keeps its
images on its host's disk, so its agent neither removes images nor reports
`DiskPressure`.
:::

## When a node disconnects

| After the node disconnects | What happens                                                                                                                                                                             |
| -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 0 seconds                  | The node keeps running its pods from the last assignment that it received.                                                                                                               |
| 30 seconds                 | The Node reads `NotReady`, and the control plane adds the taint `node.kubernetes.io/unreachable:NoExecute`. A node that reconnects before this keeps its pods.                           |
| 60 seconds                 | The control plane evicts each pod that sets no toleration for the taint, because such a pod gets a default toleration of 30 seconds. A Deployment then replaces the pod on another node. |
| 30 minutes, by default     | A node pool deletes the machine, after the pool's `spec.disruption.unreachableTimeout`. See [Consolidation and expiry](/docs/node-pools/#consolidation-and-expiry).                      |

While it is disconnected, the node restarts a container that exits, runs its
probes, and stops its pods if its host shuts down. A pod that you delete
meanwhile runs until the node reconnects.

When the host shuts down or reboots, the agent holds the shutdown while it
stops the node's pods:

1. The agent tells the control plane. The Node reads `NotReady` at once, with
   the reason `NodeShuttingDown`, and the scheduler places no new pod on it.
   Every pod on the node reads not ready, so its Services stop sending it
   traffic.
2. Each pod runs its preStop hooks and gets SIGTERM, as a deleted pod does.
   Pods at `system-cluster-critical` priority or above stop after the others,
   in the last 10 seconds of the shutdown grace period of 30 seconds. A pod
   gets its own grace period or its share of that time, whichever is shorter.
3. A stopped pod reads `Failed`, with the reason `Terminated` and the condition
   `DisruptionTarget`, so its controller replaces it on another node, and a
   Job's `podFailurePolicy` can ignore the failure.
4. Once no pod runs, or the grace period is over, the agent lets the shutdown
   go on. The control plane then treats the Node as disconnected at once,
   without the 30-second grace.

systemd-logind tells the agent about the shutdown. On a host without
systemd-logind, a shutdown is a disconnect like any other.
[Machine shutdown](/docs/reference/node-agent/#machine-shutdown) says how to change
the grace period.

The control plane taints the disconnected Nodes of a zone one at a time, as
upstream Kubernetes does:

| Disconnected Nodes in the zone        | Taints                                 |
| ------------------------------------- | -------------------------------------- |
| Fewer than 55%, or fewer than three   | One Node every 10 seconds              |
| 55% or more, in a zone of over 50     | One Node every 100 seconds             |
| 55% or more, in a zone of 50 or fewer | None                                   |
| Every Node in every zone              | None, and the taint comes off each one |

A zone is the Node's `topology.kubernetes.io/region` and
`topology.kubernetes.io/zone` labels. When no Node in the cluster is connected,
the fault is most likely the control plane's, so the control plane evicts no
pod.

An evicted pod shows Terminating until you delete the Node. Deleting the Node
deletes every pod left on it. A StatefulSet recreates its pod only after you
delete the Node.

## Next

- [Manage nodes](/docs/manage-nodes/): join, check, update and remove a host.
- [Networking](/docs/networking/): pod routes and firewall access.
- [Set up a node pool](/docs/set-up-node-pools/): create nodes in a cloud account.
