Azure Functions : Kubernetes

1 minute read

Just like OpenFaaS, Azure provides serverless computing.
The Azure functions runtime can run anywhere, it is not tied to Azure.
You can use Functions to build web APIs, respond to database changes, process IoT streams, manage message queues…

Setup

1/ Install the Azure Functions Core Tools
2/ Create a new function from a template with a Dockerfile

func new --name '[function-name]' \
         --worker-runtime '[worker-runtime]' \
         --template '[template]' \
         --docker
  • function-name: Just pick a name
  • worker-runtime: dotnet, dotnetIsolated, node, python, powershell, custom
  • template: HTTP trigger, RabbitMQ trigger, SendGrid, Kafka trigger…

Note: If you are not passing the arguments worker-runtime/template, the func CLI will present you with a list of options.

Example Python HTTP trigger

1/ Create the function

func new --name 'my-python-function' \
         --worker-runtime python \
         --template 'HTTP trigger' \
         --authlevel anonymous \
         --docker

2/ Test the function locally

func start
curl http://localhost:7071/api/my-python-function\?name\=sir 

3/ Build/Push the docker image

docker build -t ghcr.io/[org]/my-python-function:0.1.0 .
docker push ghcr.io/[org]/my-python-function:0.1.0

4/ Generate functions.yaml (K8s)

func kubernetes deploy \
     --name k8s-deployment \
     --image-name "ghcr.io/[org]/my-python-function:0.1.0" \
     --write-configs

At this stage you can directly apply the generated functions.yaml file with kubectl.
It is most likely that you would want to commit this file in a git repository and manage it via Kustomize and ArgoCD (:smiley:).
Also if you are deploying it in AKS, best practice would be for secrets to use the secrets-store-csi-driver-provider-azure.

Final thoughts

The fact to be able to run Azure functions in any kubernetes cluster allow for a lot of flexibility.
Combine with KEDA, we are able to autoscale based on e.g messages in the Azure Service Bus.
Also from a developer perspective, configuration is done through a simple yaml file consumed by the Kubernetes API.
Concerning observability, if you happen to be running a service Mesh like Linkerd, you can really track all requests to your Azure Function.

References

Updated: