繁体   English   中英

如何在apache airflow DAG中运行独立requirements.txt文件

[英]How to run independent requirements.txt file in apache airflow DAG

我有多个 aiflow dags 在 composer 环境中运行。 现在我没有机会安装任何额外的 python 包,除了 airflow 给出的包。但现在我想为两个不同的 DAG 安装 beautifulsoup 和其他打包程序。 我怎样才能做到这一点?

第一种方法:

def a():
   print("Response")
   return True

response = PythonVirtualenvOperator(
    task_id = "response",
    requirements=["lxml==4.9.1","beautifulsoup4==4.11.1"],
    python_callable = a,
    dag=dag,
    provide_context=True
) 

它安装包,但不执行代码。 简直是逃课。 我想知道后端发生了什么以及我将如何执行代码。

第二种方法:

virtual_classic = BashOperator(
    task_id="virtual_classic",
    bash_command="bash /home/airflow/gcs/dags/test_req/requirements.txt",
    dag=dag
)

在这里,我试图从需求文件中安装所有包,然后尝试运行所需的必要代码。 但是这里我不确定如何通过 airflow 运营商安装这个文件? 在此感谢您的宝贵建议。

第三种方法:

gcloud composer environments update ENVIRONMENT_NAME \
--location LOCATION \
 --update-pypi-packages-from-file gs://composer_bucket/requirements.txt

错误:

gcloud 崩溃(类型错误):无法在类似字节的 object 上使用字符串模式

请让我知道为三个中的独立 dag 安装 requirement.txt 文件的最佳方法。

默认情况下,对于AirflowCloud Composerpypi包安装在全局所有 DAG 的机器中。

如果你想为每个 DAG 或任务提供版本和包,你可以使用Kube.netesPodOperator

此运算符允许使用自定义Docker图像。 在此图像中,您可以安装和隔离此任务和Python程序所需的所有包。

operator 中使用的Docker镜像需要在同一个项目的Container registry中发布(我不确定是否可以使用Artifact Registry )。

文档中此运算符的示例:

kubernetes_min_pod = KubernetesPodOperator(
        # The ID specified for the task.
        task_id="pod-ex-minimum",
        # Name of task you want to run, used to generate Pod ID.
        name="pod-ex-minimum",
        # Entrypoint of the container, if not specified the Docker container's
        # entrypoint is used. The cmds parameter is templated.
        cmds=["echo"],
        # The namespace to run within Kubernetes, default namespace is
        # `default`. In Composer 1 there is the potential for
        # the resource starvation of Airflow workers and scheduler
        # within the Cloud Composer environment,
        # the recommended solution is to increase the amount of nodes in order
        # to satisfy the computing requirements. Alternatively, launching pods
        # into a custom namespace will stop fighting over resources,
        # and using Composer 2 will mean the environment will autoscale.
        namespace="default",
        # Docker image specified. Defaults to hub.docker.com, but any fully
        # qualified URLs will point to a custom repository. Supports private
        # gcr.io images if the Composer Environment is under the same
        # project-id as the gcr.io images and the service account that Composer
        # uses has permission to access the Google Container Registry
        # (the default service account has permission)
        image="gcr.io/gcp-runtimes/ubuntu_18_0_4",
    )

你有不同的选择来在隔离环境中运行你的 dags:

  1. 在单独的 python 虚拟环境中运行任务:类似于您的第一种方法,但在每次运行时安装需求不是一个好主意,您可以手动创建环境(或使用 CI/CD),并安装您需要的所有包需要两个 dag,然后您可以使用 bash 运算符并调用命令/path/to/<venv1 or venv2>/bin/python some_script.py
  2. 使用DockerOperator在 VM 上将它们作为 docker 容器运行:在这种情况下,任务将在隔离的容器中执行,您只需要为每个 dag 创建 2 个 docker 图像和所需的包,将它们推送到 docker 注册表(ex Google Docker注册表),然后为每个 dag 提供相应图像的标签。
  3. 使用Kube.netesPodOperator将它们作为 Kube.netes pod 运行:在这种情况下,任务将在 cloud composer 集群(这是一组在 GKE 集群上运行的 pod)之外执行,并且它们将作为单独的 pod 运行。 您需要按照第二个选项中的说明创建映像并将它们推送到 docker 注册表,然后配置 Airflow 以访问您的 GKE 集群( 这里是官方文档)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM