[Airflow] Installation

minyeamer·2025년 5월 27일
0

Apache Airflow 배우기

목록 보기
2/13
post-thumbnail

설치 환경

  • macOS Sequoia 15.5
  • Docker Desktop 4.41.2
  • Airflow 3.0.1

Docker

  • 애플리케이션을 독립적인 환경에서 실행시키는 기술
  • 가상화 서버(VM)와 비교했을 때, Guest OS를 요구하지 않아 더 효율적인 자원 활용이 가능
  • docker-compose 를 이용해 Airflow를 운영하는데 필요한 여러 서비스(컨테이너)를 실행

docker

Docker Descktop 설치

https://docs.docker.com/desktop/setup/install/mac-install/

  • 운영체제에 맞는 Docker Descktop 파일을 내려받아 설치
  • 설치 후 실행하면 아래와 같은 초기 화면을 확인

docker-desktop

Airflow 설치

https://airflow.apache.org/docs/apache-airflow/stable/howto/docker-compose/index.html

1. docker-compose.yaml

% curl -LfO 'https://airflow.apache.org/docs/apache-airflow/3.0.1/docker-compose.yaml'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 12766  100 12766    0     0  38365      0 --:--:-- --:--:-- --:--:-- 38336
  • Airflow 문서로부터 docker-compose.yaml 경로를 복사해 터미널에서 요청
  • 내려받은 docker-compose.yaml 파일을 보면 아래와 같은 컨테이너가 설치될 것임을 짐작 가능
services:
  postgres:
    image: postgres:13

  redis:
    image: redis:7.2-bookworm

  airflow-apiserver: ...
  airflow-scheduler: ...
  airflow-dag-processor: ...
  airflow-worker: ...
  airflow-triggerer: ...
  airflow-init: ...
  airflow-cli: ...
  flower: ...

2. 환경 설정

mkdir -p ./dags ./logs ./plugins ./config
echo -e "AIRFLOW_UID=$(id -u)" > .env
  • Airflow 문서에 따라 필요한 디렉토리를 생성
  • AIRFLOW_UID가 설정되지 않았다는 경고를 피하기 위해 .env 파일을 생성

3. Airflow 초기화

docker compose up airflow-init
  • exited with code 0 가 출력되면 정상적으로 초기화되어 다음 단계로 진행

4. Airflow 실행

docker compose up
  • Docker Desktop 상에서 아래 이미지와 같이 모든 컨테이너가 실행되고 있는지 확인 가능

docker-run

  • 또는 터미널에서 docker ps 명령어를 입력해 컨테이너 실행 상태를 조회 가능
% docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS                    PORTS                    NAMES
a0431bfa0d1f   apache/airflow:3.0.1   "/usr/bin/dumb-init …"   1 minutes ago   Up 1 minutes (healthy)    8080/tcp                 airflow-airflow-worker-1
4d32f0c45d4a   apache/airflow:3.0.1   "/usr/bin/dumb-init …"   1 minutes ago   Up 1 minutes (healthy)    8080/tcp                 airflow-airflow-triggerer-1
f4cd1b0887de   apache/airflow:3.0.1   "/usr/bin/dumb-init …"   1 minutes ago   Up 1 minutes (healthy)    8080/tcp                 airflow-airflow-dag-processor-1
3213192935b6   apache/airflow:3.0.1   "/usr/bin/dumb-init …"   1 minutes ago   Up 1 minutes (healthy)    8080/tcp                 airflow-airflow-scheduler-1
e933dc415a73   apache/airflow:3.0.1   "/usr/bin/dumb-init …"   1 minutes ago   Up 1 minutes (healthy)    0.0.0.0:8080->8080/tcp   airflow-airflow-apiserver-1
94f88f2e5507   postgres:13            "docker-entrypoint.s…"   6 minutes ago   Up 6 minutes (healthy)   5432/tcp                 airflow-postgres-1
3f82ba15433e   redis:7.2-bookworm     "docker-entrypoint.s…"   6 minutes ago   Up 6 minutes (healthy)   6379/tcp                 airflow-redis-1

Airflow 둘러보기

Airflow 로그인

  • 컨테이너가 정상적으로 실행 중인 경우 브라우저에서 localhost:8080 주소로 접속하면 Airflow UI에 접근 가능
  • 초기 계정은 Username 과 Password 모두 airflow 입력

