Azure Kubernetes Service (AKS) Cluster Autoscaler

Application high availability is critical for enterprise, because unavailability of application may cost the organizations. So in a microservices applications, each of the services scale different rate. Its important to adjust the application infrastructure to handle such kind of application scale.

Azure Kubernetes Service (AKS) is a managed Kubernetes (K8s) offering from Azure team to deploy microservice application. At the time of writing this blog AKS doesn’t have autoscaling build in to its platform, but AKS auto scaling feature in a preview. So I thought of go through this awesome feature and see the results.

AKS Cluster Autoscaler

To applications changing demand and cluster should have an autoscaling capabilities. AKS have two types of scaling.

  • Cluster Auto Scaler – This will auto scale AKS cluster worker nodes based on the resource utilization
  • Horizontal Pod Auto Scaler – This will autoscale pods inside the Kubernetes cluster using ReplicaSet, ReplicaController or Deployment
Image :- Microsoft

Before we start we need to verify few things in place.

Note:- If you have an AKS cluster with VM worker nodes this autoscaler feature is not supported. This is in preview and we need to use VMSSPreview feature enabled.

  • We need to have Azure CLI version 2.0.55 or later. Run az –version to verify current version installed.
  • Azure Subscription

AKS cluster autoscaler only supported in virtual machine scale set (VMSS) with Kubernetes version 1.12.4. This is still in preview stage and we need to opt-in to preview features to try this. For this we need to add the aks-preview feature to CLI

Install aks-preview CLI extension
az extension add --name aks-preview
Register scale set feature provider

To create an AKS that uses scale sets, you must also enable a feature flag on your subscription. To register the VMSSPreview feature flag we can use following command

az feature register --name VMSSPreview --namespace Microsoft.ContainerService

To register the feature it will take few minutes. We can verify the status of the registration from below command

az feature list -o table --query "[?contains(name, 'Microsoft.ContainerService/VMSSPreview')].{Name:name,State:properties.state}"

When ready, refresh the registration of the Microsoft.ContainerService resource provider using below command

az provider register --namespace Microsoft.ContainerService
AKS cluster and enable the cluster autoscaler

First we create resource group for AKS cluster

az group create --name kubecluster --location southeastasia

Next we will create the AKS cluster with auto scaler enabled

az aks create \
  --resource-group kubecluster \
  --name kubecluster-demo \
  --kubernetes-version 1.12.6 \
  --node-count 1 \
  --enable-vmss \
  --enable-cluster-autoscaler \
  --min-count 1 \
  --max-count 10
  • –resource-group – specify the resource group created in earlier step
  • –name – Name of the Kubernetes cluster
  • –kubernetes-version – Kubernetes version to install (must be 1.12.4 or above)
  • –node-count – Initial node count
  • –enable-vmss – Use VMSS as worker nodes
  • –enable-cluster-autoscaler – Enable cluster autoscaler
  • –min-count – Minimum node can scale down
  • –max-count – Maximum node can scale up

It will take few minutes to create the cluster depending on the node count.

If the AKS deployment succeeded we can see as above.

Testing the AKS autoscaler

So we created an AKS cluster with autoscaler enabled. Next step is to see the autoscaler work in action. For this I created a ReplicaSet yaml config file to deploy it to cluster.

apiVersion: apps/v1beta2
kind: ReplicaSet
metadata:
  name: kubia-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: kubia
  template:
    metadata:
      labels:
        app: kubia
    spec:
      containers:
        - image:  kasunsjc/kubia
          name:  kubia
          ports:
          - containerPort: 8080
            protocol: TCP

Initial stage of this ReplicaSet deployment it creates only 3 replicas of Pods.

So to autoscale this ReplicaSet I used following kubectl command

kubectl scale --replicas=200 rs/kubia-rs

This will create 200 replica pods under the ReplicaSet resource.

Following demo shows how AKS autoscale scale up & down based on the resources deployed.

AKS autoscaler scale up within minutes, but for scale down it will take much time comparing to scale up. So if you wouldn’t see the scale down process wait for few minutes, then we can see the VMSS instance count reduced appropriately.

Further Reference