NI Tech Blog
Published in

NI Tech Blog

Autoscaling your Airflow using DataDog External Metrics

Follow this step-by-step guide and learn how to autoscale your Airflow K8s pods based on ExternalMetrics

Setup Datadog cluster agent

Enable Airflow StatsD

pip install 'apache-airflow[statsd]'
[metrics]
statsd_on = True
statsd_host = localhost
statsd_port = 8125
statsd_prefix = airflow
datadog_enabled = true
# Allow List of Metrics
allow_list: executor
# Datadog Tags of the environment
datadog_tags: "statsd_group:datainfra,statsd_env:production"

Create a Custom Resource Definition (CRD) like DatadogMetric

resource "kubectl_manifest" "DatadogMetric" {
yaml_body = <<YAML
apiVersion: datadoghq.com/v1alpha1
kind: DatadogMetric
metadata:
name: running-tasks
namespace: production
spec:
query: avg:airflow.executor.running_tasks{*}.rollup(max, 60)
YAML
}
kubectl get --raw "/apis/external.metrics.k8s.io/v1beta1" | jq .
{
"kind": "APIResourceList",
"apiVersion": "v1",
"groupVersion": "external.metrics.k8s.io/v1beta1",
"resources": [
{
"name": "datadogmetric@production:running-tasks",
"singularName": "",
"namespaced": true,
"kind": "ExternalMetricValueList",
"verbs": [
"get"
]
}
]
}

Create HPA using the DatadogMetric

workers:
enabled: true
replicas: 1
resources:
requests:
cpu: 800m
memory: 3.5Gi
autoscaling:
enabled: true
maxReplicas: 2
metrics:
- type: External
external:
metric:
name: "datadogmetric@production:running-tasks"
target:
type: AverageValue
averageValue: 1

Conclusion

It’s possible to take advantage of these insights and implement them in other areas within your production environment.

Airflow HPA workers with Datadog External Metrics
## add this helm repository & pull updates from it
helm repo add airflow-stable https://airflow-helm.github.io/charts
helm repo update
## install using helm 3
helm install \
airflow \
airflow-stable/airflow \
--namespace production \
--version "8.5.2" \
--values ./custom-values.yaml

--

--

Natural Intelligence is a global leader in the online comparison market. This publication allows members of Natural Intelligence technology teams to share their knowledge and experience.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store