# Enable Cluster Autoscaler in EKS with easy steps

Kubernetes offers human intervention free function to scale up or down the resources to meet the changing demands. Cloud Autoscaler is an EKS supported feature to enable on any existing cluster.

## Prerequisites

1. An existing Amazon EKS cluster
2. Access to Kubeconfig file
3. Kubectl and AWS Cli

Right now the applied IAM roles in your cluster and nodes would be looking like this. We will be adding new IAM policy in our IAM role on the nodes.
![Screenshot from 2022-01-24 01-22-26.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642967572579/M0vn-r1io.png)

## Configuring Cluster Autoscaler

1. Create a policy with following content. You can name it as ClusterAutoscalerPolicy. 

```json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "autoscaling:DescribeAutoScalingGroups",
                "autoscaling:DescribeAutoScalingInstances",
                "autoscaling:DescribeLaunchConfigurations",
                "autoscaling:DescribeTags",
                "autoscaling:SetDesiredCapacity",
                "autoscaling:TerminateInstanceInAutoScalingGroup",
                "ec2:DescribeLaunchTemplateVersions"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}
```

2. Attach this policy to the IAM Worker Node Role which is already in use.

![eks (1).jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1642967721521/kKO0rJJQ7.jpeg)

3. Deploy the ```Cluster Autoscaler``` with the following command.

```bash
kubectl apply -f https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml
```

4. Add an annotation to the deployment with the following command.
```bash
kubectl -n kube-system annotate deployment.apps/cluster-autoscaler cluster-autoscaler.kubernetes.io/safe-to-evict="false"
```

5. Edit the Cluster Autoscaler deployment with the following command.
```bash
kubectl -n kube-system edit deployment.apps/cluster-autoscaler
```
This command will open the yaml file for your editting. Replace <CLUSTER NAME> value with your own cluster name, and add the following command option ```--skip-nodes-with-system-pods=false``` to the command section under ```containers``` under ```spec```. Save and exit the file by pressing ```:wq```. The changes will be applied.

6. Find an appropriate version of your cluster autoscaler in the [link](https://github.com/kubernetes/autoscaler/releases). The version number should start with version number of the cluster Kubernetes version. For example, if you have selected the Kubernetes version 1.17, you should find something like ```1.17.x```.

7. Then, in the following command, set the Cluster Autoscaler image tag as that version you have found in the previous step.
```bash
kubectl -n kube-system set image deployment.apps/cluster-autoscaler cluster-autoscaler=us.gcr.io/k8s-artifacts-prod/autoscaling/cluster-autoscaler:<YOUR-VERSION-HERE>
```

## Conclusion
The Kubernetes Cluster Autoscaler is an easy to use function to prevent downtime when pods fail or are rescheduled onto other nodes by adjusting the number of nodes. This is typically already installed as a Deployment in an EKS cluster. Basic familiarity can save extensive human resources.

