# Node pools

A node pool creates a machine in your cloud account when a pending pod does not fit on any node, and deletes machines that it no longer needs. Hosts that you join with `kite node add` do not need a pool. To connect GCP, AWS or DigitalOcean and create a pool, see [Set up a node pool](/docs/set-up-node-pools/).

## NodeClass, NodePool and NodeClaim

| Resource    | Who creates it           | What it describes                                                   |
| ----------- | ------------------------ | ------------------------------------------------------------------- |
| `NodeClass` | You                      | One provider account, location, image, and ordered machine types.   |
| `NodePool`  | You                      | The class to use, limits, labels and taints, and disruption policy. |
| `NodeClaim` | The node pool controller | One requested machine and where it is in its lifecycle.             |

All three are cluster-scoped `kiteplane.dev/v1alpha1` resources. The [Node pool reference](/docs/reference/node-pools/) lists every field.

## From pending pod to node

When a pod does not fit on any node, the node pool controller tries the pools in order: highest `spec.weight` first, then by name. It picks the first pool where:

- the pod's selectors and tolerations allow the pool's labels and taints,
- the pool's limits leave capacity for a new machine, and
- the pool's class has a machine type that fits the pod.

The controller then creates a `NodeClaim`. The provider creates the machine, and the machine joins the cluster as a node.

