美文网首页我爱编程
airflow安装和配置

airflow安装和配置

作者: 醉清风_55a1 | 来源:发表于2018-05-24 16:37 被阅读954次

Airflow是Airbnb开源的一个用Python写就的工作流管理平台(workflow management platform)

名称 版本
Centos 7.4
Ubuntu 18.04
Python 2.7

1. 创建linux用户(非必须)

sudo adduser airflow

2. 创建airflow的Home目录

该目录包含了airflow的配置文件和默认的日志文件

mkdir airflow

3. 配置环境变量

export AIRFLOW_HOME=~/airflow
# 非root用户airflow将安装在~/.local目录
export PATH=~/.local/bin:$PATH

4. 安装mysql客户端

airflow 默认使用Sqlite作为数据库,如果不需要mysql,也可以不安装客户端

#centos
yum -y install mysql-devel 
#ubuntu
sudo apt-get install libmysqlclient-dev

5. 修复sqlalchemy的兼容性BUG

airflow-1.9.0 和sqlalchemy1.2.7无法兼容,导致用户认证和密码设置出现异常,后期版本可能会修复。

pip install 'sqlalchemy>=1.1.15, <1.2.0'

6. 安装airflow和相关的组件

pip install apache-airflow
pip install apache-airflow[celery,crypto,mysql,password,redis]

7. 数据库配置

#创建库表和用户
create database airflow default charset utf8mb4 collate utf8mb4_general_ci;
create user airflow@'%' identified by 'airflow';
grant all on airflow.* to airflow@'%'; 
flush privileges;

8. airflow配置文件修改

a. 产生配置文件

#首次运行该命令之后才会产生配置文件
airflow version

b. 修改数据库配置

cd $AIRFLOW_HOME
vi airflow.cfg
#修改数据库配置
sql_alchemy_conn = mysql://airflow:airflow@localhost:3306/airflow

c. 设置celery broker为redis

redis安装教程请百度

broker_url = redis://localhost:6379/0 
celery_result_backend = redis://localhost:6379/0
executor = CeleryExecutor 

d. 设置celery broker为Rabbitmq(非必须)

Rabbitmq安装教程请百度

broker_url = amqp://ct:152108@localhost:5672/ct_airflow 
celery_result_backend = amqp://ct:152108@localhost:5672/ct_airflow
executor = CeleryExecutor 

e. 创建web管理用户

初始化数据库

airflow initdb

在python中运行

import airflow  
from airflow import models, settings  
from airflow.contrib.auth.backends.password_auth import PasswordUser  
user = PasswordUser(models.User())  
user.username = 'afuser'  
user.email = 'afuser@example.com'  
user.password = 'afuser'  
session = settings.Session()  
session.add(user)  
session.commit()  
session.close()  
exit()  

启用访问认证

> vim /usr/local/airflow/airflow.cfg
[webserver]
authenticate = true
auth_backend = airflow.contrib.auth.backends.password_auth

注意:必须是在webserver模块底下的配置。

9. airflow 服务管理

a. 安装supervisor

pip install supervisor

#生成supervisor配置文件
echo_supervisord_conf > $AIRFLOW_HOME/supervisord.conf

b. 配置airflow

vi $AIRFLOW_HOME/supervisord.conf
[program:airflow_web]  
command=airflow webserver -p 8080  
autostart=false
stopasgroup=true
killasgroup=true

[program:airflow_scheduler]  
command=airflow scheduler
autostart=false
stopasgroup=true
killasgroup=true

[program:airflow_worker]
command=airflow worker
autostart=false
stopasgroup=true
killasgroup=true

c. 运行supervisor

supervisord -c $AIRFLOW_HOME//supervisord.conf

d. 管理airflow

supervisorctl start airflow_scheduler
supervisorctl start airflow_worker
supervisorctl start airflow_web  
supervisorctl stop airflow_web  
supervisorctl stop airflow_scheduler
supervisorctl stop airflow_worker
supervisorctl restart airflow_web
supervisorctl stop all

相关文章

网友评论

    本文标题:airflow安装和配置

    本文链接:https://www.haomeiwen.com/subject/inmsjftx.html