[데이터 엔지니어링 데브코스 2기] TIL-12주차 Airflow의 다양한 고급 기능과 CI/CD 환경 설정에 대해 학습 (2)

이재호·2024년 1월 4일
0

1. 구글 시트를 테이블로 복사하는 예제


절차.

  • 시트 API를 활성화하고 구글 서비스 어카운트를 생성한 후 해당 내용을 JSON 파일로 다운로드.
  • 어카운트에서 생성해 준 이메일을 조작하고 싶은 시트에 공유.
  • Airflow DAG 쪽에서 해당 JSON 파일로 인증하고 앞서 시트를 조작.
  • aws_conn_id Connections 설정.
  • g. spread sheet api 활성화.

2. API & Airflow 모니터링


알아볼 내용.

  • Airflow의 건강 여부 체크(health check).
  • Airflow API로 외부에서 Airflow 조작하는 방법.

2-1. Airflow API 활성화


  • airflow.cfg의 api 섹션에서 auth_backend의 값을 변경.
auth_backend = airflow.api.auth.backend.basic_auth
  • docker-compose.yaml에는 이미 설정되어 있음. (AIRFLOWAPIAUTH_BACKENDS. AIRFLOW섹션키)

  • docker exec -it learn-airflow-airflow-scheduler-1 airflow config get-value api auth_backend 명령어로 확인.

  • Airflow Web UI에서 새로운 사용자 추가. (Security -> List Users)

2-2. health API 호출


  • /health API 호출. curl -X GET --user "monitor:MonitorUser1" http://localhost:8080/health

API 사용 예.

  • https://airflow.apache.org/docs/apache-airflow/stable/stable-rest-api-ref.html#section/Overview
  • 특정 DAG를 API로 트리거하기. curl -X POST "airflow:airflow" -H ... -d '' "http://localhost:8080/api/v1/dags/DAG 아이디/dagRuns"
  • 모든 DAG 리스트하기. curl -X GET --user "airflow:airflow" http://localhost:8080/api/v1/dags
  • 모든 Variable 리스트하기. curl -X GET --user "airflow:airflow" http://localhost:8080/api/v1/variables
  • 모든 Config 리스트하기. curl -X GET --user "airflow:airflow" http://localhost:8080/api/v1/config 기본적으로 Disabled로 돼 있기에 확인할 수가 없음. 따라서, airflow.cfg에서 해당 섹션을 찾아 키 값을 변경해 주어야 함.

혹은 airflow {variables, connections} {export, import} {variables, connections}.json 등으로 (Web UI에서 처럼) 확인 가능.

  • docker exec -it learn-airflow-airflow-scheduler-1 airflow config get-value api auth_backend 로 api 확인.

  • curl -X POST --user "airflow:airflow" -H 'Content-Type: application/json' -d '{"execution_date": "2023-05-24T00:00:00Z"}' "http://localhost:8080/api/v1/dags/HelloWorld/dagRuns"처럼 특정 dag에 대해 api로 호출 가능.

profile
천천히, 그리고 꾸준히.

0개의 댓글