데이터 공부/Apache Airflow

Airflow를 helm으로 배포해보기 - (2)

한소희DE 2023. 4. 8. 19:18
** Airflow 관련 업데이트 소식

지난 4월 1일, Airflow에서 2.5.3 버전을 release 했다.
중요한 변화는 없었지만, 크고 작은 버그들이 개선됐다고 한다.

참고문서: Airflow Release Notes
https://airflow.apache.org/docs/apache-airflow/stable/release_notes.html#airflow-2-5-3-2023-04-01
 

Release Notes — Airflow Documentation

 

airflow.apache.org

 


 

지난 포스팅에 이어서, GKE 환경의 Airflow를 처음부터 배포해 보도록 하겠다.

 

 

01. DB 생성 

Airflow 를 처리하기 위해서는 postgreSQL 서버가 필요하다.

간단한 테스트 용 postgreSQL 서버를 생성해 보겠다.

 

나는 Elephant SQL을 이용하여 PostgreSQL 무료 서버를 생성하였다.

(사실 사내에서는 GCP SQL을 사용하긴 하는데, 공부 용으로 간단히 필요한 거라 Elephant SQL을 썼다.)

 

https://customer.elephantsql.com/instance

 

ElephantSQL

 

customer.elephantsql.com

 

ElephantSQL 이란?

PostgreSQL 데이터베이스를 클라우드 상에서 제공하는 서비스다. 클라우드 상에서 제공하기 때문에, 인터넷에 연결된 어디서든 접근이 가능하다. 제한된 규모 내에서 무료 버전이 존재하여, 작은 테스트를 진행할 때 편리하다.
PostgreSQL으로 DB를 생성하는 이유?

Airflow 문서를 보면, PostgreSQL을 공식적으로 지원하는 것을 알 수 있다. 따라서 기본 Helm 차트는 컨테이너에서 실행되는 Postgres 데이터베이스가 존재한다. 이 Helm 차트 내 DB를 사용해도 좋으나, 상용에서는 외부 DB를 설정하는 것이 좋다.
Helm 차트 내 DB가 아닌, 외부 DB설정이 좋은 이유?
[아래 출처 링크에서 복사해 왔다.]
1. 임베디드 Postgres 버전은 일반적으로 구식 이므로 심각한 보안 버그에 취약하다.
2. 임베디드 데이터베이스는 성능 요구 사항에 맞게 확장되지 않을 수 있다.
3. 외부 데이터베이스는 더 높은 가동 시간을 달성할 가능성이 높다.
4. 백업 및 재해 복구로 외부 데이터베이스를 구성할 수 있다.
** 출처: https://github.com/airflow-helm/charts/blob/main/charts/airflow/docs/faq/database/external-database.md

 

DB 설정 관련 Airflow 문서 링크는 다음과 같다.

 

Production Guide — helm-chart Documentation

 

airflow.apache.org

 

GitHub - airflow-helm/charts: The User-Community Airflow Helm Chart is the standard way to deploy Apache Airflow on Kubernetes w

The User-Community Airflow Helm Chart is the standard way to deploy Apache Airflow on Kubernetes with Helm. Originally created in 2018, it has since helped thousands of companies create production-...

github.com

 

 

 

 

여튼 위와 같은 이유로, 위 사진처럼, DB 생성을 진행했다. DB를 생성했으니, 이제 본격적으로 배포를 해보겠다.

 

 

 


 

 

02. Airlfow Helm 배포

 

왜 Helm으로 Airflow를 배포하는가?

유지보수가 쉽다. 왜냐하면 Airflow는 K8S 친화적 프로젝트를 목표로 하기 때문이다. 따라서, 공식 Helm 차트를 유지 및 관리하기도 하고, 이를 통해 복잡한 배포를 간소화하고 & 업그레이드 등이 간단해진다. 따라서, Airflow 내에서도 공식 Helml 차트를 이용한 배포를 권장하는 것으로 알고 있다.

 

 

내가 참고 한 퀵스타트 가이드 링크다.

https://github.com/airflow-helm/charts/blob/main/charts/airflow/docs/guides/quickstart.md

 

GitHub - airflow-helm/charts: The User-Community Airflow Helm Chart is the standard way to deploy Apache Airflow on Kubernetes w

The User-Community Airflow Helm Chart is the standard way to deploy Apache Airflow on Kubernetes with Helm. Originally created in 2018, it has since helped thousands of companies create production-...

