Kubernetes Executor Deep Dive
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
| State | Description | Next State |
|---|---|---|
| Pending | Pod created, waiting for scheduling | Scheduled |
| Scheduled | Node assigned to pod | Running |
| Running | Container started, task executing | Success or Failed |
| Succeeded | Task completed successfully | Cleanup |
| Failed | Task encountered an error | Retry (if retries > 0) |
| Cleanup | Pod resources being released | Deleted |
Key Concepts Table
| Component | Purpose | Configuration | Impact |
|---|---|---|---|
| Executor | Task dispatch | executor = KubernetesExecutor | Core |
| Namespace | Isolation | namespace = airflow | Security |
| Pod Template | Pod configuration | pod_template_file | Flexibility |
| Resources | CPU/Memory limits | worker_resource_* | Performance |
| Node Selector | Pod placement | worker_node_selector | Cost |
| Tolerations | Schedule on tainted nodes | worker_tolerations | Availability |
| Service Account | RBAC | worker_service_account_name | Security |
Code Examples
Advanced K8s Executor Configuration
Pod Monitoring and Cleanup
Resource-Aware Scheduling
Performance Metrics
K8s Executor vs Other Executors
| Metric | Sequential | Local | Celery | Kubernetes |
|---|---|---|---|---|
| Startup Time | 0s | 0s | 10-30s | 30-60s |
| Isolation | None | Process | Container | Pod |
| Scaling | Manual | Manual | Static | Dynamic |
| Resource Efficiency | Low | Medium | Medium | High |
| Cost | Low | Low | Medium | Variable |
| Multi-tenancy | No | No | Limited | Yes |
Pod Resource Optimization
| Workload Type | CPU Request | Memory Request | Cost/Pod-Hour |
|---|---|---|---|
| Light Task | 250m | 512Mi | $0.02 |
| Medium Task | 1 CPU | 2Gi | $0.08 |
| Heavy Task | 2 CPU | 4Gi | $0.16 |
| GPU Task | 2 CPU + 1 GPU | 8Gi | $1.50 |
Best Practices for K8s Executor
- Set resource requests and limits to prevent pods from consuming excessive cluster resources
- Use pod templates to customize worker configurations for different workload types
- Enable pod cleanup (
delete_worker_pods = True) to prevent resource leaks - Configure node selectors to place pods on appropriate node pools
- Use service accounts with minimal RBAC permissions for security
- Monitor pod metrics to optimize resource allocation and costs
See Also
- Executors Comparison â Comparing all executor types
- Performance Tuning â Optimizing executor performance
- Monitoring and Alerting â Monitoring pod metrics
- Multi-Tenancy â Isolating workloads with K8s