Airflow【部署 01】Airflow官网Quick Start实操(一篇学会部署Airflow)_apache airflow教程


本次使用虚拟环境进行安装:



创建并切换到airflow虚拟环境

conda create -n airflow python=3.8
conda activate airflow


创建约束文件`airflowInstall.sh`添加官网给出的内容:



AIRFLOW_VERSION=2.7.2
PYTHON_VERSION=“ ( p y t h o n − − v e r s i o n ∣ c u t − d " " − f 2 ∣ c u t − d " . " − f 1 − 2 ) " C O N S T R A I N T _ U R L = " h t t p s : / / r a w . g i t h u b u s e r c o n t e n t . c o m / a p a c h e / a i r f l o w / c o n s t r a i n t s − (python --version | cut -d " " -f 2 | cut -d "." -f 1-2)" CONSTRAINT\_URL="https://raw.githubusercontent.com/apache/airflow/constraints- (pythonversioncutd""f2∣cutd"."f12)"CONSTRAINT_URL="https://raw.githubusercontent.com/apache/airflow/constraints{AIRFLOW_VERSION}/constraints- P Y T H O N _ V E R S I O N . t x t " p i p i n s t a l l " a p a c h e − a i r f l o w = = {PYTHON\_VERSION}.txt" pip install "apache-airflow== PYTHON_VERSION.txt"pipinstall"apacheairflow=={AIRFLOW_VERSION}” --constraint “${CONSTRAINT_URL}”


执行文件即可。也可以使用`pip`进行安装:



pip install “apache-airflow==2.7.2”


查询版本:



airflow version


## 3.启动单机版


### 3.1 快速启动


该命令初始化数据库、创建用户并启动所有组件。



airflow standalone

启动成功标志

standalone | Airflow is ready
standalone | Login with username: admin password: ZUUNtd9ppZZTQuqy
standalone | Airflow Standalone is for development purposes only. Do not use this in production!


### 3.2 分步骤启动


如果您想手动运行Airflow的各个部分,而不是使用一体化的独立命令,您可以运行:


1. 该命令用于执行数据库迁移。在使用 Airflow 之前,你需要初始化数据库结构。db migrate 命令会根据你的配置文件创建数据库表格,以便存储任务调度、任务实例、DAG(Directed Acyclic Graph,有向无环图)等信息。运行后的信息:



airflow db migrate


初始化数据库后的输出信息:



DB: sqlite:root/airflow/airflow.db
Performing upgrade to the metadata database sqlite:root/airflow/airflow.db
[2023-10-19T14:21:37.687+0800] {migration.py:213} INFO - Context impl SQLiteImpl.
[2023-10-19T14:21:37.688+0800] {migration.py:216} INFO - Will assume non-transactional DDL.
[2023-10-19T14:21:37.690+0800] {db.py:1620} INFO - Creating tables
INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
WARNI [unusual_prefix_911b7e3bced5159145cb88698226ecde6e08c7be_example_kubernetes_executor] The example_kubernetes_executor example DAG requires the kubernetes provider. Please install it with: pip install apache-airflow[cncf.kubernetes]
WARNI [unusual_prefix_008dd7238a3787d68b758fe337b9f566c5014ba3_tutorial_taskflow_api_virtualenv] The tutorial_taskflow_api_virtualenv example DAG requires virtualenv, please install it.
WARNI [unusual_prefix_db2b4614a7fb1ba43706f0a1f2be91e808476bfa_example_python_operator] The virtalenv_python example task requires virtualenv, please install it.
WARNI [unusual_prefix_5624127e5a8d9c88ab5a41d62ecf92869309dd74_example_local_kubernetes_executor] Could not import DAGs in example_local_kubernetes_executor.py
Traceback (most recent call last):
File “/root/anaconda3/envs/airflow/lib/python3.8/site-packages/airflow/example_dags/example_local_kubernetes_executor.py”, line 37, in
from kubernetes.client import models as k8s
ModuleNotFoundError: No module named ‘kubernetes’
WARNI [unusual_prefix_5624127e5a8d9c88ab5a41d62ecf92869309dd74_example_local_kubernetes_executor] Install Kubernetes dependencies with: pip install apache-airflow[cncf.kubernetes]
WARNI [unusual_prefix_f16a910b73b9eed67cbb95faa136bc7fd6c14eb6_workday] Could not import pandas. Holidays will not be considered.
Database migrating done!


2. 该命令用于创建 Airflow 的用户。在这个例子中,它创建了一个名为 “admin” 的用户,具有管理员角色(Admin),并提供了一些用户信息,如名字、姓氏、电子邮件等。



airflow users create
–username admin
–firstname Peter
–lastname Parker
–role Admin
–email spiderman@superhero.org

测试

airflow users create
–username test
–firstname te
–lastname st
–role Admin
–email testman@superhero.org

输入密码并确认后即可创建新用户

