You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在另一台服务器执行Airflow任务?已搭建主从Airflow架构

解决Airflow任务指定到从服务器Worker执行的方案

1. 配置从服务器Worker监听目标队列

从服务器的Airflow Worker必须明确监听node1队列,有两种配置方式:

  • 修改从服务器的airflow.cfg配置文件,找到[celery]段,设置:
    worker_queue = node1
    
  • 启动Worker时直接通过命令指定队列(优先级高于配置文件):
    airflow celery worker -q node1
    

(注:你使用Redis作为消息中间件,说明Airflow采用CeleryExecutor,此配置是核心前提)

2. 确保主从共享元数据与消息队列

主服务器的Airflow必须和从服务器共用同一套元数据库与消息队列:

  • 主服务器airflow.cfg的[database]段,sql_alchemy_conn需指向从服务器的Postgres实例(或主从共同连接同一个Postgres)
  • 主服务器[celery]段的broker_url需指向从服务器的Redis实例(或共享Redis)
    只有这样,主服务器的Scheduler才能将任务投递到指定队列,从服务器的Worker才能接收到任务。

3. 在DAG任务中指定目标队列

编写DAG时,给需要分发到从服务器的任务添加queue='node1'参数:

from airflow import DAG
from airflow.operators.bash import BashOperator
from datetime import datetime

with DAG(
    dag_id='dispatch_to_slave',
    start_date=datetime(2024, 1, 1),
    schedule_interval='@daily',
    catchup=False
) as dag:
    run_on_slave = BashOperator(
        task_id='slave_task',
        bash_command='echo "Task executed on slave server: $(hostname)"',
        queue='node1'  # 指定任务投递到node1队列
    )

4. 验证执行结果

  • 查看从服务器Worker的启动日志,确认日志中出现Listening on queues: node1的提示
  • 触发DAG后,查看任务日志,通过日志中的主机名(hostname输出)确认任务是否由从服务器Worker执行

内容的提问来源于stack exchange,提问作者M. Mariscal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 10:27:34