Cluster Autoscaler in Kubernetes

Kubernetes Advocate
AVM Consulting Blog
3 min readJun 6, 2020

Cluster Autoscaler (CA) scales your cluster nodes supported unfinished pods.

In simple words, you can say scale up and scale down of nodes as demanded.

High-level CA Workflow

Prerequisites

  • Kubernetes Cluster (I tested with 1.10.11 via KOPS on AWS)
  • Resource limit set in deployment with HPA enabled
  • Metric Server installed

How to Setup Metric Server

How to Setup HPA

Assuming you have the below setup in working mode to carry out the testing, I have used the AWS environment and used t2.micro instances for testing. I have also configured a separate instance group (DB-pool) for testing

Validation

Let’s apply the IAM permissions to ASG

Check the attached policy-JSON file and deploy via AWS CLI

aws iam put-role-policy --role-name nodes.${KOPS_CLUSTER_NAME} --policy-name asg-nodes.${KOPS_CLUSTER_NAME} --policy-document file:///tmp/autoscale-aws-policy.json

Let’s install the auto-scaler

  • Clone or download the YAML file and make necessary changes in the file, you can specify multiple instance group ASG
- --nodes=1:2:db-pool.houm-host-devops-11-sg.k8s.local

Let me explain what all fields are :

  • nodes — common name
  • 1:2 — Minimum: Maximum node used for scale up and scale down
  • DB-pool — ASG name
  • home-host-DevOps-11-sg.k8s.local — Cluster name

You can also use aws CLI to list the autoscaling group

aws autoscaling describe-auto-scaling-groups |grep AutoScalingGroupName

NOTE: I have been using cluster created with KOPS it attaches the auto-scaling policy to all ASG associated with a cluster, or else you can use the policy uploaded and attach to your ASG in AWS

Let’s deploy the HPA

HPA
-: Use the HPA which is explained and you will see at one point when your pods go pending your new node will be spawned.

OR Simply deploy the Nginx with multiple replicas!

kubectl run nginx --image=nginx --replicas=20
  • Watch the POD creation and going in a pending state
nginx-65899c769f-fdkh5   0/1       ContainerCreating   0         2m
nginx-65899c769f-b8xqf 0/1 ContainerCreating 0 2m
nginx-65899c769f-89njs 0/1 Pending 0 0s
nginx-65899c769f-89njs 0/1 Pending 0 0s
nginx-65899c769f-2lpwt 0/1 Pending 0 0s
nginx-65899c769f-2lpwt 0/1 Pending 0 0s
nginx-65899c769f-987tb 0/1 Pending 0 0s
nginx-65899c769f-987tb 0/1 Pending 0 0s
  • ScaleUP logs you can see in your cluster-scaler pod, the max we have only defined 2 nodes
  • Verify new node is launched

Let’s delete the deployment, once the load is normal then we will see scale down logs, and the node is removed from the cluster and deleted.

kubectl delete deploy nginx
kubectl get pod
  • Let’s verify the logs in a cluster-scaler pod, one of the nodes (IP-172–20–62–212.ap-southeast-1.compute.internal) is marked for Scale Down
  • Let’s wait for 10 mins and you see the node is removed, verify same in the cluster-scaler pod
  • Lets verify the nodes running in cluster,node (IP-172–20–62–212.ap-southeast-1.compute.internal) is removed
  • TBD Get the default value checks for scale-up Get the default value time for scale down How it can be changed

Reference links

https://github.com/kubernetes/autoscaler/tree/master/cluster-autoscaler/cloudprovider/aws

https://kumorilabs.com/blog/k8s-5-setup-horizontal-pod-cluster-autoscaling-kubernetes/

Check out our best AWS deal:

👋 Join us today !!

️Follow us on LinkedIn, Twitter, Facebook, and Instagram

If this post was helpful, please click the clap 👏 button below a few times to show your support! ⬇

--

--

Kubernetes Advocate
AVM Consulting Blog

Vineet Sharma-Founder and CEO of Kubernetes Advocate Tech author, cloud-native architect, and startup advisor.https://in.linkedin.com/in/vineet-sharma-0164