EKS Cluster Autoscaler: 6 Best Practices For Effective Autoscaling
Tags: cloud, eks, kubernetes, devops
Published: 2022-12-30 17:24:42

<!-- wp:paragraph -->
<p>We all love Kubernetes for its autoscaling capabilities and enjoy them when running clusters in a managed Kubernetes service like Amazon EKS. Many of you already have set up VPA and/or HPA for pod autoscaling to ensure that your application is scaled to meet the load demand. But at some point, you’re bound to face a new challenge - and this is where the EKS Cluster Autoscaler can help.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>You might not get enough capacity in your cluster during peak times. Or you might experience the opposite - wasted hardware capacity during off-peak moments. The Cluster Autoscaler comes to the rescue!</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this guide, we will explore the EKS Cluster Autoscaler to show you how it works and share some best practices to help you always adjust capacity to demand.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-when-to-use-the-cluster-autoscaler">When to use the Cluster Autoscaler?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The Cluster Autoscaler is one of the three Kubernetes autoscaling dimensions. It automatically adds or removes nodes in a cluster based on pod resource requests.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Contrary to Horizontal and Vertical Autoscalers, the Cluster Autoscaler doesn’t measure CPU and RAM usage values for its decisions. Instead, <strong>it checks the cluster every N seconds for pending pods in a “pending” state</strong>. That state indicates that the Kubernetes scheduler wasn’t able to assign these pods to a node because of insufficient cluster capacity or other conditions.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Teams use the Cluster Autoscaler to automate the process of scaling the number of nodes up or down in line with their application’s demand. The best part of using the Cluster Autoscaler is that it automatically does the scaling job for you.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>That’s what makes the Cluster Autoscaler a great cost management tool. By using it, you can eliminate overprovisioning and cloud waste, paying only for as many cloud resources as your application really utilizes.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-eks-cluster-autoscaler-autoscaling-on-aws">EKS Cluster Autoscaler: Autoscaling on AWS</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Even managed Kubernetes usually doesn’t have built-in autoscaling out-of-the-box. For a long time, the official EKS documentation has recommended using the official Kubernetes <a href="https://cast.ai/blog/guide-to-kubernetes-autoscaling-for-cloud-cost-optimization/">Cluster Autoscaler</a>.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Not a long time ago, <a href="https://cast.ai/blog/aws-karpenter-is-the-aws-open-source-kubernetes-autoscaler-right-for-you/">Karpenter</a> appeared on the scene. Karpenter is an open-source project that attempts to address some of the issues of the original Kubernetes Cluster Autoscaler.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Even though it faces new competition, the official Cluster Autoscaler remains a popular choice. Being vendor-neutral, widely adopted, and battle-tested, it’s an attractive option for many teams.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-setting-the-eks-cluster-autoscaler-up">Setting the EKS Cluster Autoscaler up</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Usually, the Cluster Autoscaler is installed as a Kubernetes Deployment in the cluster, in the kube-system namespace. You can set up the Autoscaler to run several replicas and use the leader election mechanism for high availability.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, note that actually only one replica is responsible for scaling at a time (the elected leader). It’s important to understand that multiple replicas won’t provide horizontal scalability. This means that you need to adjust it vertically to be able to handle your cluster load.</p>
<!-- /wp:paragraph -->

