【问题标题】:How to have a mix of both Celery Executor and Kubernetes Executor in Apache Airflow?如何在 Apache Airflow 中混合使用 Celery Executor 和 Kubernetes Executor?
【发布时间】:2019-10-12 17:38:36
【问题描述】:

我有多个使用 Celery Executor 的 dag,但我希望使用 Kubernetes Executor 运行一个特定的 dag。我无法推断出一个好的可靠的方法来实现这一点。

我有一个airflow.cfg,我在其中声明要使用CeleryExecutor。而且我不想更改它,因为除了一个之外,所有 dags 都确实需要它。

# The executor class that airflow should use. Choices include
# SequentialExecutor, LocalExecutor, CeleryExecutor
executor = CeleryExecutor

我的 dag 代码:

from datetime import datetime, timedelta

from airflow import DAG
from airflow.contrib.operators.kubernetes_pod_operator import \
    KubernetesPodOperator
from airflow.operators.dummy_operator import DummyOperator

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': datetime.utcnow(),
    'email': ['airflow@example.com'],
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=5)
}

dag = DAG(
    'kubernetes_sample_1', default_args=default_args)


start = DummyOperator(task_id='run_this_first', dag=dag)

passing = KubernetesPodOperator(namespace='default',
                                image="Python:3.6",
                                cmds=["Python", "-c"],
                                arguments=["print('hello world')"],
                                labels={"foo": "bar"},
                                name="passing-test",
                                task_id="passing-task",
                                get_logs=True,
                                dag=dag
                                )

failing = KubernetesPodOperator(namespace='default',
                                image="ubuntu:1604",
                                cmds=["Python", "-c"],
                                arguments=["print('hello world')"],
                                labels={"foo": "bar"},
                                name="fail",
                                task_id="failing-task",
                                get_logs=True,
                                dag=dag
                                )

passing.set_upstream(start)
failing.set_upstream(start)

我可以设置一个 if-else 条件,然后从 Airflow 获取配置的位置更改值。如果这听起来不错,请告诉我路径和文件。虽然我希望得到一个更成熟的方法,如果它存在的话。

【问题讨论】:

    标签: python python-3.x kubernetes celery airflow


    【解决方案1】:

    现在有CeleryKubernetesExecutor(看不到它是什么时候引入的),它需要设置 Celery 和 Kubernetes,但也提供了两者的功能。

    在官方文档中,他们提供了一个经验法则来决定何时值得使用它:

    我们建议您在使用时考虑使用 CeleryKubernetesExecutor 案例相遇:

    高峰期需要调度的任务数超过 您的 Kubernetes 集群可以轻松处理的规模

    一小部分任务需要运行时隔离。

    你有很多可以在 Celery 工人身上执行的小任务 但是你也有资源消耗的任务,这些任务会更好地运行 预定义的环境。

    【讨论】:

      【解决方案2】:

      我认为不可能同时使用这两个执行器。但是您可以只使用 CeleryExecutor,但使用 KubernetesPodOperator 声明资源密集型任务,解决问题的作业由 CeleryExecutor 调度/监视,并由 Kubernetes 运行以用于任务中的实际处理逻辑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-28
        • 2021-12-14
        • 2021-04-15
        • 2021-03-01
        • 2022-08-18
        • 1970-01-01
        • 2021-08-13
        相关资源
        最近更新 更多