1. 程式人生 > 實用技巧 >Apache Airflow部署文件(物理機版本)

Apache Airflow部署文件(物理機版本)

0.airflow架構

從開發的角度出發來看,使用Local Execultor的基礎 Airflow架構是一個絕佳的理解Apache Airflow架構的起點。

以下是airflow 主要元件的說明:

  • 元資料庫(Metadata Database): Airflow使用 SQL 資料庫 來儲存關於 資料流水線執行相關的元資料資訊。在圖片下方,元資料庫由在Airflow當中很受歡迎的Postgres來表示。Airflow也支援MySQL作為其元資料庫。

  • Web伺服器和排程器(Web ServerandScheduler): Airflow web伺服器 和 排程器 在本臺機器中是分別的程序(在這個例子中) 且和上面所提到的元資料庫進行互動。

  • 執行器(Executor)被單獨的顯示出來了,由於其通常與Airflow一同本探討,但是其並不是單獨的一個程序,它執行在 排程器之中。

  • 工作節點(Worker)是不同的經常,其與圖中的其他元件進行互動。

  • airflow.cfg是被WebServer Scheduler 及 Worker元件使用的配置檔案

  • DAGs 指的是 包含Python程式碼的 DAG檔案, 代表將要被Airflow執行的資料流水線。這些檔案的位置在Airflow配置檔案中指定,他們需要被Web Server ,Scheduler及 Worker元件訪問到。

1.依賴安裝配置

1.0 airflow使用者建立並設定密碼

使用 root 使用者執行下面的命令

useradd airflow

passwd airflow

# 輸入airflow,收到提示後再次輸入 airflow

1.1 python 3

centos7自帶的python版本為 2.7.5,需要安裝 python 3以支援 airflow的使用。

#安裝 pyenv 安裝 pyenv-virtualenv

curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash

exec"$SHELL"

#安裝 python

3.7.7

pyenv install3.7.7

#新增虛擬環境

pyenv virtualenv3.7.7airflow

#使用虛擬環境

pyenv activate airflow

#檢視當前虛擬環境的python版本

python -V

#退出當前虛擬環境

pyenv deactivate

#設定環境變數

vim /etc/profile

export PYENV_ROOT="/data/pyenv"

export PATH="$PYENV_ROOT/bin:$PATH"

eval"$(pyenv init -)"

eval"$(pyenv virtualenv-init -)"

1.2 mysql

使用 mysql 作為 airflow 的元資料庫,按照步驟安裝好mysql軟體包

# 查詢centos7自帶的 mariadb 軟體包

rpm -qa | grep mariadb

# 刪除 mariadb 相關的軟體包

rpm -e mariadb* --nodeps

# 在mysql 配置中加入 airflow需要的引數

vim /etc/my.cnf

explicit_defaults_for_timestamp=1

# 下載 mysql rpm包

wget -t0https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar

# 解壓rpm包

tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar

# 一次安裝相關mysql rpm軟體包

rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm

rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm

rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm

rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm

# 啟動 mysql 服務

systemctl start mysqld

# 檢視 mysql 服務狀態

systemctl status mysqld

# 檢視 mysql 臨時密碼

grep password /var/log/mysqld.log

# 使用 root 使用者登入

mysql -u root -p

# 設定新mysql 新密碼, 新密碼需要滿足 有字元 數字 大小寫字母

GRANT ALL PRIVILEGES ON *.* TO'root'@'%'IDENTIFIED BY'[email protected]'WITH GRANT OPTION;

# 重新整理許可權

flush privileges;

# 新建 airflow 元資料庫

CREATE DATABASE airflow CHARACTER SET utf8 COLLATE utf8_unicode_ci;

CREATE USER'airflow'IDENTIFIED BY'airflow';

GRANT ALL PRIVILEGES ON airflow.* TO'airflow';

1.3 redis

使用redis作為 Celery的broker,按照下面的命令依次安裝即可

# 依賴下載

yum -y install gcc gcc-c++ epel-release wget

# tar包下載

wget http://download.redis.io/releases/redis-5.0.4.tar.gz

# 解壓

tar xf redis-5.0.4.tar.gz

# 移動到 /usr/local/redis目錄

mv redis-5.0.4/usr/local/redis

# 進入 redis目錄

cd /usr/local/redis

# 使用 make 命令安裝 redis

make PREFIX=/usr/local/redis/ install

# 進入到utils目錄

cd utils/

# 拷貝啟動指令碼到 /etc/init.d/目錄

cp redis_init_script /etc/init.d/redis 啟動指令碼

# 修改 redis啟動指令碼

vim /etc/init.d/redis

# 需要修改的引數及命令如下

EXEC=/usr/local/redis/bin/redis-server

CLIEXEC=/usr/local/redis/bin/redis-cli

$CLIEXEC -p $REDISPORT -a123shutdown123位redis密碼,不加-a123無法使用/etc/init.d/redis stop

# 建立 /etc/redis目錄

mkdir /etc/redis

# 將redis配置檔案 拷貝到 /etc/redis目錄下

cp ../redis.conf /etc/redis/6379.conf

# 修改配置檔案

vim /etc/redis/6379.conf

bind127.0.0.1#只允許本地連線,如果需要遠端使用需要註釋掉此行或者改為0.0.0.0

daemonize yes #允許redis後臺執行,一般的都會開啟此選項

protected-mode yes #允許公網訪問redis,根據需要選擇,如果不允許公網訪問就改為no

requirepass foobared #註釋去掉,foobared為密碼,也可修改為別的值(可選,建議設定,生產環境必須改)此處先改為123

port6379#埠號,提高安全性可以修改為其他值