<!-- wp:quote {"className":"is-style-plain"} -->
<blockquote class="wp-block-quote is-style-plain"><!-- wp:paragraph -->
<p>The Cluster Autoscaler is a core component of the Kubernetes control plane. It’s there to help you make decisions around scaling and scheduling.</p>
<!-- /wp:paragraph --></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>Where does Amazon EKS come in? It might be confusing to differentiate between the official Kubernetes Cluster Autoscaler and EKS Cluster Autoscaler.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When you’re running your cluster in an AWS-managed service, the cloud provider offers an extension that makes it all work. This extension of the Kubernetes Cluster Autoscaler communicates its decisions to the AWS infrastructure using APIs (for example, to manage the EC2 instances where your cluster is running).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Before I show you how to set up the EKS Cluster Autoscaler, let’s review how it works.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-glossary">Glossary</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before diving into the how, let’s define some terms used in this guide and official documentation:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><strong>Cluster Autoscaler</strong> - a piece of software that automatically performs cluster scale-up or scale-down when needed. It adds or removes nodes in your cluster.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>Official Kubernetes Cluster Autoscaler</strong> - this cluster autoscaler is provided by the Kubernetes community (SIG Autoscaling).</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>EKS Cluster Autoscaler</strong> - EKS cluster autoscaler is an extension that bridges the Official Kubernetes Cluster Autoscaler to integrate with AWS infrastructure. Check out <a href="https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/cloudprovider/aws/README.md">this GitHub page</a> to learn more about the Cluster Autoscaler on AWS.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>Node Group</strong> - <a href="https://docs.aws.amazon.com/eks/latest/userguide/managed-node-groups.html"><strong>Node groups</strong></a> are groups of nodes within a cluster. They’re not actual resources, but you can find them as an abstraction in the Cluster Autoscaler, Cluster API, and other Kubernetes components. When grouped, nodes may share several common properties like labels and taints but still run on a different instance type or in a different Availability Zone.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>EKS Auto Scaling Group</strong> - <a href="https://docs.aws.amazon.com/autoscaling/ec2/userguide/auto-scaling-groups.html"><strong>Auto Scaling groups</strong></a> is an AWS EC2 feature to scale the number of instances up or down. We could say that it is an implementation of Node groups in EKS.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-how-does-eks-cluster-autoscaler-work">How does EKS Cluster Autoscaler work?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>What the Cluster Autoscaler does is looping through two tasks: checking the cluster for unschedulable pods and calculating whether it’s possible to consolidate all the currently deployed pods on a smaller number of nodes.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Here’s how the Cluster Autoscaler works step by step:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>It scans the cluster to detect any pods that can’t be scheduled on any existing nodes. This might result from inadequate CPU or memory resources, and another common reason is that pod’s node taint tolerations, or affinity rules don’t match any existing nodes.&nbsp;</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Suppose the Cluster Autoscaler finds a cluster that has some unschedulable pods. It then checks its managed node pools to understand whether adding a node would unblock the pod or have no effect at all. The Autoscaler adds one node to the node pool if it's the former.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>It also scans nodes in the node pools it manages, and if it identifies a node on which pods could be rescheduled to other nodes in the cluster, it evicts them and removes the spare node. When deciding to move a pod, the Cluster Autoscaler considers factors around pod priority and PodDisruptionBudgets.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Since the Autoscaler controller works on the infrastructure level, it needs permissions to view and manage node groups. That’s why security best practices like the principle of least privilege are key here – you must do your best to securely manage these necessary credentials.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-a-hands-on-guide-to-eks-cluster-autoscaler">A hands-on guide to EKS Cluster Autoscaler</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Here is a quick lab so that you can see the EKS Cluster Autoscaler in action. We will use the most straightforward way to set it up, starting with the cluster itself. We will use the <a href="https://eksctl.io/">eksctl</a> command to make things easier.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are a few prerequisites before Cluster Autoscaler can be installed:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>A working environment with aws, eksctl and kubectl command line tools,</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>EKS cluster,</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>OIDC provider,</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Auto Scaling Group with Tags,</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>IAM Policy And Service Account.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Only the first one is mandatory to be prepared before starting. Other prerequisites can be created by following provided steps.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-1-create-the-cluster">1. Create the cluster</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you don't have an EKS cluster running or want to experiment on a temporary cluster, let's create it.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><em>Note: If you create a cluster following this guide, don't forget to delete it to stop expenses (the command is provided in the last step).</em></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; eksctl create cluster --name ca-demo-cluster --instance-types=t3.medium --nodegroup-name ng-t3m --nodes 2 --nodes-max=4 --spot --asg-access --vpc-nat-mode Disable</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>This will create a cluster called ca-demo-cluster, with an Auto Scaling node group “ng-t3m” including two nodes initially and a max capacity of four nodes. The spot nodes parameter is specified to create cheaper instances. We also specify --asg-access to prepare it for autoscaling.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><em>Note: In some regions, some instance types or spot instances might not be available. Try using another instance type if the creation fails due to instance capacity errors.</em></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-2-set-up-the-oidc-provider">2. Set up the OIDC provider</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you are setting up autoscaler in an existing cluster, then please check out the <a href="https://docs.aws.amazon.com/eks/latest/userguide/enable-iam-roles-for-service-accounts.html">documentation</a> to check if you already have an OIDC provider.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you’ve just created a new cluster, you can enable the OIDC provider like this:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; eksctl utils associate-iam-oidc-provider --cluster ca-demo-cluster --approve</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-3-auto-scaling-groups-and-tags">3. Auto Scaling Groups and Tags</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you used the eksctl command in the previous step to create your node groups, these tags should already be there. For an existing cluster, you should check and add if they do not exist.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Required tags (adjust the cluster name in the second tag):</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>k8s.io/cluster-autoscaler/enabled = true
k8s.io/cluster-autoscaler/ca-demo-cluster = owned</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-4-iam-policy">4. IAM Policy&nbsp;</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Store this content to file policy.json. Note: adjust the cluster name if you used a different one.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>{
&nbsp;&nbsp;&nbsp;&nbsp;"Version": "2012-10-17",
    "Statement": &#91;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Effect": "Allow",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Action": &#91;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autoscaling:SetDesiredCapacity",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autoscaling:TerminateInstanceInAutoScalingGroup"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Resource": "*",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Condition": {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"StringEquals": {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"aws:ResourceTag/k8s.io/cluster-autoscaler/ca-demo-cluster": "owned"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Effect": "Allow",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Action": &#91;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autoscaling:DescribeAutoScalingInstances",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autoscaling:DescribeAutoScalingGroups",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ec2:DescribeLaunchTemplateVersions",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autoscaling:DescribeTags",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autoscaling:DescribeLaunchConfigurations"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Resource": "*"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;]
}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Now run this command to create an IAM policy. You can adjust the policy name or file name.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; aws iam create-policy \
    --policy-name DemoClusterAutoscalerPolicy \
    --policy-document file://policy.json</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Take note of the created policy ARN in the command output. You’ll need to specify it in the next step.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In case you missed it, you can check it again using this:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; aws iam list-policies --query 'Policies&#91;*].&#91;PolicyName, Arn]' --output text  | grep DemoClusterAutoscalerPolicy</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-5-iam-service-account">5. IAM Service Account</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now let’s create a Service account and attach our newly created policy. Make sure to <strong>set the correct</strong> ARN from the previous step:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; eksctl create iamserviceaccount \