Password:
Repeat for confirmation:
[2023-10-19T15:08:26.070+0800] {manager.py:211} INFO - Added user %s
User “test” created with role “Admin”


3. 该命令启动 Airflow 的 Web 服务器。Web 服务器提供了一个用户界面,你可以通过浏览器访问。–port 8080 选项指定了 Web 服务器监听的端口号,这里是 8080。你可以通过访问 http://localhost:8080 来打开 Airflow Web UI。



用于启动

airflow webserver --port 8080 -D


4. 该命令启动 Airflow 的调度器。调度器负责按照你的 DAG(工作流)定义定期运行任务。它会检查定义的任务调度时间,然后触发相应的任务实例。调度器是 Airflow 中关键的组件之一,确保任务按照计划执行。



airflow scheduler -D


-D: 表示以守护进程(daemon)模式运行。以守护进程模式运行意味着该进程将在后台持续运行,而不占用当前终端。


### 3.3 启动后


在运行这些命令后,Airflow将创建`$AIRFLOW_HOME`文件夹,并创建默认值为`Airflow .cfg`的文件,这将使您快速运行。您可以使用环境变量覆盖默认值,请参见配置参考:<https://airflow.apache.org/docs/apache-airflow/stable/configurations-ref.html>。您可以在`$AIRFLOW_HOME/airflow.cfg`中检查该文件,或者通过`Admin->Configuration`菜单中的UI检查该文件。如果由systemd启动webserver的PID将存储在`$AIRFLOW_HOME/airflow-webserver.pid`或`/run/airflow/webserver.pid`文件中。


![files-0.jpg](https://img-blog.csdnimg.cn/img_convert/112cf43ed846209ed9439236025529f6.jpeg)


### 3.4 服务启动停止脚本


感谢 <https://blog.csdn.net/weixin_45417821/article/details/128729413> 的分享,脚本`airflow-service.sh`内容:



#!/bin/bash
case $1 in
“start”){
echo " --------start airflow-------"
conda activate airflow;airflow webserver -p 8080 -D;airflow scheduler -D;conda deactivate
};;
“stop”){
echo " --------stop airflow-------"
ps -ef | egrep ‘scheduler|airflow-webserver’ | grep -v grep | awk ‘{print $2}’ | xargs kill -15
};;
esac


脚本的执行环境为`非虚拟环境`也就是Linux本Lin,为何要使用`source`进行执行小伙伴儿们可以自行学习啊:



启动

source ./airflow-service.sh start

停止

source ./airflow-service.sh stop


## 4.访问


### 4.1 登录


在浏览器中访问localhost:8080,并使用终端显示的管理员帐户详细信息登录。


![login-0.jpg](https://img-blog.csdnimg.cn/img_convert/868d6df636f000696173faa9adc0b153.jpeg)


### 4.2 测试


在主页中启用`example_bash_operator`DAG。


![example-0.jpg](https://img-blog.csdnimg.cn/img_convert/5e99467196b994a602e866fa484d3f55.jpeg)  
 页面的两条信息说明:


* 开箱即用,Airflow使用SQLite数据库,由于使用此数据库后端不可能实现并行化,因此您应该很快就能适应该数据库。它与SequentialExecutor一起工作,后者只按顺序运行任务实例。虽然有很多限制,但它允许您快速启动和运行,并了解UI和命令行实用程序。
* 当您将Airflow扩展并部署到生产环境中时,您还需要从我们在这里使用的独立命令转移到单独运行组件。您可以在生产部署中了解更多信息:<https://airflow.apache.org/docs/apache-airflow/stable/administration-and-deployment/production-deployment.html>。


任务的详情:


![example-1.jpg](https://img-blog.csdnimg.cn/img_convert/610afadaad1c0ffda3e37844bf035f17.jpeg)  
 下面是几个将触发几个任务实例的命令。当您运行下面的命令时,您应该能够在example\_bash\_operator DAG中看到作业的状态变化。



1.run your first task instance

airflow tasks test example_bash_operator runme_0 2015-01-01

执行成功标志

[2023-10-19T14:15:55.666+0800] {taskinstance.py:1400} INFO - Marking task as SUCCESS. dag_id=example_bash_operator, task_id=runme_0, execution_date=20150101T000000, start_date=20231019T061426, end_date=20231019T061555

2.run a backfill over 2 days

airflow dags backfill example_bash_operator
–start-date 2015-01-01
–end-date 2015-01-02

执行成功标志

[2023-10-19T14:17:59.128+0800] {backfill_job_runner.py:412} INFO - [backfill progress] | finished run 2 of 2 | tasks waiting: 0 | succeeded: 10 | running: 0 | failed: 0 | skipped: 4 | deadlocked: 0 | not ready: 0
[2023-10-19T14:17:59.136+0800] {backfill_job_runner.py:971} INFO - Backfill done for DAG <DAG: example_bash_operator>. Exiting.


## 5.更新日志


* 2024-02-21 修改启动脚本`airflow-service.sh`内容,及调用脚本。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值