Executes task in a Kubernetes POD

Module Contents



Execute a task in a Kubernetes Pod

exception airflow.providers.cncf.kubernetes.operators.kubernetes_pod.PodReattachFailure[source]

Bases: airflow.exceptions.AirflowException

When we expect to be able to find a pod but cannot.

class airflow.providers.cncf.kubernetes.operators.kubernetes_pod.KubernetesPodOperator(*, namespace=None, image=None, name=None, random_name_suffix=True, cmds=None, arguments=None, ports=None, volume_mounts=None, volumes=None, env_vars=None, env_from=None, secrets=None, in_cluster=None, cluster_context=None, labels=None, reattach_on_restart=True, startup_timeout_seconds=120, get_logs=True, image_pull_policy=None, annotations=None, resources=None, affinity=None, config_file=None, node_selectors=None, node_selector=None, image_pull_secrets=None, service_account_name=None, is_delete_operator_pod=True, hostnetwork=False, tolerations=None, security_context=None, dnspolicy=None, schedulername=None, full_pod_spec=None, init_containers=None, log_events_on_failure=False, do_xcom_push=False, pod_template_file=None, priority_class_name=None, pod_runtime_info_envs=None, termination_grace_period=None, configmaps=None, **kwargs)[source]

Bases: airflow.models.BaseOperator

Execute a task in a Kubernetes Pod

See also

For more information on how to use this operator, take a look at the guide: KubernetesPodOperator


If you use Google Kubernetes Engine and Airflow is not running in the same cluster, consider using GKEStartPodOperator, which simplifies the authorization process.

  • namespace (Optional[str]) -- the namespace to run within kubernetes.

  • image (Optional[str]) -- Docker image you wish to launch. Defaults to, but fully qualified URLS will point to custom repositories. (templated)

  • name (Optional[str]) -- name of the pod in which the task will run, will be used (plus a random suffix if random_name_suffix is True) to generate a pod id (DNS-1123 subdomain, containing only [a-z0-9.-]).

  • random_name_suffix (Optional[bool]) -- if True, will generate a random suffix.

  • cmds (Optional[List[str]]) -- entrypoint of the container. (templated) The docker images's entrypoint is used if this is not provided.

  • arguments (Optional[List[str]]) -- arguments of the entrypoint. (templated) The docker image's CMD is used if this is not provided.

  • ports (Optional[List[kubernetes.client.models.V1ContainerPort]]) -- ports for the launched pod.

  • volume_mounts (Optional[List[kubernetes.client.models.V1VolumeMount]]) -- volumeMounts for the launched pod.

  • volumes (Optional[List[kubernetes.client.models.V1Volume]]) -- volumes for the launched pod. Includes ConfigMaps and PersistentVolumes.

  • env_vars (Optional[List[kubernetes.client.models.V1EnvVar]]) -- Environment variables initialized in the container. (templated)

  • secrets (Optional[List[airflow.kubernetes.secret.Secret]]) -- Kubernetes secrets to inject in the container. They can be exposed as environment vars or files in a volume.

  • in_cluster (Optional[bool]) -- run kubernetes client with in_cluster configuration.

  • cluster_context (Optional[str]) -- context that points to kubernetes cluster. Ignored when in_cluster is True. If None, current-context is used.

  • reattach_on_restart (bool) -- if the worker dies while the pod is running, reattach and monitor during the next try. If False, always create a new pod for each try.

  • labels (Optional[Dict]) -- labels to apply to the Pod. (templated)

  • startup_timeout_seconds (int) -- timeout in seconds to startup the pod.

  • get_logs (bool) -- get the stdout of the container as logs of the tasks.

  • image_pull_policy (Optional[str]) -- Specify a policy to cache or always pull an image.

  • annotations (Optional[Dict]) -- non-identifying metadata you can attach to the Pod. Can be a large range of data, and can include characters that are not permitted by labels.

  • resources (Optional[kubernetes.client.models.V1ResourceRequirements]) -- resources for the launched pod.

  • affinity (Optional[kubernetes.client.models.V1Affinity]) -- affinity scheduling rules for the launched pod.

  • config_file (Optional[str]) -- The path to the Kubernetes config file. (templated) If not specified, default value is ~/.kube/config

  • node_selector (Optional[dict]) -- A dict containing a group of scheduling rules.

  • image_pull_secrets (Optional[List[kubernetes.client.models.V1LocalObjectReference]]) -- Any image pull secrets to be given to the pod. If more than one secret is required, provide a comma separated list: secret_a,secret_b

  • service_account_name (Optional[str]) -- Name of the service account

  • is_delete_operator_pod (bool) -- What to do when the pod reaches its final state, or the execution is interrupted. If True (default), delete the pod; if False, leave the pod.

  • hostnetwork (bool) -- If True enable host networking on the pod.

  • tolerations (Optional[List[kubernetes.client.models.V1Toleration]]) -- A list of kubernetes tolerations.

  • security_context (Optional[Dict]) -- security options the pod should run with (PodSecurityContext).

  • dnspolicy (Optional[str]) -- dnspolicy for the pod.

  • schedulername (Optional[str]) -- Specify a schedulername for the pod

  • full_pod_spec (Optional[kubernetes.client.models.V1Pod]) -- The complete podSpec

  • init_containers (Optional[List[kubernetes.client.models.V1Container]]) -- init container for the launched Pod

  • log_events_on_failure (bool) -- Log the pod's events if a failure occurs

  • do_xcom_push (bool) -- If True, the content of the file /airflow/xcom/return.json in the container will also be pushed to an XCom when the container completes.

  • pod_template_file (Optional[str]) -- path to pod template file (templated)

  • priority_class_name (Optional[str]) -- priority class name for the launched Pod

  • termination_grace_period (Optional[int]) -- Termination grace period if task killed in UI, defaults to kubernetes default

BASE_CONTAINER_NAME = base[source]
POD_CHECKED_KEY = already_checked[source]
template_fields :Sequence[str] = ['image', 'cmds', 'arguments', 'env_vars', 'labels', 'config_file', 'pod_template_file', 'namespace'][source]
find_pod(self, namespace, context)[source]

Returns an already-running pod for this task instance if one exists.

get_or_create_pod(self, pod_request_obj, context)[source]
await_pod_start(self, pod)[source]
extract_xcom(self, pod)[source]

Retrieves xcom value and kills xcom sidecar container

execute(self, context)[source]

This is the main method to derive when creating an operator. Context is the same dictionary used as when rendering jinja templates.

Refer to get_template_context for more context.

cleanup(self, pod, remote_pod)[source]
process_pod_deletion(self, pod)[source]
patch_already_checked(self, pod)[source]

Add an "already checked" annotation to ensure we don't reattach on retries


Override this method to cleanup subprocesses when a task instance gets killed. Any use of the threading, subprocess or multiprocessing module within an operator needs to be cleaned up or it will leave ghost processes behind.

build_pod_request_obj(self, context=None)[source]

Returns V1Pod object based on pod template file, full pod spec, and other operator parameters.

The V1Pod attributes are derived (in order of precedence) from operator params, full pod spec, pod template file.


Prints out the pod definition that would be created by this operator. Does not include labels specific to the task instance (since there isn't one in a dry_run) and excludes all empty elements.

Was this entry helpful?