&nbsp;&nbsp;--cluster=ca-demo-cluster \
&nbsp;&nbsp;--namespace=kube-system \
&nbsp;&nbsp;--name=cluster-autoscaler \
&nbsp;&nbsp;--attach-policy-arn=arn:aws:iam::1111111111:policy/DemoClusterAutoscalerPolicy \
&nbsp;&nbsp;--override-existing-serviceaccounts \
&nbsp;&nbsp;--approve</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-6-deploy-the-cluster-autoscaler">6. Deploy the Cluster Autoscaler</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We already have all the prerequisites and are ready to deploy the Autoscaler itself.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Download the Kubernetes deployment file:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; curl -o cluster-autoscaler-autodiscover.yaml https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>We need to make a few small adjustments in the file:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Replace &lt;YOUR CLUSTER NAME&gt; with the correct name of the cluster (<strong><em>ca-demo-cluster</em></strong> if you created it by following the previous steps).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Verify and adjust the container image version to the corresponding version compatible with the Kubernetes version in your cluster (version <a href="https://github.com/kubernetes/autoscaler/tree/master/cluster-autoscaler#releases">compatibility</a>).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I recommend adding the command line argument --skip-nodes-with-system-pods=false to the container command for more flexible scaling down.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now, let’s deploy it to our cluster:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; kubectl apply -f cluster-autoscaler-autodiscover.yaml</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>You should be able to see the cluster-autoscaler deployment and pod in the <a href="https://cast.ai/blog/kubernetes-namespace-how-to-use-it-to-organize-and-optimize-costs/">kube-system namespace</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; kubectl get deploy/cluster-autoscaler -n kube-system
&gt; kubectl get pods -n kube-system -l=app=cluster-autoscaler</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>In pod logs, you should see messages about cluster state and scaling decisions (replace with the correct pod name in the command).</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; kubectl logs cluster-autoscaler-POD-NAME -n kube-system</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>If there are any errors related to authorization - possibly any of the steps related to OIDC or IAM Policy and Service Account were not completed correctly.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-7-testing-cluster-autoscaling">7. Testing cluster autoscaling</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let’s create a simple deployment to trigger cluster scale-up.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>apiVersion: apps/v1
kind: Deployment
metadata:
&nbsp;&nbsp;name: test-app
spec:
&nbsp;&nbsp;selector:
&nbsp;&nbsp;&nbsp;&nbsp;matchLabels:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app: test-app
&nbsp;&nbsp;replicas: 1
&nbsp;&nbsp;template:
&nbsp;&nbsp;&nbsp;&nbsp;metadata:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;labels:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app: test-app
&nbsp;&nbsp;&nbsp;&nbsp;spec:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;containers:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: nginx
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image: nginx
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ports:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- containerPort: 80
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resources:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;requests:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cpu: 1000m
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memory: 1000Mi</code></pre>
<!-- /wp:code -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; kubectl apply -f test-app.yaml</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>This should create The deployment “test-app” with a single pod. Now, let’s scale it to have more replicas:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; kubectl scale --replicas=6 -f test-app.yaml</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Check the pod status:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; kubectl get pods -l=app=test-app</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Initially, some pods should be pending. Then autoscaling should be triggered, and more nodes should be added to the cluster. Some pods could be left unscheduled (pending) if the Auto Scaling group reaches the max number of nodes. Try adjusting the replica count to your needs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now let’s reduce the replica count to see the Cluster Autoscaler downscaling action:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; kubectl scale --replicas=0 -f test-app.yaml</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>The number of nodes will go down after a few minutes. You can recheck the Autoscaler pod logs to see which decisions were made.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-8-destroy-the-created-resources">8. Destroy the created resources</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Clusters and VMs incur costs in your cloud provider account. If you created a cluster by following the steps in this guide, destroy it as soon as you’re done playing around using this command:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>&gt; eksctl delete cluster --name ca-demo-cluster</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-6-best-practices-for-eks-cluster-autoscaler">6 best practices for EKS Cluster Autoscaler</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-set-the-least-privileged-access-to-the-iam-role">Set the least privileged access to the IAM role</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you use <a href="https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/cloudprovider/aws/README.md#Auto-discovery-setup">Auto Discovery</a>, it’s smart to use the least privilege access by limiting the Actions autoscaling:SetDesiredCapacity and autoscaling:TerminateInstanceInAutoScalingGroup to the Auto Scaling Groups scoped to the current cluster.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Why is this important? This is how you prevent the Cluster Autoscaler from running in one cluster by modifying node groups in a different cluster. Even if you didn’t scope the --node-group-auto-discovery argument down to the node groups of the cluster using tags (for example, k8s.io/cluster-autoscaler/&lt;cluster-name&gt;).</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>{
&nbsp;&nbsp;&nbsp;&nbsp;"Version": "2012-10-17",
&nbsp;&nbsp;&nbsp;&nbsp;"Statement": &#91;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Effect": "Allow",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Action": &#91;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autoscaling:SetDesiredCapacity",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autoscaling:TerminateInstanceInAutoScalingGroup"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Resource": "*",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Condition": {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"StringEquals": {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autoscaling:ResourceTag/k8s.io/cluster-autoscaler/enabled": "true",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"aws:ResourceTag/k8s.io/cluster-autoscaler/&lt;my-cluster&gt;": "owned"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Effect": "Allow",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Action": &#91;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autoscaling:DescribeAutoScalingInstances",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autoscaling:DescribeAutoScalingGroups",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ec2:DescribeLaunchTemplateVersions",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autoscaling:DescribeTags",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autoscaling:DescribeLaunchConfigurations"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Resource": "*"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;]
}</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-configure-node-groups-well">Configure node groups well</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To make your autoscaling effort worth the time, start by configuring a set of node groups for your cluster. If you pick the right set of node groups, you’ll maximize availability and reduce cloud costs across all of your workloads.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>In AWS, node groups are implemented with EC2 Auto Scaling Groups that offer flexibility to a broad range of use cases. </strong>Still, the Cluster Autoscaler needs to make some assumptions about your node groups, so it pays to keep the configuration consistent with them.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For example, each node needs to have identical scheduling properties (labels, taints, resources). Instead of creating many node groups containing fewer nodes, try creating fewer nodes with many nodes. This will have the greatest impact on scalability.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-use-the-correct-kubernetes-version">Use the correct Kubernetes version</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Kubernetes is evolving fast, and its control plane API changes often. The maintainers of Cluster Autoscaler do not guarantee compatibility with other versions than that for which it was released. When deploying the EKS Cluster Autoscaler, ensure you use a matching version. You can find a compatibility list <a href="https://github.com/kubernetes/autoscaler/tree/master/cluster-autoscaler#releases">here</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-check-node-group-instances-for-the-same-capacity">Check node group instances for the same capacity</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you don’t, the Cluster Autoscaler won’t work as expected. Why? Because it assumes that every instance in your node group has the same amount of CPU and memory. The Cluster Autoscaler takes the first instance type in the node group for scheduling simulation.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If your group contains instance types with more resources, they won’t be utilized - this means wasted resources and higher costs. And vice versa, if there is an instance type with fewer resources - pods won’t fit to be scheduled on it.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>That’s why you must double-check that the node group that will undergo autoscaling contains instances or nodes of the same type. And if you’re managing mixed instance types, ensure they have the same resource footprint.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-define-resource-requests-for-each-pod">Define resource requests for each pod</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The Cluster Autoscaler makes scaling decisions based on pods’ scheduling status, as well as individual node utilization. If you fail to specify resource requests for every pod, the autoscaler won’t work as it should.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When scaling up, Cluster Autoscaler chooses instance types according to pod resources.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When scaling down, it will look for nodes with utilization lower than the specified threshold. To calculate utilization, it sums up the requested resources and compares them to node capacity.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:quote {"className":"is-style-plain"} -->
<blockquote class="wp-block-quote is-style-plain"><!-- wp:paragraph -->
<p>If there are any pods or containers without resource requests, Autoscaler’s decisions will definitely be affected, and you’ll be facing an issue.</p>
<!-- /wp:paragraph --></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>Make your life easier and double-check that all the pods scheduled to run in an autoscaled node or instance group have their resource requests specified.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading" id="h-set-the-poddisruptionbudget-wisely">Set the PodDisruptionBudget wisely</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>PodDisruptionBudget (PDB) helps in two ways. Its main mission is to prevent your applications from disruption. PDB will protect from evicting all or a significant amount of pods of a single Deployment or StatefulSet. The Cluster Autoscaler will respect PDB rules and downscale nodes safely by moving only the allowed number of pods.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>On the other hand, PDB can help to downscale not only in a restrictive way but also in a permissive one. By default, the Cluster Autoscaler won’t evict any kube-system pods unless PDB is specified. So, by specifying a reasonable PDB, you will enable the Cluster Autoscaler to evict even kube-system pods and remove underutilized nodes.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><em>Note: Before an eviction, the Cluster Autoscaler ensures that evicted pods will be scheduled on a different node with enough free capacity.</em></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When specifying the PodDisruptionBudget, consider the minimum necessary number of replicas of the pods. Many system pods run as single instance pods (aside from Kube-dns), and restarting them might cause disruptions. So, don’t add a disruption budget for single instance pods like metrics-server; you’ll sleep better at night.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-curious-to-see-a-modern-autoscaler-in-action">Curious to see a modern autoscaler in action?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>By constantly monitoring cloud provider inventory pricing and availability in supported cloud provider regions and zones, we have collected data and knowledge on which instance families provide the best value and which should be avoided. That’s how CAST AI Cluster Autoscaler can select the best matching instance types on its own (or according to your preferences).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Given that it’s a managed service, you don’t need to worry about upgrades, scalability, and availability. CAST AI platform is monitoring clusters and is always ready to act promptly.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Here’s an example showing how closely the CAST AI autoscaler follows the actual resource requests in the cluster.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://lh4.googleusercontent.com/Cm982SofGTRQlGIqHMZ3jWgBLEmIRXMUleAB7eTEul7ItrtKVJuaWAIMVG1hCIj8RUxyr76kOfOfkAkqtqDCCUMuc3tzUyDCTffyMGvinoLuT16ZIKK0pnAfX7PthN7nYDjy1rscjHlAwu8SwreQljfj-SK9H3_2F4tsGkSQuGgcBeyz085AoB92Lwna-Q" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Check how well your cluster is doing in terms of autoscaling and cost-efficiency. Check out instance recommendations by connecting your cluster to our free and <a href="https://cast.ai/blog/how-does-the-read-only-cast-ai-agent-work-and-what-data-can-it-read/">read-only mode</a> <a href="https://cast.ai/cloud-cost-monitoring/">Kubernetes cost monitoring</a> module – it works with Amazon EKS and Kops, as well as GKE and AKS.</p>
<!-- /wp:paragraph -->