dir /var/lib/redis_6379 #redis資料存放路徑,預設是當前目錄下,最好修改,防止誤刪

# 設定iptables規則,允許外部訪問6379

iptables -I INPUT1-p tcp -m state --state NEW -m tcp --dport6379-j ACCEPT

# 額外操作

mkdir -p /var/lib/redis_6379

echo512>/proc/sys/net/core/somaxconn

ln -s /usr/local/redis/bin/redis-* /usr/bin/

# 啟動

/etc/init.d/redis start

2.airflow安裝

2.0 airflow 基本安裝包

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[async]==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[celery]==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[crypto]==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[devel_hadoop]==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[hdfs]==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[hive]==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[jdbc]==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[kerberos]==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[ldap]==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[mysql]==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[password]==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[presto]==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[redis]==1.10.12

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow[ssh]==1.10.12

2.1 airflow operator 使用到的 軟體包

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple impyla

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple sasl

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple thrift

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple thrift-sasl

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyHive

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspark

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apache-airflow-backport-providers-apache-spark

3.airflow配置

3.0配置 airflow.cfg檔案

# 設定 AIRFLOW_HOME 目錄

export AIRFLOW_HOME=/opt/airflow

# 執行 airflow 命令,這時會在 /opt/airflow 目錄下生產 airflow的配置檔案

airflow

# 進入到 AIRFLOW_HOME目錄下

cd /opt/airflow

# 修改 airflow 配置檔案

vim airflow.cfg

# dag 檔案的根目錄

dags_folder = /opt/airflow/dags

# 日誌的根目錄

base_log_folder = /opt/airflow/logs

# dag處理器的日誌路徑

dag_processor_manager_log_location = /opt/airflow/logs/dag_processor_manager/dag_processor_manager.log

# 時區改為北京時間

default_timezone = Asia/Shanghai

# 執行器使用 CeleryExecutor

executor = CeleryExecutor

# 使用 mysql 作元資料管理

sql_alchemy_conn = mysql://airflow:[email protected]/airflow

# 設定併發為320

parallelism =320

# 設定 dag 併發為100

dag_concurrency =100

# 設定每個 dag 最大

max_active_runs_per_dag =1

# 將載入 dag 樣例設定為否

load_examples = False

# 設定 外掛的目錄

plugins_folder = /opt/airflow/plugins

# 設定終端url

endpoint_url = http://localhost:7080

# 設定 基準

base_url = http://localhost:7080

# 設定 webserver埠

web_server_port =7080

# 將airflow.cfg暴露到 web 介面

expose_config = True

# 將每頁顯示的 dag 數目設定為10

page_size =10

# 開啟rbac

rbac = True

# 配置 celery 的 broker 為 redis

broker_url = redis://:[email protected]:6379/0

# 設定 celery 的結果儲存的介質,使用mysql

result_backend = db+mysql://airflow:[email protected]/airflow

# 設定 scheduler 下發任務的佇列

default_queue = queue_edw

# 設定最小處理dag 檔案的間隔

min_file_process_interval =1

# 設定scheduler子處理任務的日誌目錄

child_process_log_directory = /opt/airflow_uat/logs/scheduler

# 設定 scheduler 最大執行的 殭屍程序閾值

scheduler_zombie_task_threshold =30

# 設定 scheduler的最大執行緒數

max_threads =20

3.1 依賴元件配置

若使用hive的話,airflow使用者需要提交任務到yarn上,需要配置相關許可權

Fair Scheduler Allocations (Deployed)

yarn.admin.acl

需要在上面兩個配置中新增 airflow 使用者

4.airflow 命令

# 檢視 airflow 版本

airflow version

# 初始化 元資料庫

airflow initdb

# 升級 元資料庫

airflow upgradedb

# 重置 元資料庫

airflow resetdb

# 在後臺啟動 airflow 的 web 服務

airflow webserver -D

# 在後臺啟動 airflow 的排程器

airflow scheduler -D

# 在後臺啟動 celery 的 flower監控元件

airflow flower -D

# 啟動worker服務 -q 指定 task 傳遞的佇列

airflow worker -q queue_edw -D

# 關閉airflow 相關服務

ps -ef|egrep'celeryd|serve_logs|scheduler|airflow-webserver|flower'|grep -v grep|awk'{print $2}'|xargs kill -9

rm /opt/airflow/*.pid

5. 分散式安裝配置

經過 1-4步 即可在物理機中執行起一個 單機版本的 airflow, 若想要將 airflow 分散式安裝到多個節點,只需 在其餘節點 重複 1-3步,然後再複製 第 4 步的 airflow.cfg 檔案到 其餘節點即可。

為了方便安裝,需要配置ssh,並互相授信,以方便傳遞配置檔案。

參考文件

[0]Basic Airflow architecture

https://airflow.readthedocs.io/en/latest/start.html#basic-airflow-architecture

[1] 使用 pyenv 管理 Python 版本

https://einverne.github.io/post/2017/04/pyenv.html

[2]Linux 使用rpm方式安裝最新mysql(5.7.22)步驟以及常見問題解決

https://blog.csdn.net/hao134838/article/details/80163181

[3]redis快速安裝部署

https://blog.csdn.net/guancong3412/article/details/88683044

[4] Airflow Installation Doc

https://airflow.readthedocs.io/en/latest/installation.html

[5] Configuration Reference

https://airflow.readthedocs.io/en/latest/configurations-ref.html

[6]Using the Command Line Interface

https://airflow.readthedocs.io/en/latest/usage-cli.html

[7] MySQL 5.7中explicit_defaults_for_timestamp引數

https://www.jianshu.com/p/dfa0380eb6b9