版权声明:本文为博主原创文章,转载请注明来源。 https://blog.csdn.net/Crazy__Hope/article/details/83410340
airflow的安装步骤
本文使用的安装环境是python3, 建议单独设置虚拟环境进行安装。airflow server端目前只支持Linux
1. 设置airflow的家目录位置
在/etc/profile添加如下代码
export AIRFLOW_HOME=~/airflow
export SLUGIFY_USES_TEXT_UNIDECODE=yes
修改完毕,在shell中执行 source /etc/profile
2. 安装airflow
pip install apache-airflow
3. 初始化airflow
airflow initdb
################################ 可忽略 ###########################################
初始化以后,默认使用sqlite和SequentialExecutor,无法并行化任务
airflow webserver 启动web页面,默认端口8080 直接访问 ip:8080即可观看效果
airflow scheduler 启动调度
单纯的玩玩还是可以的,安装配置到此为止
不过线上更推荐使用LocalExecutor或者CeleryExecutor
4. mysql服务器配置
创建airflow的数据库
create database airflow charset=utf8;
修改mysql服务器的配置文件 my.cnf,在[mysqld]下添加如下参数
explicit_defaults_for_timestamp=1
至于为什么设置,官方原解释说明: We rely on more strict ANSI SQL settings for MySQL in order to have sane defaults
修改完毕,重启mysql服务器
service mysqld restart
5. airflow建议两种运行方案 LocalExecutor或者CeleryExecutor
-
5.1 LocalExecutor 配置 (单机版调度)
修改airflow.cfg配置文件
executor = LocalExecutor
sql_alchemy_conn = mysql://user:[email protected]:3306/airflow #连接mysql的地址
-
5.2 CeleryExecutor 配置(可实现分布式调度)
# 安装必要的包
pip install apache-airflow[celery]
pip install apache-airflow[redis]
pip install redis
# 修改airflow.cfg配置文件
executor = CeleryExecutor
sql_alchemy_conn = mysql://user:[email protected]:3306/airflow #连接mysql的地址
broker_url=redis://192.168.115.101:6379/0
result_backend = db+mysql://user:[email protected]:3306/airflow
6. 再次初始化数据库
airflow initdb
7. 启动web服务和调度服务
airflow webserver
airflow scheduler
airflow worker # CeleryExecutor 执行这个, LocalExecutor不用执行
8. 添加web认证
- 安装web认证包
pip install apache-airflow[password]
- 修改airflow.cfg配置文件
[webserver]
authenticate = True
auth_backend = airflow.contrib.auth.backends.password_auth
- 添加账号
# 启动一个python命令窗
>>> import airflow
>>> from airflow import models, settings
>>> from airflow.contrib.auth.backends.password_auth import PasswordUser
>>> user = PasswordUser(models.User())
>>> user.username = 'new_user_name'
>>> user.email = '[email protected]'
>>> user.password = 'set_the_password'
>>> session = settings.Session()
>>> session.add(user)
>>> session.commit()
>>> session.close()
>>> exit()
- 重启web即可
参考airflow官方文档
[1]: https://airflow.incubator.apache.org/