[Airflow 기초 μžλ™ν™” - Airflow β†’ MLflow β†’ FastAPI]

🧭 전체 흐름 μ˜ˆμ‹œ [AIRFLOW DAG μ‹€ν–‰] ↓ [train_mlflow.py] - iris λͺ¨λΈ ν•™μŠ΅ - νŒŒλΌλ―Έν„°/λ©”νŠΈλ¦­ λ‘œκΉ… - λͺ¨λΈ Registry 등둝 ↓ [promote_mlflow.py] - μ΅œμ‹  λͺ¨λΈμ„ Production으둜 μ „ν™˜ ↓ [FastAPI] - models:/IrisModel/Production β†’ μ‹€μ‹œκ°„ 예츑 πŸ‘‰ μ‹€μŠ΅ μ½”λ“œλŠ” πŸ”— GitHub (Airflow + MLflow + FastAPI) βœ… [1단계] ν”„λ‘œμ νŠΈ κΈ°λ³Έ 폴더 ꡬ쑰 섀계 πŸ“ 1. 전체 디렉토리 ꡬ성도 mlops_project/ β”œβ”€β”€ airflow/ πŸ›« Airflow μ„€μ • 및 DAG μŠ€μΌ€μ€„λŸ¬ β”‚ β”œβ”€β”€ dags/ ← DAG μ •μ˜ 디렉토리 β”‚ β”‚ └── train_with_mlflow.py ← ν•™μŠ΅ DAG (MLflow 연동) β”‚ β”œβ”€β”€ Dockerfile.airflow ← Airflow용 Dockerfile β”‚ β”œβ”€β”€ requirements.txt ← Airflow μ˜μ‘΄μ„± β”‚ └── .dockerignore β”‚ β”œβ”€β”€ fastapi/ ⚑ FastAPI 예츑 API μ„œλ²„ β”‚ β”œβ”€β”€ app/ β”‚ β”‚ └── main.py ← λͺ¨λΈ μ„œλΉ™ μ—”λ“œν¬μΈνŠΈ β”‚ β”œβ”€β”€ Dockerfile.api ← FastAPI용 Dockerfile β”‚ β”œβ”€β”€ requirements.txt ← FastAPI μ˜μ‘΄μ„± β”‚ └── .dockerignore β”‚ β”œβ”€β”€ ml_code/ 🧠 ML ν•™μŠ΅ 및 ν”„λ‘œλͺ¨μ…˜ μ½”λ“œ β”‚ β”œβ”€β”€ train_mlflow.py ← λͺ¨λΈ ν•™μŠ΅ 및 MLflow λ‘œκΉ… β”‚ └── promote_mlflow.py ← λͺ¨λΈ ν”„λ‘œλͺ¨μ…˜ (Staging β†’ Production) β”‚ β”œβ”€β”€ mlflow_store/ πŸ—‚οΈ MLflow μ €μž₯μ†Œ 경둜 (λ³Όλ₯¨) β”‚ β”œβ”€β”€ Dockerfile.mlflow ← MLflow μ„œλ²„ μ»€μŠ€ν„°λ§ˆμ΄μ§• β”‚ β”œβ”€β”€ mlflow.db ← Model Registry DB (sqlite) β”‚ β”œβ”€β”€ mlruns/ ← μ‹€ν—˜ 둜그 디렉토리 β”‚ β”œβ”€β”€ artifacts/ ← λͺ¨λΈ μ•„ν‹°νŒ©νŠΈ μ €μž₯μ†Œ β”‚ └── .dockerignore β”‚ β”œβ”€β”€ docker-compose.yaml 🧩 전체 μ„œλΉ„μŠ€ ꡬ성 μ •μ˜ β”œβ”€β”€ .env πŸ” 민감 정보 (.env둜 뢄리) β”œβ”€β”€ README.md πŸ“ 전체 ν”„λ‘œμ νŠΈ λ¬Έμ„œν™” β”œβ”€β”€ .gitignore └── .dockerignore βœ… [2단계] docker-compose.yaml 톡합 ꡬ성 🧭 ꡬ성 λͺ©ν‘œ μ„œλΉ„μŠ€λͺ… μ„€λͺ… 포트 airflow DAG μ‹€ν–‰ ν™˜κ²½ (webserver/scheduler) 8080 postgres Airflow 메타데이터 μ €μž₯용 DB λ‚΄λΆ€ 톡신 mlflow MLflow UI + Registry κΈ°λŠ₯ 5000 fastapi μΆ”λ‘  API μ„œλ²„ (λͺ¨λΈ λ‘œλ”©) 8000 이미지 μ‚¬μš©μ‹œ 주의 (UI만 μ œκ³΅ν•˜λŠ” 이미지 쑴재) πŸ“„ docker-compose.yaml 전체 μ˜ˆμ‹œ version: '3.8' services: # πŸ“¦ PostgreSQL: Airflow 메타데이터 μ €μž₯용 DB postgres: image: postgres:13 container_name: postgres env_file: - .env # ← 민감정보 뢄리 (아이디/λΉ„λ²ˆ) environment: POSTGRES_USER: ${POSTGRES_USER} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} POSTGRES_DB: ${POSTGRES_DB} volumes: # ← μ½”λ“œ/데이터 곡유 및 μ˜μ†μ„± 보μž₯ - postgres_data:/var/lib/postgresql/data # ← DB 데이터 μœ μ§€ (μž¬μ‹œμž‘ λŒ€λΉ„) # πŸ›« Airflow: DAG μŠ€μΌ€μ€„λŸ¬ 및 νƒœμŠ€ν¬ μ‹€ν–‰ airflow: build: context: ./airflow # β†’ Airflow μ „μš© Dockerfile 경둜 dockerfile: Dockerfile.airflow container_name: airflow command: standalone # β†’ 둜컬 ν…ŒμŠ€νŠΈμš© 간단 μ‹€ν–‰ λͺ…λ Ή # (- Scheduler + Webserver + DB μ΄ˆκΈ°ν™”κΉŒμ§€ μžλ™μœΌλ‘œ ν•œλ²ˆμ— μ‹€ν–‰) # (- 싀무/μš΄μ˜μ—μ„œλŠ” airflow-webserver, airflow-scheduler ν•„λ“œ 뢄리) ports: - "8080:8080" # β†’ Airflow μ›Ή UI (localhost:8080) depends_on: - postgres # β†’ DBκ°€ λ¨Όμ € μ˜¬λΌμ™€μ•Ό Airflow μ‹œμž‘ κ°€λŠ₯ env_file: - .env environment: # Airflow 메타데이터 DB μ—°κ²° μ£Όμ†Œ AIRFLOW__CORE__SQL_ALCHEMY_CONN: ${AIRFLOW__CORE__SQL_ALCHEMY_CONN} # Airflow 예제 DAG λΆˆλŸ¬μ˜¬μ§€ μ—¬λΆ€ AIRFLOW__CORE__LOAD_EXAMPLES: ${AIRFLOW__CORE__LOAD_EXAMPLES} MLFLOW_TRACKING_URI: http://mlflow:5000 # β†’ DAG μ½”λ“œμ—μ„œ MLflow 연동 volumes: - ./airflow/dags:/opt/airflow/dags # DAG 파일 mount - ./ml_code:/opt/airflow/ml_code # ν•™μŠ΅ μ½”λ“œ 곡유 - ./mlflow_store:/mlflow # λͺ¨λΈ μ €μž₯μ†Œ 곡유 # πŸ”¬ MLflow: μ‹€ν—˜ 좔적 + λͺ¨λΈ λ ˆμ§€μŠ€νŠΈλ¦¬ μ„œλ²„ mlflow: build: context: ./mlflow_store # μ»€μŠ€ν…€ Dockerfile μœ„μΉ˜ dockerfile: Dockerfile.mlflow ports: - "5000:5000" # β†’ MLflow UI (localhost:5000) volumes: - ./mlflow_store:/mlflow # μ‹€ν—˜ 둜그 + DB + artifacts μ €μž₯ environment: - MLFLOW_TRACKING_URI=http://0.0.0.0:5000 # λ‚΄λΆ€ μ»¨ν…Œμ΄λ„ˆ κΈ°μ€€ URI # ⚑ FastAPI: λͺ¨λΈ μ„œλΉ™ API fastapi: build: context: ./fastapi dockerfile: Dockerfile.api container_name: fastapi ports: - "8000:8000" # β†’ 예츑 API μ—”λ“œν¬μΈνŠΈ (localhost:8000) volumes: - ./fastapi/app:/app/app # FastAPI app 디렉토리 mount - ./ml_code:/app/ml_code # ν•™μŠ΅/λͺ¨λΈ μ½”λ“œ 곡유 - ./mlflow_store:/mlflow # μ €μž₯된 λͺ¨λΈ 뢈러였기 μœ„ν•œ mount # πŸ—‚οΈ λ³Όλ₯¨ μ •μ˜ (Postgres DB μ˜μ†μ„± μœ μ§€) volumes: postgres_data: 🎁 μΆ”κ°€λ‘œ ν•΄μ•Ό ν•  것 Airflow 첫 μ‹€ν–‰ ν›„μ—” 보톡 κ΄€λ¦¬μž 계정 생성도 ν•΄μ€˜μ•Ό 함: # airflow μ»¨ν…Œμ΄λ„ˆ 접속 docker exec -it airflow bash # κ΄€λ¦¬μž 계정 생성 airflow users create \ --username airflow \ --password airflow \ --firstname Keoho \ --lastname Ban \ --role Admin \ --email airflow@example.com πŸ” [ꡬ좕 Tip] Airflow, FastAPI, MLflow κ°„ 곡유 λ³Όλ₯¨ ꡬ쑰 확인 곡유 λ¦¬μ†ŒμŠ€ μ„€λͺ… ./mlflow_store:/mlflow (MLflow) MLflow μ„œλ²„κ°€ μ“°λŠ” 둜그/λͺ¨λΈ μ €μž₯μ†Œ ./mlflow_store:/mlflow (Airflow) ν•™μŠ΅ ν›„ λͺ¨λΈ μ €μž₯ μœ„μΉ˜ 곡유 ./mlflow_store:/mlflow (FastAPI) λͺ¨λΈ μΆ”λ‘  μ‹œ λ‘œλ“œ 경둜 곡유 ➑ 경둜 톡일성이 μ€‘μš”ν•¨! μ§€κΈˆμ€ λͺ¨λ‘ ./mlflow둜 곡유 (./mlflow ν•˜μœ„μ— /mlruns 쑴재) ...

June 13, 2025 Β· 8 min