title | description | author | ms.topic | ms.custom | ms.date | ms.author |
---|---|---|---|---|---|---|
Azure Functions on Kubernetes with KEDA |
Understand how to run Azure Functions in Kubernetes in the cloud or on-premises using KEDA, Kubernetes-based event driven autoscaling. |
eamonoreilly |
conceptual |
build-2023, build-2024 |
08/19/2024 |
eamono |
The Azure Functions runtime provides flexibility in hosting where and how you want. KEDA (Kubernetes-based Event Driven Autoscaling) pairs seamlessly with the Azure Functions runtime and tooling to provide event driven scale in Kubernetes.
Important
Running your containerized function apps on Kubernetes, either by using KEDA or by direct deployment, is an open-source effort that you can use free of cost. Best-effort support is provided by contributors and from the community by using GitHub issues in the Azure Functions repository. Please use these issues to report bugs and raise feature requests.
For fully-supported Kubernetes deployments, instead consider Azure Container Apps hosting of Azure Functions.
The Azure Functions service is made up of two key components: a runtime and a scale controller. The Functions runtime runs and executes your code. The runtime includes logic on how to trigger, log, and manage function executions. The Azure Functions runtime can run anywhere. The other component is a scale controller. The scale controller monitors the rate of events that are targeting your function, and proactively scales the number of instances running your app. To learn more, see Azure Functions scale and hosting.
Kubernetes-based Functions provides the Functions runtime in a Docker container with event-driven scaling through KEDA. KEDA can scale in to zero instances (when no events are occurring) and out to n instances. It does this by exposing custom metrics for the Kubernetes autoscaler (Horizontal Pod Autoscaler). Using Functions containers with KEDA makes it possible to replicate serverless function capabilities in any Kubernetes cluster. These functions can also be deployed using Azure Kubernetes Services (AKS) virtual nodes feature for serverless infrastructure.
To run Functions on your Kubernetes cluster, you must install the KEDA component. You can install this component in one of the following ways:
-
Azure Functions Core Tools: using the
func kubernetes install
command. -
Helm: there are various ways to install KEDA in any Kubernetes cluster, including Helm. Deployment options are documented on the KEDA site.
You can deploy any function app to a Kubernetes cluster running KEDA. Since your functions run in a Docker container, your project needs a Dockerfile. You can create a Dockerfile by using the --docker
option when calling func init
to create the project. If you forgot to create your Dockerfile, you can always call func init
again from the root of your code project.
-
(Optional) If you need to create your Dockerfile, use the
func init
command with the--docker-only
option:func init --docker-only
To learn more about Dockerfile generation, see the
func init
reference. -
Use the
func kubernetes deploy
command to build your image and deploy your containerized function app to Kubernetes:func kubernetes deploy --name <name-of-function-deployment> --registry <container-registry-username>
In this example, replace
<name-of-function-deployment>
with the name of your function app. The deploy command performs these tasks:- The Dockerfile created earlier is used to build a local image for your containerized function app.
- The local image is tagged and pushed to the container registry where the user is logged in.
- A manifest is created and applied to the cluster that defines a Kubernetes
Deployment
resource, aScaledObject
resource, andSecrets
, which includes environment variables imported from yourlocal.settings.json
file.
The previous deployment steps work for private registries as well. If you're pulling your container image from a private registry, include the --pull-secret
flag that references the Kubernetes secret holding the private registry credentials when running func kubernetes deploy
.
After deploying you can remove a function by removing the associated Deployment
, ScaledObject
, an Secrets
created.
kubectl delete deploy <name-of-function-deployment>
kubectl delete ScaledObject <name-of-function-deployment>
kubectl delete secret <name-of-function-deployment>
You can remove KEDA from your cluster in one of the following ways:
-
Azure Functions Core Tools: using the
func kubernetes remove
command. -
Helm: see the uninstall steps on the KEDA site.
KEDA has support for the following Azure Function triggers:
You can use Azure Functions that expose HTTP triggers, but KEDA doesn't directly manage them. You can use the KEDA prometheus trigger to scale HTTP Azure Functions from one to n
instances.
For more information, see the following resources: