Running Azure Functions on Kubernetes like on Azure in the consumption plan

Azure Functions 2.0 introduced the capability to run Functions in a container. So, you have the choice to run your Functions natively on Azure in the consumption plan for automated massive scale or in a container on your Azure Kubernetes Service or Kubernetes cluster.

But how you configure automated scaling for a Functions application in a container on Kubernetes to run it like on Azure?

The answer is the Horizontal Pod Autoscaler (HPA) in Kubernetes. Supporting different metrics like CPU, memory and more, you can configure the HPA for your Functions application to scale them out on-demand as requests are coming in.

Here is an example HPA configuration for a Kubernetes Functions deployment.

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: helloworld-function-figlet
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: helloworld-function-figlet
  minReplicas: 1
  maxReplicas: 20
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 10

-> https://github.com/neumanndaniel/kubernetes/blob/master/hpa/hpa-functions.yaml

I have set the targetAverageUtilization value to 10% for demonstration purposes. Depending on what your Function is doing you have a higher threshold here.

When running Functions applications with an HPA enabled you should consider using the Cluster Autoscaler (CA) also. The CA takes care of the agent node scaling. So, you ensure enough capacity for your container applications.

-> https://docs.microsoft.com/en-us/azure/aks/autoscaler

Let us have a look how the HPA reacts when we are putting some load onto our Functions application.

In the following screenshots you see the Function container running without any load and the deployment is scaled down to one.

aksfunctions01aksfunctions02

For the load testing I am using Azure Test Plans as part of Azure DevOps.

-> https://azure.microsoft.com/en-us/services/devops/test-plans/

The following load test configuration has been used to test the HPA configuration.

aksfunctions03aksfunctions04

The load test runs for 5 minutes and starts with 10 users running requests against the Functions application. Every 10 seconds the user count is increased.

As the request increases the HPA spins up additional containers and does a scale-out of the deployment to keep the Functions application operational.

aksfunctions05aksfunctions06

When the requests are decreasing the HPA adjusts the number of containers and does a scale-in of the deployment.

aksfunctions07

As you can see with the HPA configuration you can run Azure Functions on Kubernetes like on Azure in the consumption plan.

The easiest way to deploy the Functions application with all configuration parameters on Kubernetes is the usage of helm and a helm chart. Have a look at my GitHub repository for an example helm chart in this case.

-> https://github.com/neumanndaniel/kubernetes/tree/master/helm/charts/function-figlet

Facebooktwittergoogle_pluslinkedinmail