A `NodeClaim` whose machine joins moves through the phases `Pending`,
`Provisioning` and `Registered`. Later, consolidation or expiry ends it, as
[Consolidation and expiry](#consolidation-and-expiry) describes:

```mermaid
graph TD
  pending[NodeClaim Pending] --> prov[Provisioning] --> reg[Registered]
  reg -->|consolidation or expiry| drain[Node drained] --> gone[Machine deleted]
```

If a pool creates no node, see [If no node appears](/docs/set-up-node-pools/#if-no-node-appears).

### Capacity refusals

A capacity refusal is a request that the provider refuses for lack of capacity or quota. The pool's `status.unavailable` shows each refusal and when the next try is due, and the controller tries a refused zone again later.

| Provider or event                         | What the controller does                                                                                                                                                                            |
| ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| GCP or AWS refuses a machine              | Tries the class's other zones, then the next machine type that fits, then the next eligible pool.                                                                                                   |
| AWS Spot with `spec.aws.order: SpotPrice` | Tries the next offering, a machine type and zone together, cheapest Spot price first, then the next eligible pool. See [Spot price order](/docs/reference/node-pools/#spot-price-order).            |
| DigitalOcean refuses a droplet            | Tries the same request again, because no DigitalOcean error counts as a capacity refusal. The claim's `Launched` condition is `Unknown`, with the reason `CreateFailed` and DigitalOcean's message. |
| The provider reclaims a Spot machine      | Deletes the NodeClaim and waits 30 minutes before it tries that machine type in that zone again, twice as long after each further reclaim in a row, up to 6 hours.                                  |

If the provider reclaims a machine before it joins, the controller ends its NodeClaim within two minutes of the launch, without waiting for the registration deadline. [Capacity refusals](/docs/reference/node-pools/#capacity-refusals) in the reference lists the provider codes that count.

## Limits and placement

Every node that a pool creates has the label `kiteplane.dev/nodepool=<pool>`. Use it in a `nodeSelector` to place a pod on that pool, and add a taint to the pool to keep other pods off.

:::caution
Set `limits` on every pool. A pool with no limits creates machines without bound.
:::

To see how close a pool is to its limits, read its `status.resources`. It counts the pool's machines as the limits count them, including the ones still starting:

```bash
kubectl get nodepool workers -o jsonpath='{.spec.limits}{"\n"}{.status.resources}{"\n"}'
```

If a pool reaches its limits and runs pods of different sizes, set the scheduler to [pack pods onto fewer nodes](/docs/configure/#choose-how-pods-spread-over-nodes).

### Pods of different sizes

When a pool is at its limits, the capacity that a pod frees goes to the next pod that fits in it. A pod that needs a larger machine than the pool's remaining capacity allows waits until enough of the pool's nodes are empty. On a busy pool, that may never happen.

To reserve the pool's capacity for such a pod once it has waited for a set time, set `spec.queueing.reserveAfter`:

```yaml
spec:
  limits:
    cpu: "80"
  queueing:
    reserveAfter: 10m
```

While the pool reserves its capacity:

- It creates no machine for a newer pod, and its nodes accept no newer pod. The scheduler still places DaemonSet pods.
- It immediately deletes an empty node that is too small for the waiting pod, so that the waiting pod can use that capacity under the limits.
- `status.reservedFor` identifies the waiting pod. A newer pod's `FailedScheduling` event says `node(s) are reserved for an older pod`.

The reservation ends when the pod has a machine, or when the pod is deleted. A pool reserves nothing for a pod that fits no machine type under its limits, even when the pool is empty. With `reserveAfter: 0s`, the pool reserves its capacity as soon as such a pod starts to wait. While one pool reserves its capacity, another pool can still create machines for the newer pods.

### Pools on two clouds

One cluster can have pools on GCP and on AWS. Pods on different clouds can connect to each other only when the cluster allows public endpoints, and the provider bills that traffic as egress. See [Join nodes in different networks](/docs/configure/#join-nodes-in-different-networks). If the cluster does not allow public endpoints, use labels or taints on the pools to keep pods that talk to each other on one cloud.

### Class changes

A node that no longer matches its pool's class has drifted, and the pool replaces it. A node drifts when:

- the pool's `spec.nodeClassRef` refers to a different class from the one that created the node,
- the class no longer lists the node's machine type or location, or
- the class changed a field that applies only when a machine is created: `image`, `diskGiB`, `runtimes`, or a provider setting such as the network, the service account or Spot.

Adding a machine type or a location does not make any node drift, and neither does a new token in the class's Secret.

A class refuses a change to the account that its machines are in while a NodeClaim names the class, because the pool lists and deletes those machines through the class. The provider block itself names the account, and so do these fields in it:

| Provider     | Fields                                                                        |
| ------------ | ----------------------------------------------------------------------------- |
| GCP          | `project`, `workloadIdentityProvider`, `credentialsSecretRef` and `endpoints` |
| AWS          | `region`, `roleArn` and `endpoints`                                           |
| DigitalOcean | `credentialsSecretRef` and `endpoint`                                         |

To move a pool to another account, create a NodeClass for it and point the pool's `spec.nodeClassRef` at the new class. The pool replaces each node as drifted.

The pool drains a drifted node in one of two ways:

- When the node's pods fit on no other node, the pool first creates a machine for them from the current class, and drains the drifted node once that machine has joined as a node.
- When the pods fit on other nodes, or the pool cannot create a machine, such as when it is at its node limit, the pool drains the drifted node at once.

Your pool's [disruption budgets](#disruption-budgets) limit how many drifted nodes it replaces at once. To stop replacing drifted nodes, add a budget of `nodes: "0"` with `reasons: [Drifted]`.

The pool keeps a copy of each class that any of its machines came from, so you can delete the old class before its machines are gone. If that class reads its credential from a Secret, keep the Secret until the pool has deleted the class's machines.

## Local SSD

Kite can put containerd data, pod data, and pod logs on a machine's local NVMe SSDs instead of its boot disk:

- On GCP, set `spec.gcp.localSsdCount`.
- On AWS, list a machine type with an NVMe SSD instance store, such as `m6id.2xlarge`. Kite uses the instance store on every machine of that type. To keep data on the boot volume, list types without an instance store.

:::caution
Data on a local SSD is lost with the machine. Keep durable data elsewhere.
:::

## Consolidation and expiry

Consolidation removes nodes that the pool does not need, and expiry removes nodes after a set age. Both are opt-in. Other events remove a node too, and only some of them respect a pool's [disruption budgets](#disruption-budgets) and the [`do-not-disrupt` annotation](#keep-a-node):

| What removes a node | On by default                                                          | Budgets limit it | `do-not-disrupt` stops it |
| ------------------- | ---------------------------------------------------------------------- | ---------------- | ------------------------- |
| Consolidation       | No. Set `consolidateAfter`.                                            | Yes              | Yes                       |
| Drift               | Yes. See [Class changes](#class-changes).                              | Yes              | Yes                       |
| Expiry              | No. Set `expireAfter`.                                                 | No               | No                        |
| Unreachable node    | Yes, after `spec.disruption.unreachableTimeout`, 30 minutes by default | No               | No                        |
| Manual delete       | Not applicable                                                         | No               | No                        |

- Consolidation removes a node once it has been a candidate for `consolidateAfter`. Under the default `WhenEmpty` policy, a node with only DaemonSet pods is a candidate. Under `WhenEmptyOrUnderutilized`, an underutilized node is also a candidate when its pods fit on other nodes, and consolidation moves those pods.
- Expiry removes a node older than `expireAfter`.

When the controller drains a node, it respects PodDisruptionBudgets for up to `terminationGracePeriod`.

### Keep a node

A pod with the annotation `kiteplane.dev/do-not-disrupt` protects its node from consolidation and drift while the pod runs:

```yaml
metadata:
  annotations:
    kiteplane.dev/do-not-disrupt: "true"
```

Set it to `"true"` to keep the node until the pod ends, or to a duration such as `"30m"` to keep the node for that long after the pod starts. The pool ignores any other value. Karpenter's key, `karpenter.sh/do-not-disrupt`, works the same way, so a pod written for Karpenter keeps its node without changes. To keep one node whatever runs on it, set either key to `"true"` on the Node:

```bash
kubectl annotate node <name> kiteplane.dev/do-not-disrupt=true
```

While an annotation protects a node, the pool records a `DisruptionBlocked` event on the Node.

### Disruption budgets

A pool's `spec.disruption.budgets` limit how many of its nodes consolidation and drift remove at once. A pool without the field removes up to 10% of its nodes at once, rounded up, so at least one. Nodes that are already draining and nodes that are unreachable count against the budget. When several budgets apply, the pool uses the lowest. While a budget prevents the pool from removing a node, the pool records a `DisruptionBlocked` event.

This pool removes up to two empty nodes at a time, and none on weekdays from 09:00 to 17:00 UTC:

```yaml
spec:
  disruption:
    consolidateAfter: 10m
    budgets:
      - nodes: "2"
      - nodes: "0"
        schedule: "0 9 * * 1-5"
        duration: 8h
```

:::caution
After `terminationGracePeriod`, 5 minutes by default, the controller deletes the machine with pods still on it. Before you turn on consolidation or expiry, give critical workloads enough replicas, requests, and PodDisruptionBudgets.
:::

## Next

- [Set up a node pool](/docs/set-up-node-pools/) — connect GCP, AWS or DigitalOcean and check that a node launches.
- [Autoscaling pods](/docs/autoscaling/) — scale pods once capacity is there.
