Azure Kubernetes Service (AKS) cluster and Azure Functions with KEDA
Create an Azure Kubernetes Service (AKS) cluster and deploy an Azure Function App with KEDA (Kubernetes-based Event Driven Autoscaling)
This example lives in the pulumi/examples repository. Check out just this directory to use it:
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examplesgit -C pulumi-examples sparse-checkout set classic-azure-ts-aks-kedacd pulumi-examples/classic-azure-ts-aks-kedaThis example demonstrates creating an Azure Kubernetes Service (AKS) Cluster, and deploying an Azure Function App with Kubernetes-based Event Driven Autoscaling (KEDA) into it, all in one Pulumi program. Please see https://docs.microsoft.com/en-us/azure/aks/ for more information about AKS and https://docs.microsoft.com/en-us/azure/azure-functions/functions-kubernetes-keda for more information about KEDA.
Prerequisites#
This example deploys a Helm Chart from the Kedacore Helm chart repository. Add and update the repository before deploying.
If you are using Helm v2:
helm init --client-onlyhelm repo add kedacore https://kedacore.github.io/chartshelm repo updateIf you are using Helm v3:
helm repo add kedacore https://kedacore.github.io/chartshelm repo updateDeploying the example#
-
Create a new stack, which is an isolated deployment target for this example:
Terminal window pulumi stack init -
Set the Azure region to deploy to:
Terminal window pulumi config set azure:location <value>pulumi config set azure:subscriptionId <YOUR_SUBSCRIPTION_ID> -
Install dependencies:
Terminal window npm install -
Deploy everything with the
pulumi upcommand. This provisions all the Azure resources necessary, including an Active Directory service principal, AKS cluster, and then deploys the Apache Helm Chart, and an Azure Function managed by KEDA, all in a single gesture:Note: Due to an issue in Azure Terraform Provider, the creation of an Azure Service Principal, which is needed to create the Kubernetes cluster (see cluster.ts), is delayed and may not be available when the cluster is created. If you get a “Service Principal not found” error, as a work around, you should be able to run
pulumi upagain, at which time the Service Principal replication should have been completed. See this issue and this doc for further details.Terminal window pulumi up -
After a couple minutes, your cluster and Azure Function app will be ready. Four output variables will be printed, reflecting your cluster name (
clusterName), Kubernetes config (kubeConfig), Storage Account name (storageAccountName), and storage queue name (queueName).Using these output variables, you may configure your
kubectlclient using thekubeConfigconfiguration:Terminal window pulumi stack output kubeconfig --show-secrets > kubeconfig.yamlKUBECONFIG=./kubeconfig.yaml kubectl get deploymentNAME READY UP-TO-DATE AVAILABLE AGEkeda-edge 1/1 1 1 9mqueue-handler 0/0 0 0 2mNow, go ahead an enqueue a new message to the storage queue. You may use a tool like Microsoft Azure Storage Explorer to navigate to the queue and add a new message.
Wait for a minute and then query the deployments again:
Terminal window KUBECONFIG=./kubeconfig.yaml kubectl get deploymentNAME READY UP-TO-DATE AVAILABLE AGEkeda-edge 1/1 1 1 14mqueue-handler 1/1 1 1 7mNote that the
queue-handlerdeployment got 1 instance ready. Looking at the pods:Terminal window KUBECONFIG=./kubeconfig.yaml kubectl get podNAME READY STATUS RESTARTS AGEkeda-edge-97664558c-q2mkd 1/1 Running 0 15mqueue-handler-c496dcfc-mb6tx 1/1 Running 0 2m3sThere’s now a pod processing queue messages. The message should be gone from the storage queue at this point. Query the logs of the pod:
Terminal window KUBECONFIG=./kubeconfig.yaml kubectl logs queue-handler-c496dcfc-mb6tx...C# Queue trigger function processed: Test MessageExecuted 'queue' (Succeeded, Id=ecd9433a-c6b7-468e-b6c6-6e7909bafce7)... -
At this point, you have a running cluster. Feel free to modify your program, and run
pulumi upto redeploy changes. The Pulumi CLI automatically detects what has changed and makes the minimal edits necessary to accomplish these changes. This could be altering the existing chart, adding new Azure or Kubernetes resources, or anything, really.
Cleaning up#
Once you are done, destroy all of the resources and the stack:
pulumi destroypulumi stack rm