⚙️ Scenario
You have a cleanup service (e.g., deleting old files, cleaning temp data, or archiving logs) that should run periodically.
You can schedule it using either:
- Kubernetes CronJob → native K8s scheduling
- Airflow DAG → workflow orchestration
1️⃣ Kubernetes CronJob
Definition:
A CronJob in Kubernetes runs a Job on a schedule, similar to Linux
cron.
How It Works:
-
Define a CronJob YAML:
apiVersion: batch/v1 kind: CronJob metadata: name: cleanup-cron spec: schedule: "0 2 * * *" # every day at 2 AM jobTemplate: spec: template: spec: containers: - name: cleanup image: my-cleanup-service:latest restartPolicy: OnFailure -
K8s scheduler triggers a Job container at the scheduled time.
-
Job runs the cleanup → deletes old resources or temp data.
-
Can scale horizontally if needed (multiple jobs run concurrently).
Pros:
- Simple, declarative
- Native K8s scheduling → no external tool
- Handles retries via
restartPolicy
Cons:
- Limited to simple cron scheduling
- No dependency management between jobs
2️⃣ Airflow DAG (Directed Acyclic Graph)
Definition:
Airflow schedules tasks (Operators) in DAGs with complex dependencies, retries, and logging.
How It Works:
-
Define a DAG for cleanup:
from airflow import DAG from airflow.operators.python import PythonOperator from datetime import datetime, timedelta def cleanup(): # logic to delete old files pass dag = DAG( 'cleanup_dag', start_date=datetime(2025,10,10), schedule_interval='0 2 * * *', # daily at 2 AM catchup=False ) cleanup_task = PythonOperator( task_id='cleanup_task', python_callable=cleanup, dag=dag ) -
Airflow scheduler triggers the task at scheduled time.
-
Supports retries, notifications, dependencies, and logging.
Pros:
- Powerful scheduling & dependency management
- Easy retries and alerting
- Good for complex workflows
Cons:
- More infrastructure overhead (Airflow server, DB, scheduler)
- Overkill for simple periodic tasks
🔄 Comparison Table
| Feature | Kubernetes CronJob | Airflow DAG |
|---|---|---|
| Complexity | Simple | Advanced |
| Dependencies | None | Yes, DAG-based |
| Retries | Yes, restartPolicy | Yes, configurable |
| Logging / Monitoring | Basic | Extensive, web UI |
| Infrastructure | K8s cluster only | Airflow server + DB |
| Use Case | Simple periodic jobs | Complex workflows with dependencies |
🧩 Analogy
- CronJob: “Set an alarm clock to run cleanup at 2 AM” → simple, reliable
- Airflow: “Set a workflow schedule with multiple steps, retry on failure, notify if something goes wrong” → more control
✅ Key Takeaways
- K8s CronJob → lightweight, good for simple periodic tasks (cleanup, reports)
- Airflow DAG → heavyweight, better for complex pipelines with dependencies and retries
- Both can trigger cleanup services, but choice depends on complexity, monitoring, and dependencies