# Turn on autoscaling

Let the cluster autoscaler grow a node group when pods cannot be scheduled and shrink it when nodes are underused, and tune it for the whole cluster.

Replace _my-cluster_ with your cluster's name or id, and _workers_ with the node group's name.

With autoscaling on, the cluster autoscaler changes the group's `desiredSize` between `minSize` and
`maxSize`: pods that cannot be scheduled for lack of room grow the group, and underused nodes are
drained and removed. Each change appears in **Activity**.

## Prerequisites

- Permission for `eks:UpdateNodegroupConfig` (or `eks:CreateNodegroup` for a new group); for the
  cluster-wide settings, `eks:UpdateClusterConfig`.
- In the console, also `eks:DescribeCluster` and `eks:ListNodegroups`.

## Turn it on for a node group

**Console**
1. Open **Kubernetes**, open the cluster, then choose **Node groups**.
2. On the node group's row, choose **Edit**.
3. Turn on **Autoscaling**, and set **Minimum** and **Maximum**.
4. Choose **Save**.

**CLI**
```sh
dcloud eks nodegroups update my-cluster workers --autoscaling --min-size 1 --max-size 10
```

**API**
```http
PATCH /eks/api/clusters/my-cluster/nodegroups/workers
{ "autoscaling": true, "minSize": 1, "maxSize": 10 }
```

**Terraform**
```hcl
resource "dcloud_eks_nodegroup" "workers" {
  cluster_id    = dcloud_eks_cluster.main.id
  name          = "workers"
  instance_type = "performance-4"
  min_size      = 1
  desired_size  = 2
  max_size      = 10
  autoscaling   = true

  lifecycle {
    ignore_changes = [desired_size]
  }
}
```

`ignore_changes` keeps an apply from undoing what the autoscaler set.

A node group created together with its cluster starts with autoscaling off; turn it on this way.

## Tune the autoscaler for the cluster

Three settings apply to every node group of the cluster that has autoscaling on:

| Setting                         | Default       | Range                                | Meaning                                                                  |
| ------------------------------- | ------------- | ------------------------------------ | ------------------------------------------------------------------------ |
| `scaleDownUtilizationThreshold` | `0.5`         | 0.1–0.9                              | A node whose pods request less than this share of CPU and memory may go. |
| `scaleDownUnneededMinutes`      | `10`          | 1–120                                | How long a node must be unneeded before it goes.                         |
| `expander`                      | `least-waste` | `least-waste`, `random`, `most-pods` | Which group grows when several could.                                    |

**Console:** not available. The console does not show or change the autoscaler's settings.
**CLI**
```sh
dcloud eks clusters update my-cluster \
  --autoscaler '{"scaleDownUtilizationThreshold":0.6,"scaleDownUnneededMinutes":15}'
```

**API**
```http
PATCH /eks/api/clusters/my-cluster
{ "autoscaler": { "scaleDownUtilizationThreshold": 0.6, "scaleDownUnneededMinutes": 15 } }
```

**Terraform**
Set `autoscaler` on the `dcloud_eks_cluster` (a JSON string, for example
`jsonencode({ scaleDownUtilizationThreshold = 0.6 })`) and apply.

Settings you leave out keep their current value.

## Related

- [Change a node group](change-a-node-group.md)
- `eks.nodegroups.update` · `eks.clusters.update`
