# Set up a node pool

Connect a Google Cloud, AWS or DigitalOcean account to your cluster. Then create a node pool, which creates a machine in that account when a pod does not fit on any node.

## What you need

- The [Kite CLI](/docs/quickstart/#install-the-cli), signed in to the cluster, and `kubectl` pointed at the same cluster.
- A network at the provider where machines can connect to the cluster's address on port 443, and to each other on UDP port 51820. Its addresses must not overlap the [pod and Service ranges](/docs/configure/#set-the-pod-and-service-networks).

## Connect a cloud account

For each provider, `kite nodepool connect` writes a setup script. The script creates what the control plane needs to sign in to your account, and applies a NodeClass. A NodeClass contains the provider settings that a pool uses to create machines.

<Tabs syncKey="provider">
<TabItem label="Google Cloud">

The control plane signs in to Google Cloud with Workload Identity Federation, so you don't create a service-account key. Sign in to the Google Cloud CLI as a principal that can create a workload identity pool, a provider, a service account, and role bindings.

<Aside type="caution">
  Use a project that runs nothing but this cluster's machines. The script grants the cluster
  `roles/compute.instanceAdmin.v1` on the whole project, because Compute Engine cannot limit that
  role to the cluster's own machines.
</Aside>

Write the setup script. It stops at the first command that fails, so choose a workload identity pool name and a provider name that the project does not use yet:

```bash

kite nodepool connect gcp \
  --project "$GCP_PROJECT" \
  --zone "$GCP_ZONE" \
  --pool "$GCP_WIP_POOL" \
  --provider-id "$GCP_WIP_PROVIDER" \
  --service-account "$GCP_NODE_SERVICE_ACCOUNT" > kite-gcp-connect.sh
```

To use another VPC, add `network` and `subnetwork` under `gcp` in the script.

Read the script, then run it with `bash kite-gcp-connect.sh`. It creates the workload identity pool, the provider, the service account and the role bindings, and applies a NodeClass named `gce`. Check the class:

```bash
kite nodepool verify "$NODECLASS"
```

It prints `ok  gce` when the class works. Otherwise it prints the step that failed and what to change.

</TabItem>
<TabItem label="AWS">

The control plane assumes an IAM role with a token that it signs itself, so you don't create an access key. Sign in to the AWS CLI as a principal that can create an OIDC identity provider, roles, an instance profile, and a role policy.

Write the setup script. Choose a role name and an instance profile name that the account does not use yet.

The role lets the control plane launch an instance, delete it and read its console only when the instance has your cluster's name as a tag. `kite nodepool connect aws` reads that name from the cluster when it writes the script. See [Share a cloud project between clusters](/docs/configure/#share-a-cloud-project-between-clusters).

```bash

kite nodepool connect aws \
  --region "$AWS_REGION" \
  --role "$AWS_ROLE" \
  --instance-profile "$AWS_INSTANCE_PROFILE" \
  --class "$NODECLASS" > kite-aws-connect.sh
```

To use your own VPC, add `subnets` and `securityGroups` under `aws` in the script. Without them, machines launch into each zone's default subnet with the VPC's default security group.

Read the script, then run it with `bash kite-aws-connect.sh`. It creates the OIDC identity provider, the roles and the instance profile, and applies a NodeClass named `ec2`. Check the class with `kite nodepool verify "$NODECLASS"`.

<Aside type="caution">
  Let only `ec2.amazonaws.com` assume the instance profile's role. Anyone who can assume that role
  can join a node that poses as one of your machines.
</Aside>

</TabItem>
<TabItem label="DigitalOcean">

Create a custom-scoped DigitalOcean API token with these scopes:

| Resource | Required scopes                                    |
| -------- | -------------------------------------------------- |
| Droplets | `droplet:create`, `droplet:read`, `droplet:delete` |
| Capacity | `sizes:read`, `regions:read`                       |
| Tags     | `tag:create`                                       |

<Aside type="caution">
  `kite nodepool verify` does not check `tag:create`. A token without it fails when the pool creates
  its first machine.
</Aside>

Write the setup script:

```bash

kite nodepool connect digitalocean \
  --region "$DO_REGION" \
  --secret "$DO_SECRET" \
  --class "$NODECLASS" > kite-digitalocean-connect.sh
```

To use another VPC, add `vpc` under `digitalocean` in the script.

Read the script, then run it with `bash kite-digitalocean-connect.sh`. It asks for the token, stores it in the Secret `$DO_SECRET` in `kube-system`, and applies the NodeClass. To store the Secret in another namespace, pass `--namespace` to `kite nodepool connect`. Check the class with `kite nodepool verify "$NODECLASS"`.

If `kubectl create secret` reports that the Secret exists, check which account its token belongs to before you replace it.

<Aside type="caution">
  Anyone who can read the Secret can create machines in the DigitalOcean account.
</Aside>

</TabItem>
</Tabs>

## Create a pool

Create a pool for the class, with at most three machines and six vCPUs:

```bash
kubectl apply -f - <<EOF
apiVersion: kiteplane.dev/v1alpha1
kind: NodePool
metadata:
  name: ${POOL_NAME}
spec:
  nodeClassRef:
    name: ${NODECLASS}
  limits:
    nodes: "3"
    cpu: "6"
EOF
```

## Check that the pool creates a node

Create a pod that can run only on a node from the new pool:

```bash
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: nodepool-check
spec:
  nodeSelector:
    kiteplane.dev/nodepool: ${POOL_NAME}
  containers:
    - name: web
      image: nginx:1.27
      resources:
        requests:
          cpu: 100m
          memory: 128Mi
EOF

kubectl get nodeclaims -w
```

The pool creates a NodeClaim for each new machine. When a NodeClaim appears, stop the watch and check the claim, the node, and the pod:

```bash
kubectl describe nodeclaim <claim-name>
kubectl get nodes -l "kiteplane.dev/nodepool=${POOL_NAME}"
kubectl get pod nodepool-check
```

The claim's `Launched` condition becomes `True`, its phase becomes `Registered`, and a Ready node with the pool's label appears. When the pod is Running, the pool works. Delete the pod:

```bash
kubectl delete pod nodepool-check
```

<Aside type="caution">
  The machine keeps running after you delete the pod, and the provider keeps billing for it. It runs
  until you delete the pool, or until the pool removes it under `spec.disruption.consolidateAfter`
  or `spec.disruption.expireAfter`. See [Consolidation and
  expiry](/docs/node-pools/#consolidation-and-expiry).
</Aside>

## If no node appears

```bash
kubectl get nodeclasses,nodepools,nodeclaims
kubectl describe nodepool <pool-name>
kubectl describe nodeclaim <claim-name>
```

Check these in order:

1. `kite nodepool verify <nodeclass>` passes.
2. The pool is below its limits, and its labels, taints, and machine types fit the pending pod.
3. The claim has `Launched=True`. If not, its message says which setting to fix. If the provider had no capacity for the machine, read `status.unavailable` on the pool.
4. The claim has `Registered=True` within the pool's `registrationTimeout`, ten minutes by default. If not, the controller deletes the claim and its machine, and the pool's `RegistrationTimeout` event holds the last lines that the machine's startup script printed to its console. Also check that the machine can connect to the cluster's address.
5. The node is Ready, and the pod's events show no scheduling or mount failure.

## Delete the pool

Delete the pool, not its claims or nodes. Kite drains and deletes the pool's machines, and the pool stays until the provider no longer lists any of them:

```bash
kubectl delete nodepool "$POOL_NAME"
```

`kubectl delete` waits until the pool is gone. Draining each node takes up to the pool's `spec.disruption.terminationGracePeriod`, 5 minutes by default. If the command is still waiting after that, read the pool's events:

```bash
kubectl describe nodepool "$POOL_NAME"
```

A `MachinesUnlisted` event says why Kite cannot list the machines, such as a credential that no longer works. Fix the cause, and the delete finishes. If you remove the finalizer `kiteplane.dev/termination` from the pool instead, the pool is deleted, but its machines keep running.

<Aside type="caution">Keep the class and the provider credentials until the delete returns.</Aside>

To check the provider yourself, you need the cluster's name, which the deployment's settings file contains. For the default deployment, `kite-control`, run:

```bash

```

If the file has no `KITE_CLUSTER` line, the cluster is named `kite`. Then check that the provider lists none of the pool's machines:

<Tabs syncKey="provider">
<TabItem label="Google Cloud">

This list must be empty:

```bash
gcloud compute instances list \
  --project="$GCP_PROJECT" \
  --zones="$GCP_ZONE" \
  --filter="labels.kite-cluster=${KITE_CLUSTER} AND labels.kite-pool=${POOL_NAME}"
```

</TabItem>
<TabItem label="AWS">

This list must be empty:

```bash
aws ec2 describe-instances --region "$AWS_REGION" \
  --filters "Name=tag:kite-cluster,Values=${KITE_CLUSTER}" "Name=tag:kite-pool,Values=${POOL_NAME}" \
    "Name=instance-state-name,Values=pending,running,stopping,stopped" \
  --query 'Reservations[].Instances[].InstanceId'
```

</TabItem>
<TabItem label="DigitalOcean">

Wait until no droplet has both the tags `kite-cluster:${KITE_CLUSTER}` and `kite-pool:${POOL_NAME}`.

</TabItem>
</Tabs>

Check that no other pool uses the class, then delete the class:

```bash
kubectl get nodepools -o custom-columns=NAME:.metadata.name,CLASS:.spec.nodeClassRef.name
kubectl delete nodeclass "$NODECLASS"
```

### Remove the provider's trust resources

<Tabs syncKey="provider">
<TabItem label="Google Cloud">

Run these only when nothing else uses the workload identity pool, provider, or service account:

```bash

gcloud projects remove-iam-policy-binding "$GCP_PROJECT" \
  --member="$GCP_NODE_PRINCIPAL" \
  --role="roles/compute.instanceAdmin.v1"
gcloud iam service-accounts remove-iam-policy-binding \
  "${GCP_NODE_SERVICE_ACCOUNT}@${GCP_PROJECT}.iam.gserviceaccount.com" \
  --project="$GCP_PROJECT" \
  --member="$GCP_NODE_PRINCIPAL" \
  --role="roles/iam.serviceAccountUser"
gcloud iam service-accounts delete \
  "${GCP_NODE_SERVICE_ACCOUNT}@${GCP_PROJECT}.iam.gserviceaccount.com" \
  --project="$GCP_PROJECT"
gcloud iam workload-identity-pools providers delete "$GCP_WIP_PROVIDER" \
  --project="$GCP_PROJECT" \
  --location=global \
  --workload-identity-pool="$GCP_WIP_POOL"
gcloud iam workload-identity-pools delete "$GCP_WIP_POOL" \
  --project="$GCP_PROJECT" \
  --location=global
```

</TabItem>
<TabItem label="AWS">

Run these only when nothing else uses the roles, the instance profile, or the OIDC provider. Set `KITE_ISSUER` to the issuer that `kite nodepool verify` prints:

```bash

aws iam remove-role-from-instance-profile \
  --instance-profile-name "$AWS_INSTANCE_PROFILE" --role-name "$AWS_INSTANCE_PROFILE"
aws iam delete-instance-profile --instance-profile-name "$AWS_INSTANCE_PROFILE"
aws iam delete-role --role-name "$AWS_INSTANCE_PROFILE"
aws iam delete-role-policy --role-name "$AWS_ROLE" --policy-name machines
aws iam delete-role --role-name "$AWS_ROLE"
aws iam delete-open-id-connect-provider \
  --open-id-connect-provider-arn "arn:aws:iam::${AWS_ACCOUNT}:oidc-provider/${KITE_ISSUER#https://}"
```

</TabItem>
<TabItem label="DigitalOcean">

Check that no other `NodeClass` refers to the same Secret, then delete the Secret:

```bash
kubectl get nodeclasses \
  -o custom-columns=NAME:.metadata.name,SECRET:.spec.digitalocean.credentialsSecretRef.name,NAMESPACE:.spec.digitalocean.credentialsSecretRef.namespace
kubectl delete secret "$DO_SECRET" --namespace=kube-system
```

</TabItem>
</Tabs>

## Next

- [Node pools](/docs/node-pools/): placement, limits, consolidation, and expiry.
- [Node pool reference](/docs/reference/node-pools/): every `NodeClass`, `NodePool`, and `NodeClaim` field.
- [Control plane costs](/docs/cost/): the provider bills machines separately.
