Scale Azure Kubernetes Deployments using Azure Function
By: Shwetha L
Tags: AWS, Scaling, Aks, Azure Functions, Node

Hi Readers,

This blog is to explain how to “Scale Azure Kubernetes Deployments using Azure Function”.

We were working on a Quick project to Scale the Azure AKS Nodes and Pods based on the AWS Message count in Queuing Service(SQS).

I have done a lot of research to find ways to automate this use case but was not able to find anything appropriate.

Hence I started exploring Azure services and thought to use Azure Function App to automate this use case by building custom logic in C#(.net core).

I am writing this blog to share my ideas and the ways which I used to resolve the issue of scaling AKS Nodes and Pods based on custom metrics from the cross-cloud platform.

Summary

​The agenda of this exercise is to scale the AKS Nodes and Pods based on the message count in SQS(AWS) i.e, whenever there is an increase in message count in the Queue beyond a threshold, it should trigger the Function to scale up the AKS Nodes, pods and vice-versa.

Prerequisite

Visual Studio 2017/2019 Azure subscription Azure Function libraries .NET Core libraries Kubernetes Libraries

You will Learn How To:

Build Function App to scale the AKS Nodes/VMSS based on the SQS Count Build Function App to scale the AKS pods based on the SQS Count Trigger/Build web hook on AWS to call the Azure Function App

1. Build Function App to scale the AKS Nodes/VMSS based on the SQS Count

Create the Function App of type HTTP Trigger, that can be invoked by using HTTP/HTTPS Endpoint In this Tech Blog series, a Function App to accept 2 parameters called SQS Name and SQS Count from AWS is being built The next step is to initialize and define the Azure AD / Service Principle Credentials for Authentication along with the Subscription ID Fetching the AKS Cluster details existing in the current subscription for the next set of steps The Above-mentioned details are being used to call a function to perform the scaling of VMSS

Below are the code snippets to perform the above steps:

try

{ string client = “xxxx”;

string key = “xxxx”;

string tenant = “xxxx”;

string subscriptionId = “xxxx”; // Authenticate

var creds = new AzureCredentialsFactory().FromServicePrincipal(client, key, tenant, AzureEnvironment.AzureGlobalCloud);



var azure = Azure.Authenticate(creds).WithSubscription(subscriptionId);



kubenetescluster = azure.KubernetesClusters;



// Print selected subscription and pass the sqsname and sqscount received as parameter RunSample(azure, “”, “”, sqsname, sqscount);

}

catch (Exception ex)

{

Console.Write(ex.Message);

return null;

}

6. The Function name called RunSample() is invoked with parameters to perform the scaling of Nodes

7. The resource ID of the Kubernetes cluster which needs to be scaled is fetched

8. Post getting the resource ID of the Kubernetes cluster a Kube config file based on the specified clusters is generated

9. A logic to scale the Nodes/VMSS based on the SQS count will be invoked next

For example: if the message count is crossing the limit, then the instances of Nodes/VMSS in the AKS Cluster is scaled up and when the message count drops less than the limit, then it needs to scale down.

Below are the code snippets to perform the above steps:

public static void RunSample(IAzure azure, string clientId, string secret, string sqsname, string sqscount)

{ try

{

//Get ResourceID of AKS Cluster IKubernetesCluster kubernetesCluster = azure.KubernetesClusters.GetById(“ResourceID of AKS Cluster”); //Generate kubeconfig File config = kubenetescluster.GetAdminKubeConfigContents(“Resource Group Name”, “ AKS Cluster Name”); Stream stream = new MemoryStream(config); kubeconfig = KubernetesClientConfiguration.BuildConfigFromConfigFile(stream);



kubenetes = kubernetesCluster;



var sqscountval = int.Parse(sqscount); ReplicaRequest replica = new ReplicaRequest();



if (sqscountval >= Count of SQS Messages )

{ // Updates a Kubernetes cluster with X virtual machines scalesets kubernetesCluster.Update().WithAgentPoolVirtualMachineCount(“Node Pool Name”, “Count of Nodes”).Apply(); ...



2. Build Function App to scale the AKS pods based on the SQS Count(Continued)