【Airflow】Apache Airflow ~ 環境構築編 ~

■ はじめに

Amazon Managed Workflows for Apache Airflow (MWAA) っていう
AWSサービスがあるのだが、その元となっている Apache Airflow を
気軽に触ってみたいので、ローカル上に環境を構築してみる

目次

【1】Apache Airflow
【2】環境構築する上での注意点
 1)Windows 上では構築できない
 2)Python versionを確認する
 補足:DBについて
【3】構築手順 - 手動インストール編
 1)前準備
 2)インストールする
 3)DB初期化
 4)ユーザ作成
 5)サーバ起動
 6)スケジューラ起動
 7)動作確認
【4】トラブルシュート
【5】設定ファイル「/root/airflow/airflow.cfg」
 1)dags_folder

【1】Apache Airflow

* ワークフローエンジン
* Python製
=> 詳細は、以下の関連記事を参照のこと。

Apache Airflow ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2021/09/28/135510

【2】環境構築する上での注意点

1)Windows 上では構築できない

* (純粋な)Windows 上では構築できない
 => pip install を使うので、ひょっとしたらいけるかなーっと試して
  インストールは成功するが、「airflow db init」時に以下のエラーで
  ダメだった。。。
 => WSL2などを使って、仮想上のLinux(Ubuntu)に構築してみる

https://dk521123.hatenablog.com/entry/2020/12/08/165505

Windows上で「airflow db init」実行時のエラー内容

airflow db init
WARNING:root:OSError while attempting to symlink the latest log directory
Traceback (most recent call last):
・・・略・・・
ModuleNotFoundError: No module named 'termios'

2)Python versionを確認する

* Pythonが古い(3.5とか)とインストール時にエラーになるかも、、、

公式サイト より一部抜粋
https://airflow.apache.org/docs/apache-airflow/stable/installation.html

Python: 3.6, 3.7, 3.8
Databases:
 PostgreSQL: 9.6, 10, 11, 12, 13
 MySQL: 5.7, 8
 SQLite: 3.15.0+
Kubernetes: 1.18.15 1.19.7 1.20.2

補足:DBについて

* 本番環境として、PostgreSQL を選ぶのが一番良い?
* 今回は、練習なので、何も指定せず(SQLite?)に構築する

https://airflow.apache.org/docs/apache-airflow/stable/installation.html

より抜粋

MySQL

Note:
MySQL 5.x versions are unable to or have limitations with running multiple schedulers
 – please see: Scheduler.
MariaDB is not tested/recommended.

MySQL 5.xは、複数のスケジューラを実行する制限は持つことはできません。
スケジューラを見てください。
MariaDBはテストされていなく、お勧めできません。

SQLite

Note:
SQLite is used in Airflow tests.
Do not use it in production.
We recommend using the latest stable version of SQLite for local development.

SQLiteはAirflowテストとして使ってください。
SQLiteを本番環境では使わないでください。
ローカル開発環境のために最新で安定したバージョンのSQLiteを使うことを
勧めています。

【3】構築手順 - 手動インストール編

以下の公式サイトの手順を参考にするといい

https://airflow.apache.org/docs/apache-airflow/stable/start/local.html

* Docker で構築したい場合、以下の関連記事を参照のこと。

https://dk521123.hatenablog.com/entry/2021/10/11/134840

構築環境

* OS : Ubuntu 20.04.2 LTS
* Python : Python 3.8.10
 => なければ「sudo apt-get install python-is-python3 python3-distutils python3-pip」

1)前準備

# 更新
sudo apt upgrade

# pip の更新
pip install --upgrade pip

2)インストールする

# Apache Airflowのインストール
sudo pip install apache-airflow
# 固定したバージョン(v2.0.2=AWS MWAA)で
# インストールする場合
# sudo pip install apache-airflow==2.0.2

# ERROR: xxxx 10.6.0 has requirement xxxx<3.0.0,>=2.6.0, but you'll have xxxxx 2.3.1 which is incompatible.
# ってエラーが出てきた場合
# sudo pip uninstall xxxxx
# してから、
# sudo pip install 'xxxx<3.0.0,>=2.6.0'

