背景说明

近期,我负责的公司项目增加了大量的定时任务,维护的难度大量的增加,产生了开发一个监控平台的需求。鉴于公司的技术栈,最后选型了airflow作为平台框架。将搭建过程进行简单的记录,防止以后继续踩坑。

部署前提条件

  1. 已安装docker(版本不限)
  2. 数据库postgresql(mysql、oracle也可以了,只要下载对应的驱动包)

airflow docker镜像选型

镜像的选择可以通过docker search搜索选择自己心仪的镜像,但是看到除了apache-airflow官方镜像其他的stars都太少了,但是拉了一下官方镜像太大了,最后还是决定自己去github上翻一番,最后发现了一个很好的打包好的镜像:
https://github.com/puckel/docker-airflow.

镜像代码拉取

使用 git clone 命令将代码拉取到本地

git clone git@github.com:puckel/docker-airflow.git

镜像编译

刚开始,我自己编译了遍看了一下,发现这个确实简约,基于python:3.7-slim-buster原始镜像进行改造的,镜像进去后除了python、airflow、ls等命令其他的啥也没安装。所以需要我们自己先提前修改一下Dockerfile,重新打包一下镜像.

  1. 增加常用工具curl、vim、openssh-server
    在这里插入图片描述
  2. 设置sshd启动,这个要在USER airflow命令之前,毕竟是用root用户安装的嘛
RUN /etc/init.d/ssh start
  1. 创建ssh私钥和公钥,这个就要在USER airflow命令之后了,因为以后是用airflow用户下运行嘛(其实这个功能不是必须的,因为定时任务全都是在其他服务器,所以需要使用airflow的SSHoperator)
RUN ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
  1. 因为airflow是采用的pip安装方式,所以我们要安装必要的airflow包,删掉暂时没用的其他包,因为如果都安装的话就太大了。
&& pip install apache-airflow[crypto,postgres,ssh${AIRFLOW_DEPS:+,}${AIRFLOW_DEPS}]==${AIRFLOW_VERSION} \
  1. airflow还配置了script/entrypoint.sh脚本,用来安装和配置额外的东西,可以看到里面配置了requirements.txt,还有数据库,所以也需要把里面的数据库设置注释掉,要不然config/airflow.cfg数据库配置不起效。
# Other executors than SequentialExecutor drive the need for an SQL database, here PostgreSQL is used
#if [ "$AIRFLOW__CORE__EXECUTOR" != "SequentialExecutor" ]; then
#  # Check if the user has provided explicit Airflow configuration concerning the database
	......
	......
#  wait_for_port "Postgres" "$POSTGRES_HOST" "$POSTGRES_PORT"
#fi
  1. 接下来开始编译
docker build -t airflow -f Dockerfile .

配置airflow.cfg

  1. 配置executor 参数为LocalExecutor
executor = LocalExecutor
  1. 配置时区为上海
default_timezone = Asia/Shanghai
  1. 配置数据库postgresql
sql_alchemy_conn = postgresql://xxx:xxx@xxx/pid
  1. 设置默认不加载airflow example
load_examples = False
  1. 修改DAG自动检测时长为10s,默认是0,毕竟太频繁了压力太大
min_file_process_interval = 10
  1. 修改scheduler工作线程
max_threads = 5

配置docker-compose-LocalExecutor.yml

不多说直接上代码,覆盖

version: '3.7'
services:
    webserver:
        image: airflow:latest
        restart: always
        environment:
            - LOAD_EX=n
            - EXECUTOR=Local
        logging:
            options:
                max-size: 10m
                max-file: "3"
        volumes:
            - ./dags:/usr/local/airflow/dags
            - ./script/entrypoint.sh:/entrypoint.sh
            - ./config/airflow.cfg:/usr/local/airflow/airflow.cfg
            - ./logs:/usr/local/airflow/logs
            # - ./plugins:/usr/local/airflow/plugins
        ports:
            - "8080:8080"
        tty: true
    scheduler:
        image: airflow:latest
        restart: always
        depends_on:
            - webserver
        volumes:
            - ./dags:/usr/local/airflow/dags
            - ./script/entrypoint.sh:/entrypoint.sh
            - ./config/airflow.cfg:/usr/local/airflow/airflow.cfg
            - ./logs:/usr/local/airflow/logs
            # - ./plugins:/usr/local/airflow/plugins
        environment:
            - LOAD_EX=n
            - EXECUTOR=Local
        tty: true

启动镜像

docker-compose -f docker-compose-LocalExecutor.yml up -d

查看镜像运行情况

docker ps

一般等十几秒左右airflow会运行起来,浏览器访问http://localhost:8080即可

增加用户管理模块

如果有用户管理模块的需求,也可以有一个简单的配置,很方便的拥有了这个模块

  1. 在airflow.cfg中修改以下参数:
[webserver]
security = Flask AppBuilder
secure_mode = True
rbac=True
  1. airflow.cfg删除掉authenticate和auth_backend配置参数,原因是和上面的配置不共存

  2. 重新初始化数据库,进入到webserver容器内,执行 airflow resetdb

airflow resetdb
  1. 在webserver容器内创建admin超级管理员用户
airflow create_user --lastname user --firstname admin --username admin --email admin_user@mail.com --role Admin --password 123456
  1. 在浏览器中刷新一下系统,就会发现多了一个security用户管理模块,棒棒哒。

需要记录的坑

  1. airflow是ubuntu镜像,在运行中,会有日志文件产生,就涉及到创建文件的权限问题,因为宿主目录和镜像中的目录存在映射,不知道为啥,有在创建日志文件的时候没有权限,后来测试将宿主项目权限改成ubuntu就可以了,后期在看看为啥吧:
chown -R ubuntu:ubuntu airflow
  1. docker build镜像失败问题
    原因是docker编译镜像需要去外网拉取包,而我们国家的墙很高,偶尔才能爬过去一次,给docker设置一下系统代理,我用的mac,其他系统也一样,参考如下,设置好后,重启docker,重新build快的飞起
    在这里插入图片描述

暂时先更新这些,其他的有时间会继续补充。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