Bird
Raised Fist0
Apache Airflowdevops~10 mins

Kubernetes executor for dynamic scaling in Apache Airflow - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Kubernetes executor for dynamic scaling
Airflow Scheduler receives task
↓
Scheduler sends task to Kubernetes Executor
↓
Kubernetes Executor creates a Pod for the task
↓
Pod runs the task
↓
Task completes, Pod terminates
↓
Kubernetes Executor reports task status back to Scheduler
↓
Scheduler updates task state
↓
If more tasks, repeat Pod creation
↓
Kubernetes cluster scales Pods dynamically based on demand
The Kubernetes executor creates a new Pod for each task dynamically, runs the task inside it, and scales Pods up or down based on workload.
Execution Sample
Apache Airflow
from airflow import DAG
from airflow.operators.bash import BashOperator
from datetime import datetime

dag = DAG('example', start_date=datetime(2024,1,1))
t1 = BashOperator(task_id='print_date', bash_command='date', dag=dag)
This Airflow DAG defines a simple task that prints the date, which Kubernetes executor will run in a separate Pod.
Process Table
StepActionKubernetes Executor BehaviorPod StateScheduler State
1Scheduler receives task 'print_date'No Pod yetNo PodTask queued
2Scheduler sends task to Kubernetes ExecutorCreates Pod 'print_date-pod'Pod created, PendingTask running
3Pod starts running taskPod status changes to RunningRunningTask running
4Task executes 'date' command inside PodPod runs commandRunningTask running
5Task completes successfullyPod status changes to SucceededSucceededTask success
6Pod terminates and is cleaned upPod deletedNo PodTask success
7Scheduler ready for next taskWaits for next taskNo PodIdle
💡 No more tasks to run, Kubernetes executor scales down Pods to zero.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
Pod StateNonePendingRunningSucceededDeleted
Task StateQueuedRunningRunningSuccessSuccess
Key Moments - 3 Insights
Why does Kubernetes executor create a new Pod for each task instead of reusing one?
Each task runs in its own Pod to isolate execution and allow dynamic scaling. See execution_table rows 2 and 3 where a new Pod is created and started for the task.
What happens to the Pod after the task completes?
The Pod status changes to 'Succeeded' and then the Pod is deleted to free resources, as shown in execution_table rows 5 and 6.
How does Kubernetes executor handle multiple tasks arriving at once?
It creates multiple Pods dynamically, scaling up the cluster as needed. This is implied in the flow where each task triggers Pod creation (concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Pod state at Step 3?
APending
BRunning
CSucceeded
DDeleted
💡 Hint
Check the 'Pod State' column at Step 3 in the execution_table.
At which step does the task state change to 'Success'?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look at the 'Task State' column in variable_tracker after Step 5.
If a new task arrives while a Pod is running, what does Kubernetes executor do?
ACreates a new Pod for the new task
BReuses the existing Pod
CQueues the task until Pod finishes
DFails the new task
💡 Hint
Refer to concept_flow where each task triggers Pod creation.
Concept Snapshot
Kubernetes executor runs each Airflow task in its own Pod.
Pods are created dynamically when tasks start and deleted after completion.
This allows Airflow to scale tasks up or down based on workload.
Scheduler sends tasks to executor, executor manages Pods.
Pods isolate tasks and free resources when done.
Full Transcript
The Kubernetes executor in Airflow works by creating a new Pod for each task it receives from the scheduler. When the scheduler sends a task, the executor creates a Pod in the Kubernetes cluster to run that task. The Pod starts in a Pending state, then moves to Running while executing the task command. Once the task finishes successfully, the Pod status changes to Succeeded and the Pod is deleted to free resources. The scheduler updates the task state accordingly. This dynamic creation and deletion of Pods allows Airflow to scale the number of running tasks up or down based on demand, efficiently using cluster resources.

Practice

(1/5)
1. What is the main benefit of using the Kubernetes executor in Airflow?
easy
A. It disables task retries to save resources.
B. It stores all task logs in a local file system.
C. It schedules tasks only on a single fixed server.
D. It runs each task in its own pod, allowing dynamic scaling.

Solution

  1. Step 1: Understand Kubernetes executor behavior

    The Kubernetes executor runs each Airflow task in a separate pod, which allows tasks to run independently and scale dynamically.
  2. Step 2: Compare options with executor features

    Options B, C, and D describe behaviors not related to Kubernetes executor's main feature of dynamic pod creation and scaling.
  3. Final Answer:

    It runs each task in its own pod, allowing dynamic scaling. -> Option D
  4. Quick Check:

    Kubernetes executor = dynamic pod scaling [OK]
Hint: Kubernetes executor = one pod per task for scaling [OK]
Common Mistakes:
  • Thinking tasks run on a single server
  • Confusing executor with local logging
  • Assuming retries are disabled
2. Which configuration key is required to enable the Kubernetes executor in Airflow's airflow.cfg?
easy
A. executor = LocalExecutor
B. executor = CeleryExecutor
C. executor = KubernetesExecutor
D. executor = SequentialExecutor

Solution

  1. Step 1: Identify executor configuration key

    In airflow.cfg, the executor key sets which executor Airflow uses.
  2. Step 2: Match correct executor name for Kubernetes

    The correct value to enable Kubernetes executor is KubernetesExecutor. Other options are different executors.
  3. Final Answer:

    executor = KubernetesExecutor -> Option C
  4. Quick Check:

    Enable Kubernetes executor = executor = KubernetesExecutor [OK]
Hint: Set executor = KubernetesExecutor in airflow.cfg [OK]
Common Mistakes:
  • Using LocalExecutor or CeleryExecutor by mistake
  • Misspelling KubernetesExecutor
  • Changing unrelated config keys
3. Given this Airflow task using Kubernetes executor, what happens when the DAG runs?
from airflow import DAG
from airflow.operators.bash import BashOperator
from datetime import datetime
with DAG('example', start_date=datetime(2024,1,1)) as dag:
    task = BashOperator(task_id='print_date', bash_command='date')
medium
A. The task runs inside a new Kubernetes pod created dynamically.
B. The task runs on the Airflow scheduler machine only.
C. The task fails because BashOperator is not supported with Kubernetes executor.
D. The task runs on a fixed worker node without pod creation.

Solution

  1. Step 1: Understand task execution with Kubernetes executor

    When using Kubernetes executor, each task runs inside its own pod created dynamically in the Kubernetes cluster.
  2. Step 2: Analyze the given BashOperator task

    The BashOperator runs a simple shell command. With Kubernetes executor, this command runs inside a new pod, not on the scheduler or fixed node.
  3. Final Answer:

    The task runs inside a new Kubernetes pod created dynamically. -> Option A
  4. Quick Check:

    BashOperator + Kubernetes executor = dynamic pod run [OK]
Hint: Kubernetes executor runs each task in a new pod [OK]
Common Mistakes:
  • Assuming tasks run on scheduler machine
  • Thinking BashOperator is unsupported
  • Believing pods are not created dynamically
4. You configured Airflow with Kubernetes executor but tasks are not running and pods are not created. What is the most likely cause?
medium
A. The Kubernetes cluster credentials are missing or incorrect.
B. The DAG file has syntax errors unrelated to executor.
C. The Airflow scheduler is running with LocalExecutor.
D. The BashOperator command is invalid.

Solution

  1. Step 1: Check Kubernetes executor requirements

    Kubernetes executor needs valid cluster credentials to create pods dynamically.
  2. Step 2: Identify why pods are not created

    If no pods appear, missing or wrong Kubernetes credentials is the most common cause, blocking pod creation.
  3. Final Answer:

    The Kubernetes cluster credentials are missing or incorrect. -> Option A
  4. Quick Check:

    No pods + Kubernetes executor = check cluster credentials [OK]
Hint: No pods? Verify Kubernetes credentials first [OK]
Common Mistakes:
  • Blaming DAG syntax errors
  • Assuming scheduler executor type is unrelated
  • Thinking BashOperator command causes pod creation failure
5. You want to optimize Airflow with Kubernetes executor to handle many short tasks efficiently. Which approach helps reduce pod startup overhead?
hard
A. Increase task retries to avoid pod creation failures.
B. Use pod templates with pre-configured settings and enable pod reuse.
C. Disable Kubernetes executor and switch to SequentialExecutor.
D. Run all tasks in a single pod to save resources.

Solution

  1. Step 1: Understand pod startup overhead in Kubernetes executor

    Each pod creation takes time, so reducing overhead improves efficiency for many short tasks.
  2. Step 2: Identify optimization methods

    Using pod templates with pre-set configs and enabling pod reuse reduces startup time and resource use.
  3. Step 3: Evaluate other options

    Disabling Kubernetes executor or running all tasks in one pod loses scaling benefits. Increasing retries does not reduce pod startup time.
  4. Final Answer:

    Use pod templates with pre-configured settings and enable pod reuse. -> Option B
  5. Quick Check:

    Pod reuse + templates = faster task startup [OK]
Hint: Use pod templates and reuse to speed up pods [OK]
Common Mistakes:
  • Switching to SequentialExecutor loses scaling
  • Running all tasks in one pod breaks isolation
  • Increasing retries does not reduce pod startup
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard