Computer Engineering/Data Engineering

Airflow k8s 로컬 개발환경 셋팅

jordan.bae 2022. 7. 8. 00:28

Introduction

이 글의 목표는 local에서 kubernates를 docker container에서 실행시키기 위해서 kinder를 설치하고, helm를 이용해서 airflow를 설치하고 배포하는 방법을 정리하는 것입니다. Airflow는 Production에서 kubernates(이하 k8s)에 배포하는 것을 권장하기 때문에 local 환경에서도 가능한한 k8s에서 실행시키는 것이 좋다라고 생각합니다. 그래서 kind와 helm을 이용해서 airflow를 local에서 kubernates위에서 실행시키기 위한 셋업 절차를 정리한 것을 공유하려고 합니다. 대부분의 내용은 공식문서 를 참고하여 작성하였습니다. 또, 이 글은 Mac 사용자를 기준으로 한 글입니다.

 

 

Requirements

kind

로컬에 k8s 클러스터를 실행시키기 위해서 kind를 이용합니다.

kind는 도커 컨테이너에 k8s 클러스터를 실행시켜주는 도구로 k8s를 테스트 하기 위해 디자인 되었습니다.

설치는 brew를 이용해서 설치합니다.

brew install kind

 

helm

헬름을 통한 쿠버네티스 애플리케이션 관리할 수 있습니다. 헬름 차트는 복잡한 쿠버네티스 애플리케이션도 편리하게 정의하여 설치하거나 업그레이드할 수 있습니다. 마찬가지로 brew를 이용해서 설치합니다.

brew install helm

 

 

kubectl

Kubectl은 쿠버네티스 클러스터를 제어하기 위한 커맨드 라인 도구입니다. 설치가 되어 있지 않다면 마찬가지로 brew를 이용해서 설치합니다.

brew install kubectl

 

이렇게 3가지 tool이 준비가 되면 이제 k8s 클러스터를 생성하고 그 cluster에 helm으로 airflow chart를 설치할 수 있습니다.

 

 

초기 셋업

맨 처음에는 k8s cluster 생성 및 airflow chart 설치가 필요합니다. 그 이후에는 docker container를 build하고 해당 docker image를 kind에 load해주고 chart를 업데이트 해주면 됩니다.

 

1. kind로 docker container에 Cluster 생성

kind create cluster --image kindest/node:v1.21.1

# confirm it's up
➜  ~ kubectl cluster-info --context kind-kind
Kubernetes control plane is running at https://127.0.0.1:54205
CoreDNS is running at https://127.0.0.1:54205/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

  

2. Airflow 공식 helm stable repo 추가

helm repo add apache-airflow https://airflow.apache.org
helm repo update

 

3. k8s namespace 생성

kubectl create namespace airflow-local

 

4. airflow chart 설치

export RELEASE_NAME=airflow-release

# 설치 끝
helm install $RELEASE_NAME apache-airflow/airflow --namespace $NAMESPACE
NAME: airflow-release
LAST DEPLOYED: Wed Jul  6 15:28:09 2022
NAMESPACE: airflow-local
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Thank you for installing Apache Airflow 2.3.0!

Your release is named airflow-release.
You can now access your dashboard(s) by executing the following command(s) and visiting the corresponding port at localhost in your browser:

Airflow Webserver:     kubectl port-forward svc/airflow-release-webserver 8080:8080 --namespace airflow-local
Default Webserver (Airflow UI) Login credentials:
    username: admin
    password: admin
Default Postgres connection credentials:
    username: postgres
    password: postgres
    port: 5432

You can get Fernet Key value by running the following:

    echo Fernet Key: $(kubectl get secret --namespace airflow-local airflow-release-fernet-key -o jsonpath="{.data.fernet-key}" | base64 --decode)

###########################################################
#  WARNING: You should set a static webserver secret key  #
###########################################################

You are using a dynamically generated webserver secret key, which can lead to
unnecessary restarts of your Airflow components.

Information on how to set a static webserver secret key can be found here:
https://airflow.apache.org/docs/helm-chart/stable/production-guide.html#webserver-secret-key

 

5. airflow webserver를 체크해보기 위해서 port-forwarding 설정 (편의를 위해서 다른 터미널을 띄워서 실행)

kubectl port-forward svc/airflow-release-webserver 8080:8080 --namespace airflow-local

여기 까지 진행하면 weberserver가 잘 동작하는 것을 확인할 수 있습니다.

 

기타 커맨드

➜  airflow_base kubectl get pods --namespace airflow-local
NAME                                         READY   STATUS    RESTARTS   AGE
airflow-release-postgresql-0                 1/1     Running   0          43m
airflow-release-redis-0                      1/1     Running   0          43m
airflow-release-scheduler-6dbdc8485b-k7998   2/2     Running   0          43m
airflow-release-statsd-5dcccd5c98-mdnz6      1/1     Running   0          43m
airflow-release-triggerer-bdf789cfb-vc94h    1/1     Running   0          43m
airflow-release-webserver-98d64c66d-9zn2t    1/1     Running   0          43m
airflow-release-worker-0                     2/2     Running   0          43m


➜  airflow_base  helm list --namespace airflow-local
NAME           	NAMESPACE    	REVISION	UPDATED                             	STATUS  	CHART        	APP VERSION
airflow-release	airflow-local	1       	2022-07-06 15:28:09.298143 +0900 KST	deployed	airflow-1.6.0	2.3.0

 

 

이후 코드 업데이트 후 재배포

1) sample dag 생성 및 Dockerfile 생성

➜ mkdir ariflow_base && cd airflow_base
➜ mkdir dags
cat <<EOM > Dockerfile
FROM apache/airflow:2.3.2
COPY . .
EOM
➜  airflow_base ls
Dockerfile dags

➜  cd dags
➜  dags ls
# you can get the tutorial.py here(https://airflow.apache.org/docs/apache-airflow/stable/tutorial.html)
➜  dags mkdir example_dags
➜  dags cd example_dags
➜  example_dags vi tutorial.py

 

2) docker image build 및 image load

# build docker image
$ docker build --pull --tag airflow-base:0.0.1 .

# load docker image
$ kind load docker-image airflow-base:0.0.1

 

3) chart 업데이트 

# deploy airflow using helm 

# if it's running
$ helm upgrade airflow-release apache-airflow/airflow --namespace airflow-local --set images.airflow.repository=airflow-base --set images.airflow.tag=0.0.1

# if it's not running
$ helm install airflow-release apache-airflow/airflow --namespace airflow-local --set images.airflow.repository=airflow-base --set images.airflow.tag=0.0.1

다시 fort-wording한 후에 webserver에 접속하면 이제 추가한 DAG이 잘 표시되는 것을 확인할 수 있습니다.

 

마무리

 

생각보다 간단하게 airflow 로컬 개발환경을 kind와 helm을 이용해서 셋팅할 수 있습니다. 실제 개발을 위해서는 해줄 것들이 조금 더 있는데 이 부분은 나중에 다시 글을 작성해 보도록 하겠습니다. 도움이 되셨으면 좋겠습니다!

감사합니다! 

 

Reference

https://airflow.apache.org/docs/helm-chart/stable/quick-start.html

반응형