Skip to the content.

⚙️ 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:


1️⃣ Kubernetes CronJob

Definition:

A CronJob in Kubernetes runs a Job on a schedule, similar to Linux cron.

How It Works:

  1. 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
    
  2. K8s scheduler triggers a Job container at the scheduled time.

  3. Job runs the cleanup → deletes old resources or temp data.

  4. Can scale horizontally if needed (multiple jobs run concurrently).

Pros:

Cons:


2️⃣ Airflow DAG (Directed Acyclic Graph)

Definition:

Airflow schedules tasks (Operators) in DAGs with complex dependencies, retries, and logging.

How It Works:

  1. 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
    )
    
  2. Airflow scheduler triggers the task at scheduled time.

  3. Supports retries, notifications, dependencies, and logging.

Pros:

Cons:


🔄 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


✅ Key Takeaways

  1. K8s CronJob → lightweight, good for simple periodic tasks (cleanup, reports)
  2. Airflow DAG → heavyweight, better for complex pipelines with dependencies and retries
  3. Both can trigger cleanup services, but choice depends on complexity, monitoring, and dependencies