<!-- wp:group {"style":{"spacing":{"padding":{"top":"2rem","right":"2rem","bottom":"2rem","left":"2rem"},"margin":{"top":"2rem","bottom":"2rem"}}},"backgroundColor":"cast-black","className":"pattern-cta-text-btn","layout":{"type":"flex","flexWrap":"wrap","justifyContent":"space-between","verticalAlignment":"center"}} -->
<div class="wp-block-group pattern-cta-text-btn has-cast-black-background-color has-background" style="margin-top:2rem;margin-bottom:2rem;padding-top:2rem;padding-right:2rem;padding-bottom:2rem;padding-left:2rem"><!-- wp:group {"style":{"spacing":{"padding":{"top":"0rem","right":"0rem","bottom":"0rem","left":"0rem"},"blockGap":"0.5rem"}},"layout":{"type":"constrained","contentSize":"520px"}} -->
<div class="wp-block-group" style="padding-top:0rem;padding-right:0rem;padding-bottom:0rem;padding-left:0rem"><!-- wp:heading {"level":5} -->
<h5 class="wp-block-heading" id="h-cast-ai-clients-save-an-average-of-63-on-their-kubernetes-bills">CAST AI clients <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-cast-green-color">save an average of 63%</mark> on their Kubernetes bills</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Connect your cluster and see your costs in 5 min, no credit card required.</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

<!-- wp:buttons {"layout":{"type":"flex","justifyContent":"left","orientation":"horizontal","flexWrap":"nowrap"}} -->
<div class="wp-block-buttons"><!-- wp:button {"width":100} -->
<div class="wp-block-button has-custom-width wp-block-button__width-100"><a class="wp-block-button__link wp-element-button" href="https://console.cast.ai/?screen_hint=SIGNUP" target="_blank" rel="noreferrer noopener">Get started</a></div>

<!-- /wp:group -->