github.com

 

위 가이드를 참고하여, 배포를 진행해보겠다.

 

 

 

 

01. Helm Chart 가져오고, 업데이트

 

>> helm repo add airflow-stable https://airflow-helm.github.io/charts
>> helm repo update

 

 

02. NameSpace 생성

 

>>kubectl create ns dev-airflow-2

 

 

03. values.yaml 파일 작성

 

values.yaml 파일에는, helm 차트 내에 넣을 원하는 value 값을 지정해주어야 한다. airflow 에는 엄청 많은 양의 value를 지정해주어야 해서, airflow에서는 아래의 세 개 샘플 중 원하는 Executor에 해당하는 values.yaml 샘플을 참조하여 values.yaml을 작성하는 것을 권장한다.

 

종류 설명 샘플 링크
CeleryExecutor * Airflow 기본 Executor 중 하나
* Celery를 이용하여 작업을 실행
* 여러 Worker 운영, 분산 처리 가능
https://github.com/airflow-helm/charts/blob/main/charts/airflow/sample-values-CeleryExecutor.yaml
KubernetesExecutor * K8S를 이용해 작업을 실행
* K8S에서 작업자를 동적으로 생성 및 관리
* 작업을 K8S Pod 내에서 실행되어 K8S 클러스터 내에서 자동 확장 가능
* 대규모 적합
https://github.com/airflow-helm/charts/blob/main/charts/airflow/sample-values-KubernetesExecutor.yaml
CeleryKubernetesExecutor * CeleryExecutor와 KubernetesExecutor을 결합한 형태
* Celery를 이용하여 작업을 실행
* 작업자를 K8S Pod 내에서 동적으로 생성 및 관리
* 작업 처리량, 성능 최적화 가능
https://github.com/airflow-helm/charts/blob/main/charts/airflow/sample-values-CeleryKubernetesExecutor.yaml

 

이 중에서, 나는 KubernetesExecutor의 예시를 이용하도록 하겠다.

(Celery 기능이 필요 없으며, K8S를 통한 동적 확장 및 생성 관리를 수행하여 대규모에 적합하기 때문)

 

 

해당 예시 values.yaml 에서, 내부 postgres가 아닌 (위에서 생성한) 외부 postgres를 이용할 것이기 때문에, yaml 하단 DATABASE | External Database 위치에, 위에서 생성한 DB 관련 설정 값을 작성해 두었다.

values 옵션은 해당 링크를 참조하였다.

 

###################################
## DATABASE | External Database
###################################
externalDatabase:
  ## the type of external database
  ## - allowed values: "mysql", "postgres"
  ##
  type: postgres

  ## the host of the external database
  ##
  host: ~.db.elephantsql.com

  ## the port of the external database
  ##
  port: 5432

  ## the database/scheme to use within the external database
  ##
  database: ~
 .
 .
 .

 

 

 

04. Helm Chart Install

 

test-airflow 라는 이름으로, 8.4.0 버전의 에어플로우 헬름 차트dev-airflow-2라는 namespace에 install 해보겠다.

 

helm install \
  test-airflow \
  airflow-stable/airflow \
  --namespace dev-airflow-2 \
  --version "8.4.0" \
  --values ./values.yaml

 

 

배포가 성공적으로 완료되면, Kubectl 포트 포워딩을 통해 로컬에서 클러스터에 빠르게 액세스할 수 있도록 한다.

 

kubectl port-forward svc/test-airflow-web 8089:8080 -n dev-airflow-2

 

 

 

여기까지 작업이 완료되면, 아래와 같이 127.0.0.1:8089에 접속하면, airflow가 잘 보이는 것을 알 수 있다.

 

 

 

 

 

 

하지만 위에서 생성한 db의 실질적인 반영이나, 이전 포스팅에서 다룬 git-sync 등이 모두 반영되지 않은, 빈 껍데기 배포본이다.

또한 Ingress 나, Storageclass, PersistentVolumeClaim, Secret 등의 K8S 리소스 설정이 필요하다.

 

따라서, 다음 포스팅에서 values.yaml 설정을 함께 진행해 봄으로써 구성을 조금씩 갖춰 보도록 하겠다.

(다다음 포스팅에서는 쿠버네티스 관련 리소스 설정을 진행해 볼 예정이다..!)

 

오늘의 포스팅은 여기에서 마무리하도록 하겠다 ~!