# インストール後に確認の意味でVersion確認
#  => 今回「2.1.2」が表示
airflow version

3)DB初期化

airflow db init

4)ユーザ作成

airflow users create \
    --username admin \
    --firstname YourFirstName \
    --lastname YourLastName \
    --role Admin \
    --email YourEmail@gmail.com

Password:<パスワード入力>
Repeat for confirmation:<再度パスワード入力>
Admin user admin created

5)サーバ起動

airflow webserver --port 8080

6)スケジューラ起動

# 別のターミナルを開いて、、、
airflow scheduler

7)動作確認

* ブラウザで以下にアクセス
 => ログイン画面が表示されるので「admin / 入力したパスワード」でログインする

http://localhost:8080

【4】トラブルシュート

* 以下の関連記事を参照のこと。

Apache Airflow に関するトラブル
https://dk521123.hatenablog.com/entry/2021/10/03/000000

【5】設定ファイル「/root/airflow/airflow.cfg」

https://airflow.apache.org/docs/apache-airflow/stable/configurations-ref.html
https://github.com/brunocfnba/Kubernetes-Airflow/blob/master/airflow.cfg

1)dags_folder

* DAGのパスを指定

https://airflow.apache.org/docs/apache-airflow/stable/configurations-ref.html#dags-folder
設定例

# The folder where your airflow pipelines live, most likely a
# subfolder in a code repository
dags_folder = /root/airflow/dags

参考文献

https://benzenetarou.hatenablog.com/entry/airflow-tutorial
https://medium.com/@tomaspm/airflow-dag-deployment-with-s3-2536dc347d2d

関連記事

Apache Airflow ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2021/09/28/135510
Apache Airflow ~ 環境構築 / Docker 編 ~
https://dk521123.hatenablog.com/entry/2021/10/11/134840
Apache Airflow ~ 環境構築 / Kubernetes 編 ~
https://dk521123.hatenablog.com/entry/2023/05/13/000000
Apache Airflow ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2021/07/24/233012
Apache Airflow ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2021/07/28/234319
Apache Airflow ~ DAGの引数 ~
https://dk521123.hatenablog.com/entry/2022/01/13/101634
Apache Airflow ~ Sensor ~
https://dk521123.hatenablog.com/entry/2023/10/30/002751
Apache Airflow ~ XComs ~
https://dk521123.hatenablog.com/entry/2023/10/31/000133
Apache Airflow ~ 実行タイミング ~
https://dk521123.hatenablog.com/entry/2022/01/15/014005
Apache Airflow ~ CLI
https://dk521123.hatenablog.com/entry/2021/10/21/130702
Apache Airflow ~ リトライ ~
https://dk521123.hatenablog.com/entry/2021/10/10/000000
Apache Airflow ~ タイムアウト
https://dk521123.hatenablog.com/entry/2021/10/12/000000
Apache Airflow ~ Variable ~
https://dk521123.hatenablog.com/entry/2023/12/17/000000
Apache Airflow ~ Connection ~
https://dk521123.hatenablog.com/entry/2021/10/16/000454
Apache Airflow ~ あれこれ編 ~
https://dk521123.hatenablog.com/entry/2021/09/30/163020
Apache Airflow ~ 通知あれこれ編 ~
https://dk521123.hatenablog.com/entry/2021/10/06/141323
Apache Airflow ~ 通知サンプル編 ~
https://dk521123.hatenablog.com/entry/2021/10/09/000000
Apache Airflow に関するトラブル
https://dk521123.hatenablog.com/entry/2021/10/03/000000
MWAA ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2021/09/29/131101
MWAA Local ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/10/21/233404
Docker Desktop / WSL2 ~ Windows / 環境構築編 ~
https://dk521123.hatenablog.com/entry/2020/12/08/165505
Docker ~ 基本編 / docker-compose ~
https://dk521123.hatenablog.com/entry/2020/04/11/000000
Python ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2014/08/07/231242
Amazon S3AWS CLIで操作 ~
https://dk521123.hatenablog.com/entry/2017/04/01/235355