Queue-based scaling on Azure Kubernetes Service (AKS) with Helm

Ferhat Vurucu
3 min readDec 30, 2020

This implementation requires an adapter to scale your deployments on Azure Kubernetes Service (AKS) using the Horizontal Pod Autoscaler (HPA) with External Metrics from Azure Service Bus Queues.

Prerequisites

Create an Azure Container Registry

Azure Container Registry helps you to build, store, secure, scan, replicate, and manage container images and artifacts with a fully managed, geo-replicated instance of OCI distribution.

The below example uses az acr create to create an ACR named HelmACR in the QueueBasedScalingResourceGroup resource group with the Basic SKU.

az group create --name QueueBasedScalingResourceGroup --location eastus
az acr create --resource-group QueueBasedScalingResourceGroup --name HelmACR --sku Basic

Create an Azure Service Bus queue

Microsoft Azure Service Bus is a fully managed enterprise integration message broker. Service Bus can decouple applications and services. Service Bus offers a reliable and secure platform for asynchronous transfer of data and state.

Data is transferred between different applications and services using messages. A message is in binary format and can contain JSON, XML, or just text.

Service Bus queues support a brokered messaging communication model. When using queues, components of a distributed application do not communicate directly with each other; instead they exchange messages via a queue, which acts as an intermediary (broker). A message producer (sender) hands off a message to the queue and then continues its processing. Asynchronously, a message consumer (receiver) pulls the message from the queue and processes it.

The below example helps you to create an Azure Service Bus Queue.

az servicebus namespace create --resource-group QueueBasedScalingResourceGroup --name ServiceBusNS --location eastus
az servicebus queue create --resource-group QueueBasedScalingResourceGroup --namespace-name ServiceBusNS --name ScalingQueue

Create an Azure Kubernetes Service cluster

The below command creates an AKS cluster called QueueBasedScalingAKS and attaches HelmACR.

az aks create -g QueueBasedScalingResourceGroup -n QueueBasedScalingAKS --location eastus  --attach-acr HelmACR --generate-ssh-keys

Connect to your AKS cluster

To connect to the Kubernetes cluster from your local computer, you use kubectl, the Kubernetes command-line client. It’s already installed on Azure Cloud Shell.

az aks install-cli
az aks get-credentials --resource-group QueueBasedScalingResourceGroup --name QueueBasedScalingAKS

Deploy Azure Kubernetes Metrics Adapter on your AKS Cluster.

Build and push the sample application to the ACR

Use the az acr build command to build and push an image to the registry.

az acr build --image queuebasedscaling:v1 \
--registry HelmACR \
--file Dockerfile .

Create your Helm chart

Helm is an open-source packaging tool that helps you install and manage the lifecycle of Kubernetes applications. Similar to Linux package managers such as APT and Yum, Helm is used to manage Kubernetes charts, which are packages of preconfigured Kubernetes resources.

Generate your Helm chart using the helm create command.

helm create queuebasedscaling

Make the required updates to queuebasedscaling/values.yaml.

# Default values for queuebasedscaling.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

image:
repository: *helmacr.azurecr.io*/queuebasedscaling
pullPolicy: IfNotPresent

Run your Helm chart

Applications running on Kubernetes may need to autoscale based on metrics that don’t have an obvious relationship to any object in the Kubernetes cluster and you can address this use case with external metrics.

External metrics allow you to autoscale your cluster based on any metric available in your monitoring system.

Make the required updates to queuebasedscaling/templates and queuebasedscaling/values.yaml.

apiVersion: azure.com/v1alpha2
kind: ExternalMetric
metadata:
name: queuemessages
spec:
type: azuremonitor
azure:
resourceGroup: QueueBasedScalingResourceGroup
resourceName: ServiceBusNS
resourceProviderNamespace: Microsoft.ServiceBus
resourceType: namespaces
metric:
metricName: Messages
aggregation: Total
filter: EntityName eq 'ScalingQueue'

Horizontal Pod Autoscaler automatically scales the number of pods in a replication controller, deployment, replica set or stateful set based on observed data from ExternalMetric.

Make the required updates to queuebasedscaling/templates and queuebasedscaling/values.yaml.

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: queuebasedscaler
spec:
scaleTargetRef:
apiVersion: extensions/v1beta1
kind: Deployment
name: <yourdeploymentname>
minReplicas: 2
maxReplicas: 10
metrics:
- type: External
external:
metricName: queuemessages
targetValue: 100

Use the helm install command to install your application using your Helm chart.

helm install queuebasedscaling queuebasedscaling/

To monitor the progress, use the kubectl get hpa command.

$ kubectl get hpa

NAME REFERENCE TARGETS MINPODS MAXPODS
queuebasedscaler Deployment/<yourdeploymentname> 15/100 2 10

Kubernetes Event-driven Autoscaling (KEDA) is an alternative solution to scale applications based on Azure Service Bus Queues or Topics. It’s an official CNCF Sandbox project now.

--

--