For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
      • AstroFully-managed data operations, powered by Apache Airflow.
      • Astro Private CloudRun Airflow-as-a-service in your environment.
      • Professional ServicesExpert Airflow services for your enterprise's success.
    • Tools
      • Cosmos
      • Orbiter
      • CLI
      • AI SDK
      • Agents
      • Blueprint
      • UpdatesThe State of Airflow 2026See the insights from over 5,800 data practitioners in the full report. Download Now ➔
  • Customers
  • Docs
    • Insights
      • Blog
      • Webinars
      • Resource Library
      • Events
    • Education
      • Academy
      • What is Airflow?
  • Pricing
Get Started Free
    • Overview
      • Overview
        • Set up KubernetesPodOperator
        • Configure task-level resources
        • Mount a temporary directory
        • Run your Deployment's current Airflow image
        • Use secret environment variables
        • Use the @task.kubernetes decorator
      • Dag versioning
      • Airflow logs
      • DAG and task runs
      • Airflow REST API
    • Book Office Hours

Product

  • Platform Overview
  • Astro
  • Astro Observe
  • Astro Private Cloud
  • Security & Trust
  • Pricing

Tools & Services

  • Cosmos
  • Docs
  • Professional Services
  • Product Updates

Use Cases

  • AI Ops
  • Data Observability
  • ETL/ELT
  • ML Ops
  • Operational Analytics
  • All Use Cases

Industries

  • Financial Services
  • Gaming
  • Retail
  • Manufacturing
  • Healthcare
  • All Industries

Resources

  • Academy
  • eBooks & Guides
  • Blog
  • Webinars
  • Events
  • The Data Flowcast Podcast
  • All Resources

Airflow

  • What is Airflow
  • Airflow on Astro
  • Airflow 3.0
  • Airflow Upgrades
  • Airflow Use Cases
  • Airflow 2.x End of Life

Company

  • Our Story
  • Customers
  • Newsroom
  • Careers
  • Contact

Support

  • Knowledge Base
  • Status
  • Contact Support
GitHubYouTubeLinkedInx
  • Legal
  • Privacy
  • Terms of Service
  • Consent Preferences

  • Do Not Sell or Share My Personal information
  • Limit the Use Of My Sensitive Personal Information

Apache Airflow®, Airflow, and the Airflow logo are trademarks of the Apache Software Foundation. Copyright © Astronomer 2026. All rights reserved.

LogoLogo
Develop DAGsKubernetesPodOperator

Use the @task.kubernetes decorator

Edit this page
Built with

The @task.kubernetes decorator provides a TaskFlow alternative to the traditional KubernetesPodOperator, which allows you to run a specified task in its own Kubernetes pod. Note that the Docker image provided to the @task.kubernetes decorator’s image parameter must support executing Python scripts in order to leverage the KubernetesPodOperator decorator.

Like regular @task decorated functions, XComs can be passed to the Python script running in the dedicated Kubernetes pod. If do_xcom_push is set to True in the decorator parameters, the value returned by the decorated function is pushed to XCom.

Astronomer recommends using the @task.kubernetes decorator instead of the KubernetesPodOperator when using XCom with Python scripts in a dedicated Kubernetes pod.

1from pendulum import datetime
2from airflow.configuration import conf
3from airflow.decorators import dag, task
4import random
5
6# get the current Kubernetes namespace Airflow is running in
7namespace = conf.get("kubernetes", "NAMESPACE")
8
9@dag(
10 start_date=datetime(2023, 1, 1),
11 catchup=False,
12 schedule="@daily",
13)
14def kubernetes_decorator_example_dag():
15 @task
16 def extract_data():
17 # simulating querying from a database
18 data_point = random.randint(0, 100)
19 return data_point
20
21 @task.kubernetes(
22 # specify the Docker image to launch, it needs to be able to run a Python script
23 image="python",
24 # launch the Pod on the same cluster as Airflow is running on
25 in_cluster=True,
26 # launch the Pod in the same namespace as Airflow is running in
27 namespace=namespace,
28 # Pod configuration
29 # naming the Pod
30 name="my_pod",
31 # log stdout of the container as task logs
32 get_logs=True,
33 # log events in case of Pod failure
34 log_events_on_failure=True,
35 # enable pushing to XCom
36 do_xcom_push=True,
37 )
38 def transform(data_point):
39 multiplied_data_point = 23 * int(data_point)
40 return multiplied_data_point
41
42 @task
43 def load_data(**context):
44 # pull the XCom value that has been pushed by the KubernetesPodOperator
45 transformed_data_point = context["ti"].xcom_pull(
46 task_ids="transform", key="return_value"
47 )
48 print(transformed_data_point)
49
50 load_data(transform(extract_data()))
51
52
53kubernetes_decorator_example_dag()