如何在 docker 撰写文件中添加气流变量?

How to add airflow variables in docker compose file?

我有一个 docker compose 文件,它启动本地气流实例,如下所示:

version: '3.7'
services:
    postgres:
        image: postgres:9.6
        environment:
            - POSTGRES_USER=airflow
            - POSTGRES_PASSWORD=airflow
            - POSTGRES_DB=airflow
        logging:
            options:
                max-size: 10m
                max-file: "3"

    webserver:
        image: puckel/docker-airflow:1.10.6
        restart: always
        depends_on:
            - postgres
        environment:
            - LOAD_EX=n
            - EXECUTOR=Local
            - FERNET_KEY=46BKJoQYlPPOexq0OhDZnIlNepKFf87WFwLbfzqDDho=
        logging:
            options:
                max-size: 10m
                max-file: "3"
        volumes:
            - ./dags:/usr/local/airflow/dags
            - ${HOME}/.aws:/usr/local/airflow/.aws
        ports:
            - "8080:8080"
        command: webserver
        healthcheck:
            test: ["CMD-SHELL", "[ -f /usr/local/airflow/airflow-webserver.pid ]"]
            interval: 30s
            timeout: 30s
            retries: 3

我想添加一些底层 dag 使用的 Airflow 变量,例如:CONFIG_BUCKET。 我已将它们添加为 AIRFLOW_VAR_CONFIG_BUCKET=s3://foo-bucket 在网络服务器的环境部分,但它似乎不起作用。有什么想法可以实现吗?

如果您将名为 AIRFLOW_VAR_CONFIG_BUCKET 的环境变量添加到 environment: 下的列表中,Airflow 应该可以访问它。听起来你做得对。

需要注意两点:

  • 通过环境变量设置的变量(和连接)在 Airflow UI 中可见。您可以通过在代码中执行 Variable.get("config_bucket") 来测试它们是否存在。
  • Airflow scheduler/worker(取决于 Airflow 执行器)需要在 运行 任务时访问变量。不需要向网络服务器添加变量。

您不应将变量添加到网络服务器,而应添加到调度程序。如果您使用的是 LocalExecutor,则任务在 Scheduler 的上下文中是 运行。

实际上你真正应该做的是将所有容器的所有环境变量设置为相同(这里有解释 https://airflow.apache.org/docs/apache-airflow/stable/configurations-ref.html

Use the same configuration across all the Airflow components. While each component does not require all, some configurations need to be same otherwise they would not work as expected. A good example for that is secret_key which should be same on the Webserver and Worker to allow Webserver to fetch logs from Worker.

您可以通过多种方式完成此操作 - 只需阅读有关 https://docs.docker.com/compose/environment-variables . You can also see the "Quick start" docker compose from Airflow docs where we used anchors - which is bit more sphisticated way https://airflow.apache.org/docs/apache-airflow/stable/start/docker.html

的 docker-compose 文档

请注意,“快速入门”应该只是灵感,它离生产设置还很远,如果你想制作自己的 docker compose,你需要真正深入了解 docker 撰写 - 正如我们文档中的注释中所警告的那样。