Tips Automation/Apache Airflow
Apache Airflow
Guida pratica per installare e configurare Apache Airflow in ambiente Linux, con esempio rapido su Debian/Ubuntu orientato a uso operativo.
Prerequisiti
- host Linux aggiornato
- Python 3.10+ consigliato
- accesso shell con permessi sudo
- database SQLite (test) o PostgreSQL (produzione)
Installazione base (standalone, test/lab)
sudo apt update sudo apt install -y python3 python3-venv python3-pip mkdir -p ~/airflow-home cd ~/airflow-home python3 -m venv .venv source .venv/bin/activate export AIRFLOW_HOME=~/airflow pip install --upgrade pip setuptools wheel pip install "apache-airflow==2.10.5" --constraint "https://raw.githubusercontent.com/apache/airflow/constraints-2.10.5/constraints-3.10.txt" airflow standalone
Dopo il primo avvio, apri la UI su porta 8080 e salva le credenziali mostrate in output.
Configurazione essenziale
Variabili ambiente
export AIRFLOW_HOME=~/airflow export AIRFLOW__CORE__LOAD_EXAMPLES=False export AIRFLOW__WEBSERVER__EXPOSE_CONFIG=False
Percorso DAG
Per default i DAG vivono in:
$AIRFLOW_HOME/dags
Esempio DAG minimo
Crea file `$AIRFLOW_HOME/dags/hello_airflow.py`:
from datetime import datetime
from airflow import DAG
from airflow.operators.bash import BashOperator
with DAG(
dag_id="hello_airflow",
start_date=datetime(2025, 1, 1),
schedule="0 * * * *",
catchup=False,
tags=["tips", "automation"],
):
hello = BashOperator(
task_id="hello",
bash_command="echo 'Airflow operativo su GazziNet'"
)
Avvio come servizio (systemd, esempio)
Per ambienti persistenti conviene separare scheduler e webserver.
airflow-webserver.service
[Unit] Description=Airflow Webserver After=network.target [Service] User=airflow Group=airflow Environment="AIRFLOW_HOME=/opt/airflow" ExecStart=/opt/airflow/.venv/bin/airflow webserver Restart=always [Install] WantedBy=multi-user.target
airflow-scheduler.service
[Unit] Description=Airflow Scheduler After=network.target [Service] User=airflow Group=airflow Environment="AIRFLOW_HOME=/opt/airflow" ExecStart=/opt/airflow/.venv/bin/airflow scheduler Restart=always [Install] WantedBy=multi-user.target
Sicurezza minima consigliata
- non esporre Airflow direttamente su internet senza reverse proxy
- usare HTTPS e autenticazione forte
- separare credenziali in variabili/profili dedicati
- limitare permessi utente di servizio
Troubleshooting rapido
- DAG non visibile: controlla path, permessi e parsing nel log scheduler
- task in errore: verifica connessioni, variabili, timeout e retry
- webserver lento: valuta executor e risorse host