Elasticsearch, Airflow 활용하기 - 3. Airflow 환경설정

2025. 3. 4. 01:35·ML_DL/MUJAKJUNG (무작정 시리즈)
728x90
반응형

Airflow 설치

  • windows 환경에서는 Airflow 사용이 쉽지 않기 때문에, Docker를 활용 Airflow 실행
  • Docker 설치 : https://www.docker.com/products/docker-desktop/
  • yml 파일 링크 : https://airflow.apache.org/docs/apache-airflow/2.10.5/docker-compose.yaml
  • 초기 ID와 Password는 airflow이며, 내부에서 비밀번호 변경이 가능하다.
cd /airflow/
docker-compose up

DAG 파일 생성

  • FastAPI의 서버와 Docker로 실행한 Airflow는 localhost라도 기본적으로 통신할 수 없기 때문에,  host.docker.internal:8000으로 해야 api 통신이 가능하다.
  • PythonOperator는 airflow의 operator의 한 종류로, python 함수를 실행시켜주는 operator이다.
  • schedule_interval은 해당 DAG의 실행 주기를 결정한다. UNIX 기반의 time scheduler인 cron 형식을 지원한다. 순서대로 * * * * * (분(0~59), 시간(0~23), 일(1~31), 월(1~12), 요일(0~6)(일~토)) 를 나타낸다.
from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime, timedelta
import requests

def index_articles():
    url = "http://host.docker.internal:8000/index_articles"
    response = requests.get(url)



default_args = {
    "owner": "airflow",
    "depends_on_past": False,
    "start_date": datetime(2024, 3, 1),
    "retries": 1,
    "retry_delay": timedelta(minutes=1)
}

dag = DAG(
    "fetch_articles",
    default_args=default_args,
    description="기사 최신화 테스트",
    schedule_interval="*/5 0 * * *",
)

news_update_task = PythonOperator(
    task_id="fetch_and_store_articles",
    python_callable=index_articles,
    dag=dag
)

news_update_task

Airflow 웹 UI

  • docker-compose를 통해 Airflow를 실행하면, localhost:8080으로 웹 UI를 확인할 수 있다.
  • 위에서 작성한 DAG가 정상적으로 실행되기 위해서는 아래 확인 사항을 점검해야 한다.
    • Elasticsearch 실행 (elasticsearch.bat)
    • FastAPI 서버 실행 (uvicorn backend.news_main:app --reload)
    • Airflow DAG 파일의 API URL 확인
  • DAG파일을 Actions로 삭제할 경우, DB에 메타데이터가 저장되기 때문에 복원을 위해서 DB를 초기화 해야 한다.
  • Runs에 success가 보인다면 정상적인 실행이다. (에러 발생이나 실패시 자세한 log 확인 가능)

Airflow Web UI

저작자표시 (새창열림)

'ML_DL > MUJAKJUNG (무작정 시리즈)' 카테고리의 다른 글

[LangChain] 챗봇 구성하기  (0) 2025.04.03
[LangChain] 시작하기  (0) 2025.03.19
Elasticsearch, Airflow 활용하기 - 2. Elasticsearch 설정  (0) 2025.03.03
Elasticsearch, Airflow 활용하기 - 1. 환경설정  (0) 2025.03.03
이미지 분류 모델 작성하기 (feat. wandb)  (0) 2025.02.19
'ML_DL/MUJAKJUNG (무작정 시리즈)' 카테고리의 다른 글
  • [LangChain] 챗봇 구성하기
  • [LangChain] 시작하기
  • Elasticsearch, Airflow 활용하기 - 2. Elasticsearch 설정
  • Elasticsearch, Airflow 활용하기 - 1. 환경설정
swwho
swwho
일상을 데이터화하다
  • swwho
    하루한장
    swwho
  • 전체
    오늘
    어제
    • 분류 전체보기 (188)
      • ML_DL (39)
        • MUJAKJUNG (무작정 시리즈) (18)
        • 딥러닝 공부하기 (21)
      • 데이터사이언스 (1)
        • EDA (1)
        • 데이터과학을 위한 통계 (0)
      • 데이터엔지니어링 (2)
      • 논문리뷰 (2)
        • Computer Vision (2)
      • Python 활용하기 (12)
      • 코딩테스트 (127)
        • Python (109)
        • MySQL (14)
      • Git (3)
      • MySQL 활용하기 (0)
      • 일상 이야기 (1)
  • 블로그 메뉴

    • 홈
    • 태그
  • 최근 글

  • 250x250
  • hELLO· Designed By정상우.v4.10.3
swwho
Elasticsearch, Airflow 활용하기 - 3. Airflow 환경설정
상단으로

티스토리툴바