🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Kubernetes Executor Deep Dive in Apache Airflow

đŸŸĸ Free Lesson

Advertisement

Kubernetes Executor Deep Dive

Kubernetes Executor ArchitectureSchedulerTask dispatchK8s ExecutorPod creationK8s APIPod managementWorker PodsTask executionNamespaceResource isolationPod LifecyclePending {'->'} Running {'->'} Succeeded/Failed {'->'} CleanupResource Requestsrequests: cpu=0.5, mem=1Gi | limits: cpu=1, mem=2GiOne pod per task: full isolation, higher overhead, dynamic scaling

Architecture Diagram

Formal Definitions

Detailed Explanation

What is the Kubernetes Executor?

The Kubernetes executor creates a separate pod for each task in your DAG. This provides complete isolation between tasks and dynamic resource allocation.

Key Insight: Each task gets its own container with dedicated CPU, memory, and network resources. No task can interfere with another task's execution.

How It Works

  • Task Submission: Scheduler tells the K8s executor to run a task
  • Pod Creation: Executor creates a new pod with the task's requirements
  • Task Execution: Pod runs the task in isolation
  • Cleanup: Pod is deleted after task completion (success or failure)

Basic K8s Executor Configuration

Pod Template Configuration

# pod_templates/default_template.yaml
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: airflow-worker
    component: worker
spec:
  serviceAccountName: airflow-worker
  containers:
    - name: base
      image: apache/airflow:2.8.0
      command:
        - "airflow"
        - "serve-logs"
      resources:
        requests:
          cpu: 500m
          memory: 1Gi
        limits:
          cpu: 1000m
          memory: 2Gi
      env:
        - name: AIRFLOW__CORE__EXECUTOR
          value: "KubernetesExecutor"
        - name: AIRFLOW__DATABASE__SQL_ALCHEMY_CONN
          valueFrom:
            secretKeyRef:
              name: airflow-secrets
              key: database-url
      volumeMounts:
        - name: airflow-config
          mountPath: /opt/airflow/airflow.cfg
          subPath: airflow.cfg
        - name: dags
          mountPath: /opt/airflow/dags
      securityContext:
        runAsUser: 50000
        runAsGroup: 0
        fsGroup: 0
  volumes:
    - name: airflow-config
      configMap:
        name: airflow-config
    - name: dags
      persistentVolumeClaim:
        claimName: airflow-dags-pvc
  nodeSelector:
    node-type: worker
  tolerations:
    - key: "dedicated"
      operator: "Equal"
      value: "worker"
      effect: "NoSchedule"
  restartPolicy: Never

Custom Pod Templates per Task

Pod Lifecycle States

StateDescriptionNext State
PendingPod created, waiting for schedulingScheduled
ScheduledNode assigned to podRunning
RunningContainer started, task executingSuccess or Failed
SucceededTask completed successfullyCleanup
FailedTask encountered an errorRetry (if retries > 0)
CleanupPod resources being releasedDeleted

Key Concepts Table

ComponentPurposeConfigurationImpact
ExecutorTask dispatchexecutor = KubernetesExecutorCore
NamespaceIsolationnamespace = airflowSecurity
Pod TemplatePod configurationpod_template_fileFlexibility
ResourcesCPU/Memory limitsworker_resource_*Performance
Node SelectorPod placementworker_node_selectorCost
TolerationsSchedule on tainted nodesworker_tolerationsAvailability
Service AccountRBACworker_service_account_nameSecurity

Code Examples

Advanced K8s Executor Configuration

Pod Monitoring and Cleanup

Resource-Aware Scheduling

Performance Metrics

K8s Executor vs Other Executors

MetricSequentialLocalCeleryKubernetes
Startup Time0s0s10-30s30-60s
IsolationNoneProcessContainerPod
ScalingManualManualStaticDynamic
Resource EfficiencyLowMediumMediumHigh
CostLowLowMediumVariable
Multi-tenancyNoNoLimitedYes

Pod Resource Optimization

Workload TypeCPU RequestMemory RequestCost/Pod-Hour
Light Task250m512Mi$0.02
Medium Task1 CPU2Gi$0.08
Heavy Task2 CPU4Gi$0.16
GPU Task2 CPU + 1 GPU8Gi$1.50

Best Practices for K8s Executor

  1. Set resource requests and limits to prevent pods from consuming excessive cluster resources
  2. Use pod templates to customize worker configurations for different workload types
  3. Enable pod cleanup (delete_worker_pods = True) to prevent resource leaks
  4. Configure node selectors to place pods on appropriate node pools
  5. Use service accounts with minimal RBAC permissions for security
  6. Monitor pod metrics to optimize resource allocation and costs

See Also

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Airflow Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement