Skip to main content

airflow balancer

Utilities for tracking hosts and ports and load balancing DAGs

Build Status codecov License PyPI

Overview

airflow-balancer is a utility library for Apache Airflow to track host and port usage via yaml files. It enables you to:

  • Track Hosts: Define and manage a pool of worker hosts with different capabilities (OS, queues, tags)
  • Manage Ports: Track port usage across your host infrastructure to avoid conflicts
  • Load Balance: Intelligently select hosts based on queues, operating systems, tags, or custom criteria
  • Integrate with Airflow: Automatically create Airflow pools for each host and port

Integration with airflow-laminar Stack

airflow-balancer is tightly integrated with the airflow-laminar ecosystem:

Library Integration
airflow-pydantic Core data models (Host, Port, BalancerConfiguration) are defined in airflow-pydantic, providing full Pydantic validation, type checking, and JSON/YAML serialization support
airflow-config Configuration loading via Hydra for hierarchical configs with defaults, overrides, and environment-specific settings

With airflow-balancer, you can register host and port usage in configuration:

_target_: airflow_balancer.BalancerConfiguration
default_username: timkpaine
hosts:
  - name: host1
    size: 16
    os: ubuntu
    queues: [primary]

  - name: host2
    os: ubuntu
    size: 16
    queues: [workers]

  - name: host3
    os: macos
    size: 8
    queues: [workers]

ports:
  - host: host1
    port: 8080

  - host_name: host2
    port: 8793

Either via airflow-config or directly, you can then select amongst available hosts for use in your DAGs.

from airflow_balaner import BalancerConfiguration, load

balancer_config: BalancerConfiguration = load("balancer.yaml")

host = balancer_config.select_host(queue="workers")
port = balancer_config.free_port(host=host)

...

operator = SSHOperator(ssh_hook=host.hook(), ...)

Visualization

Configuration, and Host and Port listing is built into the extension, available either from the topbar in Airflow or as a standalone viewer (via the airflow-balancer-viewer CLI).

Installation

You can install from pip:

pip install airflow-balancer

For use with Apache Airflow 2.x:

pip install airflow-balancer[airflow]

For use with Apache Airflow 3.x:

pip install airflow-balancer[airflow3]

Or via conda:

conda install airflow-balancer -c conda-forge

Using with airflow-config

The recommended approach is to use airflow-balancer as an extension within your airflow-config configuration:

# config/config.yaml
# @package _global_
_target_: airflow_config.Configuration
defaults:
  - extensions/balancer@extensions.balancer
# config/extensions/balancer.yaml
# @package extensions.balancer
_target_: airflow_balancer.BalancerConfiguration

default_username: airflow
default_key_file: /home/airflow/.ssh/id_rsa
hosts:
  - name: worker1
    size: 16
    os: ubuntu
    queues: [workers]
from airflow_config import load_config

config = load_config("config", "config")
balancer = config.extensions["balancer"]

# Select a host and use its SSH hook
host = balancer.select_host(queue="workers")
operator = SSHOperator(ssh_hook=host.hook(), ...)

Using with airflow-pydantic

Since the core models are defined in airflow-pydantic, you can leverage its testing utilities:

from airflow_balancer import BalancerConfiguration, Host
from airflow_balancer.testing import pools, variables
from airflow_pydantic import Variable

# Testing with mocked pools
with pools():
    config = BalancerConfiguration(
        hosts=[Host(name="test-host", size=8, queues=["test"])]
    )
    assert config.select_host(queue="test").name == "test-host"

# Using Airflow Variables for credentials
host = Host(
    name="secure-host",
    username="admin",
    password=Variable(key="host_password"),
)

License

This software is licensed under the Apache 2.0 license. See the LICENSE file for details.

[!NOTE] This library was generated using copier from the Base Python Project Template repository.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

airflow_balancer-0.7.9.tar.gz (281.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

airflow_balancer-0.7.9-py3-none-any.whl (255.0 kB view details)

Uploaded Python 3

File details

Details for the file airflow_balancer-0.7.9.tar.gz.

File metadata

  • Download URL: airflow_balancer-0.7.9.tar.gz
  • Upload date:
  • Size: 281.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for airflow_balancer-0.7.9.tar.gz
Algorithm Hash digest
SHA256 f12ba7c77d6ffc013fac252fef3fee5df874c697358e25dd53466334dd02ecb8
MD5 fd17cba09f15b30b3a05ccb0deaea1ab
BLAKE2b-256 6cbcfe5804ed6f7313d13abc8d5ba11c087d5d3fba19748f70af5cc69b4e64a2

See more details on using hashes here.

File details

Details for the file airflow_balancer-0.7.9-py3-none-any.whl.

File metadata

File hashes

Hashes for airflow_balancer-0.7.9-py3-none-any.whl
Algorithm Hash digest
SHA256 b0bf448b7141000427bb1cd088af30263fdc0865227197b8e437bb2e527104cd
MD5 1f250a428097390bd7e4935cf29d1cc4
BLAKE2b-256 147767f0eac53f1551460ade2ab7ea006f43cc2f4a3be6a053675e5ad56ba315

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page