airflow-login

  • 정상적으로 로그인 시 아래와 같은 홈페이지로 이동

airflow-ui

Example DAG 실행

  • 사이드바에서 Dags 메뉴를 클릭하면 DAG 목록이 있는 페이지로 이동
  • 첫 번째에 있는 tutorial_taskflow_templates DAG를 클릭하여 상세 페이지 조회

airflow-dags

  • 상단의 DAG 제목 옆에 토글 스위치를 클릭하면 DAG 활성화
  • 좌측에서 DAG 내 Task 간 종속성을 그래프로 확인 (해당 DAG는 단일 Task로 구성)

airflow-dag-overview

  • 내비게이션 메뉴 중에서 Runs 메뉴를 클릭하면 실행 이력을 조회

airflow-dag-runs

  • 실행된 결과를 하나 클릭하면 하위 Task 목록을 조회 가능
  • State 항목을 통해 처리 상태를 알 수 있는데, success 상태는 Task가 성공적으로 처리됨을 의미

airflow-dag-task

  • Task 하나를 클릭해서 Logs 메뉴에 들어가면 아래와 같이 context 라는 객체를 출력하는 구문이 확인됨
  • 앞서 DAG 페이지의 내비게이션 메뉴 중 Code 메뉴에서 이미 코드를 확인했는데, get_current_context() 라는 함수를 통해 context 라는 딕셔너리 객체를 가져와 출력하는 작업으로 추측
[2025-05-27, 23:31:26] INFO - context: {'dag': <DAG: tutorial_taskflow_templates>, ...}: chan="stdout": source="task"

airflow-dag-task-log

Airflow 중지

% docker compose stop 
[+] Stopping 8/8
 ✔ Container airflow-airflow-dag-processor-1  Stopped                      1.1s 
 ✔ Container airflow-airflow-triggerer-1      Stopped                      0.9s 
 ✔ Container airflow-airflow-worker-1         Stopped                      2.8s 
 ✔ Container airflow-airflow-scheduler-1      Stopped                      0.8s 
 ✔ Container airflow-airflow-apiserver-1      Stopped                      0.6s 
 ✔ Container airflow-airflow-init-1           Stopped                      0.0s 
 ✔ Container airflow-postgres-1               Stopped                      0.1s 
 ✔ Container airflow-redis-1                  Stopped                      0.2s 
  • docker-compose.yaml 파일이 있는 경로에서 서비스를 명시하지 않고 명령어를 실행하면 모든 Airflow 컨테이너를 중지

Airflow 컨테이너 삭제

% docker compose down --volumes --rmi all
[+] Running 13/13
 ✔ Container airflow-airflow-dag-processor-1  Removed                                                                                                                   1.5s 
 ✔ Container airflow-airflow-triggerer-1      Removed                                                                                                                   1.2s 
 ✔ Container airflow-airflow-worker-1         Removed                                                                                                                   3.4s 
 ✔ Container airflow-airflow-scheduler-1      Removed                                                                                                                   1.0s 
 ✔ Container airflow-airflow-apiserver-1      Removed                                                                                                                   0.9s 
 ✔ Container airflow-airflow-init-1           Removed                                                                                                                   0.1s 
 ✔ Container airflow-postgres-1               Removed                                                                                                                   0.2s 
 ✔ Container airflow-redis-1                  Removed                                                                                                                   0.2s 
 ✔ Image apache/airflow:3.0.1                 Removed                                                                                                                   0.9s 
 ✔ Image postgres:13                          Removed                                                                                                                   0.9s 
 ✔ Image redis:7.2-bookworm                   Removed                                                                                                                   0.9s 
 ✔ Volume airflow_postgres-db-volume          Removed                                                                                                                   0.0s 
 ✔ Network airflow_default                    Removed                                                                                                                   0.5s 
  • docker-compose.yaml 파일이 있는 경로에서 서비스를 명시하지 않고 명령어를 실행하면 모든 Airflow 컨테이너를 삭제
    • --rmi 옵션을 추가하여 관련 이미지까지 모두 삭제
profile
데이터의 모든 것을 추구합니다.

0개의 댓글