Airflow 動手玩:(六)Airflow with Kuberentes Executor


前兩篇我們看過了 Airflow 的架構及設定檔,原本這篇想要直接帶大家在 EKS 部署 Airflow,並用 Spot Instance 當 Node,不過後來覺得我們還是在 local 先用 minikube 搭配 Airflow,讓大家對於將 Airflow 部署在 Kubernetes 有經驗後,在換到線上環境部署,所以等這次的寫客松結束後,我會在另外發一篇 Deploy Airflow on EKS with Spot Instances。

minikube

minikube 是在 local 一鍵安裝 Kubernetes 的工具,minikube 會把 Kubernetes 跑在虛擬機內,所以會需要額外安裝 HyperKit 或是 Virtualbox。

安裝

> brew install minikube

如果在 local 沒有安裝過 Docker for Desktop,需要再額外安裝 HyperKit。

> brew install hyperkit

啟動

由於 Airflow 給的範例 Yaml 檔適用於 Kubernetes 1.14 或 1.15,所以我們啟動 minikube 時,要把 Kubernetes 的版本鎖定在 1.14 或 1.15,這裡使用 1.14.10 做示範。

> minikube start --memory='4g' --kubernetes-version=v1.14.10

Deploy Airflow on minikube

接下來我們要從 Airflow 的 GitHub Repository 建立 Docker image,並透過官方的 script 做部署。

Download Airflow GitHub Repository

> mkdir ~/Project && cd ~/Project
> git clone https://github.com/apache/airflow.git
> cd ~/Project/airflow
> git checkout c8597cbf143b970ad3c7b0d62e3b44d1dfdc8afe # 將 Airflow 版本切到 1.10.7 的時候

Update Build Script

接下來我們要稍微更動一下 Airflow 的 build script,因為官方的 build script 會把待會創建的 image 放到 kind cluster,所以我們要把其中一行註解掉,kind cluster 是另外一種在 local 建置 Kubernetes 的方式。我們要註解掉 kind load docker-image "${IMAGE}:${TAG}" 這行,所以印出來會像下面的範例。

> cat scripts/ci/kubernetes/docker/build.sh
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

IMAGE=${IMAGE:-airflow}
TAG=${TAG:-latest}
DIRNAME=$(cd "$(dirname "$0")" && pwd)
AIRFLOW_ROOT="${DIRNAME}/../../../.."

set -e

echo "Airflow directory ${AIRFLOW_ROOT}"
echo "Airflow Docker directory ${DIRNAME}"

cd "${DIRNAME}" && docker build --build-arg AIRFLOW_CI_IMAGE="${AIRFLOW_CONTAINER_DOCKER_IMAGE}" --pull "${DIRNAME}" --tag="${IMAGE}:${TAG}"

# kind load docker-image "${IMAGE}:${TAG}"

Build Image

由於我們待會在 minikube 用的 image 是我們自己創建的,所以我們要先讓 minikube 可以連得到我們 local 的 Docker。

> eval $(minikube docker-env)

接下來就可以創建 Image 了。

> export AIRFLOW_CONTAINER_DOCKER_IMAGE=apache/airflow:v1-10-test-python3.5-ci
> ./scripts/ci/kubernetes/docker/build.sh

Deploy Airflow

最後在 minikube 上部署 Airflow。這份 script 主要是部署了 5 份檔案,分別是:

  • secrets.yaml: 將 Metadata Database 的連線資訊存成 Kubernetes Secrets。
  • volumes.yaml: 創立 volume 存放 logs 及 dags。
  • postgres.yaml: 用 PostgreSQL 當作 Metadata Database,這份是開啟 PostgreSQL container 相關的資訊。
  • configmaps.yaml: 將 Airflow 設定檔存成 Kubernetes ConfigMap。
  • airflow.yaml: 開啟 Airflow WebServer, Scheduler 相關的資訊。

    ./scripts/ci/kubernetes/app/deploy_app.sh -d git_mode

Check Airflow Pods

雖然後面可能會出現 Could not resolve host: docker,不過這不影響我們的部署,所以只要確定 Airflow 的 pods 有像下面跑起來即可。

> kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
airflow-68b7c49c46-bk954            2/2     Running   0          5m56s
postgres-airflow-54749848cd-bl4sv   1/1     Running   0          5m56s

測試 Airflow with KubernetesExecutor

開啟 Airflow WebServer

在 minikube 上,我們只要跑下面這行指令,minikube 就會自動幫我們開啟瀏覽器,連到我們剛剛部署在 minikube 的 Airflow WebServer Container。

> minikube service airflow
|-----------|---------|-------------|---------------------------|
| NAMESPACE |  NAME   | TARGET PORT |            URL            |
|-----------|---------|-------------|---------------------------|
| default   | airflow |             | http://192.168.64.8:30809 |
|-----------|---------|-------------|---------------------------|
🎉  Opening service default/airflow in default browser...

登入 Airflow

我們這次部署的 Airflow 有預設的 Admin User,所以我們這邊要輸入帳密 airflow / airflow。

Airflow 登入

開啟 example_kubernetes_executor

把 DAG example_kubernetes_executor 的 off 改成 on,並手動 trigger 這個 DAG。

DAG example_kubernetes_executor

Watch Pods

這時候再回到 Terminal 關注 Pod 的變化,會發現每跑一個 Task,KubernetesExecutor 都會創建一個 Pod。

> kubectl get pods -w

總結

這次我們將 Airflow 架設在 minikube 上,也看到了 KubernetesExecutor 將每個 Task 創成 Pod,我們同樣可以在 Kubernetes 部署 Airflow with Celery,不過除了更改 Airflow 設定檔外,我們也要記得做 Celery Container 相關的設定,有了這次的經驗後,相信大家對於線上部署 Airflow 會更有信心

#Airflow #Data Pipeline #Data Cleaning #ETL







你可能感興趣的文章

[筆記] MySQL 環境安裝(Ubuntu 18.04)與 phpMyAdmin 架設

[筆記] MySQL 環境安裝(Ubuntu 18.04)與 phpMyAdmin 架設

[ Note ] Git 交作業

[ Note ] Git 交作業

Day 104 期中測驗心得

Day 104 期中測驗心得






留言討論