使用 KubernetesPodOperator

Managed Airflow(第 3 代) | Managed Airflow(第 2 代) | Managed Airflow(旧版第 1 代)

本页面介绍了如何使用 KubernetesPodOperator 将 Managed Service for Apache Airflow 中的 Kubernetes Pod 部署到 Managed Service for Apache Airflow 环境中的 Google Kubernetes Engine 集群。

KubernetesPodOperator 会在环境的集群中启动 Kubernetes Pod。相比之下,Google Kubernetes Engine 运算符在指定集群中运行 Kubernetes Pod,该集群可以是与您的环境无关的单独集群。您还可以使用 Google Kubernetes Engine Operator 创建和删除集群。

如果您需要以下功能,KubernetesPodOperator 是一个不错的选择:

  • 无法通过公共 PyPI 代码库获得的自定义 Python 依赖项。
  • 库存 Managed Airflow 工作器映像中不提供的二进制依赖项。

准备工作

查看以下列表,了解 Managed Airflow(第 3 代)和 Managed Airflow(第 2 代)中 KubernetesPodOperator 之间的差异,并确保您的 DAG 兼容:

  • 无法在 Managed Airflow(第 3 代)中创建自定义命名空间。Pod 始终在 composer-user-workloads 命名空间中运行,即使指定了其他命名空间也是如此。此命名空间中的 Pod 无需额外配置即可访问项目的资源和 VPC 网络(如果已启用)。

  • 无法在 Managed Airflow(第 3 代)中运行多个额外的边车容器。如果边车容器名为 airflow-xcom-sidecar,则可以运行单个边车容器。

  • 无法使用 Kubernetes API 创建 Kubernetes Secret 和 ConfigMap。 Managed Airflow 提供了 Google Cloud CLI 命令、Terraform 资源和 Cloud Composer API 来管理 Kubernetes Secret 和 ConfigMap。如需了解详情,请参阅使用 Kubernetes Secret 和 ConfigMap。

  • 无法在 Managed Airflow(第 3 代)中部署自定义工作负载。只能修改 Kubernetes Secret 和 ConfigMap,但无法进行所有其他配置更改。

  • 必须使用支持的值指定资源要求(CPU、内存和存储空间)。

  • 与 Managed Airflow(第 2 代)一样,Pod 亲和性配置不可用。如果您想使用 Pod 亲和性,请改用 GKE 运算符在其他集群中启动 Pod。

关于 Managed Airflow(第 3 代)中的 KubernetesPodOperator

本部分介绍了 KubernetesPodOperator 在 Managed Airflow(第 3 代)中的运作方式。

资源使用情况

在 Managed Airflow(第 3 代)中,环境的集群会自动扩缩。您使用 KubernetesPodOperator 运行的额外工作负载会独立于您的环境进行扩缩。您的环境不受资源需求增加的影响,但环境的集群会根据资源需求进行扩缩。

在环境集群中运行的额外工作负载的价格遵循 Managed Airflow(第 3 代)价格模式,并使用 Managed Airflow(第 3 代)SKU。

Managed Airflow(第 3 代)使用 Autopilot 集群,这些集群引入了计算类的概念:

  • Managed Airflow 仅支持 general-purpose 计算类。

  • 默认情况下,如果您在使用 KubernetesPodOperator 创建 Pod 时未选择任何类,则系统会假定使用 general-purpose 类。

  • 每个类都与特定属性和资源限制相关联,您可以在 Autopilot 文档中了解这些属性和限制。例如,在 general-purpose 类中运行的 Pod 最多可以使用 110 GiB 的内存。

拥有对项目资源的访问权限

在 Managed Airflow(第 3 代)中,环境的集群位于租户项目中,无法进行配置。Pod 在环境的集群中的隔离命名空间内执行。

在 Managed Airflow(第 3 代)中,即使指定了其他命名空间,Pod 也始终在 composer-user-workloads 命名空间中运行。此命名空间中的 Pod 可以访问 Google Cloud 项目中的资源和您的 VPC 网络(如果已启用),而无需进行其他配置。 您环境的服务账号用于访问这些资源。无法指定其他服务账号。

最少的配置工作量

如需创建 KubernetesPodOperator,只需提供 Pod 的 name、要使用的 image 和 task_id 参数。/home/airflow/composer_kube_config 包含用于向 GKE 进行身份验证的凭据。

kubernetes_min_pod = KubernetesPodOperator(
    # The ID specified for the task.
    task_id="pod-ex-minimum",
    # Name of task you want to run, used to generate Pod ID.
    name="pod-ex-minimum",
    # Entrypoint of the container, if not specified the Docker container's
    # entrypoint is used. The cmds parameter is templated.
    cmds=["echo"],
    # The namespace to run within Kubernetes. In Composer 2 environments
    # after December 2022, the default namespace is
    # `composer-user-workloads`. Always use the
    # `composer-user-workloads` namespace with Composer 3.
    namespace="composer-user-workloads",
    # Docker image specified. Defaults to hub.docker.com, but any fully
    # qualified URLs will point to a custom repository. Supports private
    # gcr.io images if the Composer Environment is under the same
    # project-id as the gcr.io images and the service account that Composer
    # uses has permission to access the Google Container Registry
    # (the default service account has permission)
    image="marketplace.gcr.io/google/ubuntu2204",
    # Specifies path to kubernetes config. The config_file is templated.
    config_file="/home/airflow/composer_kube_config",
    # Identifier of connection that should be used
    kubernetes_conn_id="kubernetes_default",
)

其他配置

以下示例展示了您可以在 KubernetesPodOperator 中配置的其他参数。

如需了解详情,请参阅以下资源:

kubernetes_full_pod = KubernetesPodOperator(
    task_id="ex-all-configs",
    name="pi",
    namespace="composer-user-workloads",
    image="perl:5.34.0",
    # Entrypoint of the container, if not specified the Docker container's
    # entrypoint is used. The cmds parameter is templated.
    cmds=["perl"],
    # Arguments to the entrypoint. The Docker image's CMD is used if this
    # is not provided. The arguments parameter is templated.
    arguments=["-Mbignum=bpi", "-wle", "print bpi(2000)"],
    # The secrets to pass to Pod, the Pod will fail to create if the
    # secrets you specify in a Secret object do not exist in Kubernetes.
    secrets=[],
    # Labels to apply to the Pod.
    labels={"pod-label": "label-name"},
    # Timeout to start up the Pod, default is 600.
    startup_timeout_seconds=600,
    # The environment variables to be initialized in the container.
    # The env_vars parameter is templated.
    env_vars={"EXAMPLE_VAR": "/example/value"},
    # If true, logs stdout output of container. Defaults to True.
    get_logs=True,
    # Determines when to pull a fresh image, if 'IfNotPresent' will cause
    # the Kubelet to skip pulling an image if it already exists. If you
    # want to always pull a new image, set it to 'Always'.
    image_pull_policy="Always",
    # Annotations are 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.
    annotations={"key1": "value1"},
    # Optional resource specifications for Pod, this will allow you to
    # set both cpu and memory limits and requirements.
    # Prior to Airflow 2.3 and the cncf providers package 5.0.0
    # resources were passed as a dictionary. This change was made in
    # https://github.com/apache/airflow/pull/27197
    # Additionally, "memory" and "cpu" were previously named
    # "limit_memory" and "limit_cpu"
    # resources={'limit_memory': "250M", 'limit_cpu': "100m"},
    container_resources=k8s_models.V1ResourceRequirements(
        requests={"cpu": "1000m", "memory": "10G", "ephemeral-storage": "10G"},
        limits={"cpu": "1000m", "memory": "10G", "ephemeral-storage": "10G"},
    ),
    # Specifies path to kubernetes config. The config_file is templated.
    config_file="/home/airflow/composer_kube_config",
    # If true, the content of /airflow/xcom/return.json from container will
    # also be pushed to an XCom when the container ends.
    do_xcom_push=False,
    # List of Volume objects to pass to the Pod.
    volumes=[],
    # List of VolumeMount objects to pass to the Pod.
    volume_mounts=[],
    # Identifier of connection that should be used
    kubernetes_conn_id="kubernetes_default",
    # Affinity determines which nodes the Pod can run on based on the
    # config. For more information see:
    # https://kubernetes.io/docs/concepts/configuration/assign-pod-node/
    # Pod affinity with the KubernetesPodOperator
    # is not supported with Composer 2
    # instead, create a cluster and use the GKEStartPodOperator
    # https://cloud.google.com/composer/docs/using-gke-operator
    affinity={},
)

使用 Jinja 模板

Airflow 支持 DAG 中的 Jinja 模板。

您必须使用运算符声明必需的 Airflow 参数(task_id、name 和 image)。如以下示例所示,您可以使用 Jinja 对所有其他参数进行模板化,包括 cmds、arguments、env_vars 和 config_file。

示例中的 env_vars 参数是通过名为 my_value 的 Airflow 变量设置的。示例 DAG 从 Airflow 中的 vars 模板变量获取其值。Airflow 还有更多变量,可用于访问不同类型的信息。例如,您可以使用 conf 模板变量访问 Airflow 配置选项的值。如需了解详情以及 Airflow 中可用的变量列表,请参阅 Airflow 文档中的模板参考。

如果不更改 DAG 或创建 env_vars 变量,示例中的 ex-kube-templates 任务会失败,因为该变量不存在。在 Airflow 界面中或使用 Google Cloud CLI 创建此变量:

Airflow 界面

  1. 转到 Airflow 界面。

  2. 在工具栏中,依次选择 Admin(管理员)> Variables(变量)。

  3. 在 List Variable 页面上,点击 Add a new record。

  4. 在 Add Variable 页面上,输入以下信息:

    • Key:my_value
    • Val:example_value
  5. 点击保存。

gcloud

输入以下命令:

gcloud composer environments run ENVIRONMENT \
    --location LOCATION \
    variables set -- \
    my_value example_value

将

  • ENVIRONMENT 替换为环境的名称。
  • LOCATION 替换为环境所在的区域。

以下示例演示了如何将 Jinja 模板与 KubernetesPodOperator 搭配使用:

kubernetes_template_ex = KubernetesPodOperator(
    task_id="ex-kube-templates",
    name="ex-kube-templates",
    namespace="composer-user-workloads",
    image="bash",
    # All parameters below can be templated with Jinja. For more information
    # and the list of variables available in Airflow, see
    # the Airflow templates reference:
    # https://airflow.apache.org/docs/apache-airflow/stable/templates-ref.html
    # Entrypoint of the container, if not specified the Docker container's
    # entrypoint is used. The cmds parameter is templated.
    cmds=["echo"],
    # DS in Jinja is the execution date as YYYY-MM-DD, this Docker image
    # will echo the execution date. Arguments to the entrypoint. The Docker
    # image's CMD is used if this is not provided. The arguments parameter
    # is templated.
    arguments=["{{ ds }}"],
    # The var template variable allows you to access variables defined in
    # Airflow UI. In this case we are getting the value of my_value and
    # setting the environment variable `MY_VALUE`. The pod will fail if
    # `my_value` is not set in the Airflow UI. The env_vars parameter
    # is templated.
    env_vars={"MY_VALUE": "{{ var.value.my_value }}"},
    # Specifies path to Kubernetes config. The config_file is templated.
    config_file="/home/airflow/composer_kube_config",
    # Identifier of connection that should be used
    kubernetes_conn_id="kubernetes_default",
)

使用 Kubernetes Secret 和 ConfigMap

Kubernetes Secret 是一个包含敏感数据的对象。Kubernetes ConfigMap 是一个对象,其中包含键值对形式的非机密数据。

在 Managed Airflow(第 3 代)中,您可以使用 Google Cloud CLI、API 或 Terraform 创建 Secret 和 ConfigMap,然后通过 KubernetesPodOperator 访问它们:

  • 使用 Google Cloud CLI 和 API 时,您需要提供 YAML 配置文件。
  • 借助 Terraform,您可以在 Terraform 配置文件中将 Secret 和 ConfigMap 定义为单独的资源。

关于 YAML 配置文件

使用 Google Cloud CLI 和 API 创建 Kubernetes Secret 或 ConfigMap 时,您需要提供 YAML 格式的文件。此文件必须遵循 Kubernetes Secret 和 ConfigMap 使用的相同格式。Kubernetes 文档提供了许多 ConfigMap 和 Secret 的代码示例。如需开始使用,您可以参阅使用 Secret 安全地分发凭据页面和 ConfigMaps。

与 Kubernetes Secret 中一样,在 Secret 中定义值时,请使用 base64 表示法。

如需对值进行编码,您可以使用以下命令(这是获取 base64 编码值的多种方法之一):

echo "postgresql+psycopg2://root:example-password@127.0.0.1:3306/example-db" -n | base64

输出:

cG9zdGdyZXNxbCtwc3ljb3BnMjovL3Jvb3Q6ZXhhbXBsZS1wYXNzd29yZEAxMjcuMC4wLjE6MzMwNi9leGFtcGxlLWRiIC1uCg==

本指南后面的示例中使用了以下两个 YAML 文件示例。 Kubernetes Secret 的 YAML 配置文件示例:

apiVersion: v1
kind: Secret
metadata:
  name: airflow-secrets
data:
  sql_alchemy_conn: cG9zdGdyZXNxbCtwc3ljb3BnMjovL3Jvb3Q6ZXhhbXBsZS1wYXNzd29yZEAxMjcuMC4wLjE6MzMwNi9leGFtcGxlLWRiIC1uCg==

另一个演示如何包含文件的示例。与上一个示例一样,首先对文件 (cat ./key.json | base64) 的内容进行编码,然后在 YAML 文件中提供此值:

apiVersion: v1
kind: Secret
metadata:
  name: service-account
data:
  service-account.json: |
    ewogICJ0eXBl...mdzZXJ2aWNlYWNjb3VudC5jb20iCn0K

ConfigMap 的 YAML 配置文件示例。您无需在 ConfigMap 中使用 base64 表示法:

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-configmap
data:
  example_key: example_value

管理 Kubernetes Secret

gcloud

创建 Secret

如需创建 Kubernetes Secret,请运行以下命令:

gcloud beta composer environments user-workloads-secrets create \
  --environment ENVIRONMENT_NAME \
  --location LOCATION \
  --secret-file-path SECRET_FILE

替换以下内容:

  • ENVIRONMENT_NAME:您的环境的名称。
  • LOCATION:环境所在的区域。
  • SECRET_FILE:包含 Secret 配置的本地 YAML 文件的路径。

示例:

gcloud beta composer environments user-workloads-secrets create \
  --environment example-environment \
  --location us-central1 \
  --secret-file-path ./secrets/example-secret.yaml

更新 Secret

如需更新 Kubernetes Secret,请运行以下命令。Secret 的名称将取自指定的 YAML 文件,Secret 的内容将被替换。

gcloud beta composer environments user-workloads-secrets update \
  --environment ENVIRONMENT_NAME \
  --location LOCATION \
  --secret-file-path SECRET_FILE

替换以下内容:

  • ENVIRONMENT_NAME:您的环境的名称。
  • LOCATION:环境所在的区域。
  • SECRET_FILE:包含 Secret 配置的本地 YAML 文件的路径。在此文件的 metadata > name 字段中指定 Secret 的名称。

列出 Secret

如需获取环境的 Secret 及其字段的列表,请运行以下命令。输出中的键值将替换为星号。

gcloud beta composer environments user-workloads-secrets list \
  --environment ENVIRONMENT_NAME \
  --location LOCATION

替换以下内容:

  • ENVIRONMENT_NAME:您的环境的名称。
  • LOCATION:环境所在的区域。

获取 Secret 的详细信息

如需获取有关 Secret 的详细信息,请运行以下命令。输出中的键值将替换为星号。

gcloud beta composer environments user-workloads-secrets describe \
  SECRET_NAME \
  --environment ENVIRONMENT_NAME \
  --location LOCATION

替换以下内容:

  • SECRET_NAME:Secret 的名称,在包含 Secret 配置的 YAML 文件中的 metadata > name 字段中定义。
  • ENVIRONMENT_NAME:您的环境的名称。
  • LOCATION:环境所在的区域。

删除 Secret

如需删除 Secret,请运行以下命令:

gcloud beta composer environments user-workloads-secrets delete \
  SECRET_NAME \
  --environment ENVIRONMENT_NAME \
  --location LOCATION
  • SECRET_NAME:Secret 的名称,在包含 Secret 配置的 YAML 文件中的 metadata > name 字段中定义。
  • ENVIRONMENT_NAME:您的环境的名称。
  • LOCATION:环境所在的区域。

API

创建 Secret

  1. 创建 environments.userWorkloadsSecrets.create API 请求。

  2. 在此请求中:

    1. 在请求正文的 name 字段中,指定新 Secret 的 URI。
    2. 在请求正文的 data 字段中,为 Secret 指定键和 base64 编码值。
    3. (可选,仅限 Beta 版 Cloud Composer API)在请求正文的 type 字段中,指定密钥类型。支持以下密钥类型:

      • (默认)Opaque
      • kubernetes.io/dockerconfigjson

示例:

// POST https://composer.googleapis.com/v1beta1/projects/example-project/
// locations/us-central1/environments/example-environment/userWorkloadsSecrets

{
  "name": "projects/example-project/locations/us-central1/environments/example-environment/userWorkloadsSecrets/example-secret",
  "data": {
    "example": "ZXhhbXBsZV92YWx1ZSAtbgo="
  }

}

kubernetes.io/dockerconfigjson 类型示例:

// POST https://composer.googleapis.com/v1beta1/projects/example-project/
// locations/us-central1/environments/example-environment/userWorkloadsSecrets

{
  "name": "projects/example-project/locations/us-central1/environments/example-environment/userWorkloadsSecrets/example-secret",
  "data": {
    ".dockerconfigjson": "eyJhdXRocyI6eyJodHRwczovL2luZGV4LmRvY2tlci5pby92MS8iOnsidXNlcm5hbWUiOiJleGFtcGxlX3VzZXIiLCJwYXNzd29yZCI6ImV4YW1wbGVfcGFzc3dvcmQiLCJlbWFpbCI6ImVtYWlsQGV4YW1wbGUuY29tIiwiYXV0aCI6ImRYTmxjanB0ZVhCaGMzTT0ifX19"
  },
  "type": "kubernetes.io/dockerconfigjson"
}

更新 Secret

  1. 创建 environments.userWorkloadsSecrets.update API 请求。

  2. 在此请求中:

    1. 在请求正文的 name 字段中,指定 Secret 的 URI。
    2. 在请求正文的 data 字段中,为 Secret 指定键和 base64 编码值。这些值将被替换。

示例:

// PUT https://composer.googleapis.com/v1beta1/projects/example-project/
// locations/us-central1/environments/example-environment/userWorkloadsSecrets/example-secret

{
  "name": "projects/example-project/locations/us-central1/environments/example-environment/userWorkloadsSecrets/example-secret",
  "data": {
    "example": "ZXhhbXBsZV92YWx1ZSAtbgo=",
    "another-example": "YW5vdGhlcl9leGFtcGxlX3ZhbHVlIC1uCg=="
  }
}

列出 Secret

创建 environments.userWorkloadsSecrets.list API 请求。输出中的键值将替换为星号。此请求可以使用分页,如需了解详情,请参阅请求的参考文档。

示例:

// GET https://composer.googleapis.com/v1beta1/projects/example-project/
// locations/us-central1/environments/example-environment/userWorkloadsSecrets

获取 Secret 的详细信息

创建 environments.userWorkloadsSecrets.get API 请求。输出中的键值将替换为星号。

示例:

// GET https://composer.googleapis.com/v1beta1/projects/example-project/
// locations/us-central1/environments/example-environment/userWorkloadsSecrets/example-secret

删除 Secret

创建 environments.userWorkloadsSecrets.delete API 请求。

示例:

// DELETE https://composer.googleapis.com/v1beta1/projects/example-project/
// locations/us-central1/environments/example-environment/userWorkloadsSecrets/example-secret

Terraform

google_composer_user_workloads_secret 资源定义了一个 Kubernetes Secret,其键和值在 data 块中定义。

resource "google_composer_user_workloads_secret" "example_secret" {
  provider = google-beta
  environment = google_composer_environment.ENVIRONMENT_RESOURCE_NAME.name
  name = "SECRET_NAME"
  region = "LOCATION"

  data = {
    KEY_NAME: "KEY_VALUE"
  }
}
  • ENVIRONMENT_RESOURCE_NAME:环境资源的名称,其中包含 Terraform 中的环境定义。此资源中还指定了实际环境的名称。
  • LOCATION:环境所在的区域。
  • SECRET_NAME:Secret 的名称。
  • KEY_NAME:此 Secret 的一个或多个密钥。
  • KEY_VALUE:密钥的 base64 编码值。您可以使用 base64encode 函数对值进行编码(请参阅示例)。

本指南后面的示例中使用了以下两个 Kubernetes Secret 示例。

resource "google_composer_user_workloads_secret" "example_secret" {
  provider = google-beta

  name = "airflow-secrets"

  environment = google_composer_environment.example_environment.name
  region = "us-central1"

  data = {
    sql_alchemy_conn: base64encode("postgresql+psycopg2://root:example-password@127.0.0.1:3306/example-db")
  }
}

另一个演示如何包含文件的示例。您可以使用 file 函数将文件内容读取为字符串,然后对其进行 base64 编码:

resource "google_composer_user_workloads_secret" "service_account_secret" {
  provider = google-beta

  name = "service-account"

  environment = google_composer_environment.example_environment.name
  region = "us-central1"

  data = {
    "service-account.json": base64encode(file("./key.json"))
  }
}

在 DAG 中使用 Kubernetes Secret

此示例展示了使用 Kubernetes Secret 的两种方式:作为环境变量,以及作为 Pod 装载的卷。

第一个 Secret airflow-secrets 被设置为名为 SQL_CONN 的 Kubernetes 环境变量(而不是 Airflow 或 Managed Airflow 环境变量)。

第二个 Secret service-account 将 service-account.json(包含服务账号令牌的文件)装载到 /var/secrets/google。

Secret 对象如下所示:

secret_env = Secret(
    # Expose the secret as environment variable.
    deploy_type="env",
    # The name of the environment variable, since deploy_type is `env` rather
    # than `volume`.
    deploy_target="SQL_CONN",
    # Name of the Kubernetes Secret
    secret="airflow-secrets",
    # Key of a secret stored in this Secret object
    key="sql_alchemy_conn",
)
secret_volume = Secret(
    deploy_type="volume",
    # Path where we mount the secret as volume
    deploy_target="/var/secrets/google",
    # Name of Kubernetes Secret
    secret="service-account",
    # Key in the form of service account file name
    key="service-account.json",
)

第一个 Kubernetes Secret 的名称在 secret_env 变量中定义。 此 Secret 名为 airflow-secrets。deploy_type 参数指定必须将其公开为环境变量。环境变量的名称为 SQL_CONN,如 deploy_target 参数中所指定。最后,SQL_CONN 环境变量的值会设置为 sql_alchemy_conn 键的值。

第二个 Kubernetes Secret 的名称在 secret_volume 变量中定义。此 Secret 名为 service-account。它会作为卷公开,如 deploy_type 参数中所指定。要装载的文件 deploy_target 的路径为 /var/secrets/google。最后,存储在 deploy_target 中的 Secret 的 key 为 service-account.json。

操作节点配置类似于如下所示:

kubernetes_secret_vars_ex = KubernetesPodOperator(
    task_id="ex-kube-secrets",
    name="ex-kube-secrets",
    namespace="composer-user-workloads",
    image="marketplace.gcr.io/google/ubuntu2204",
    startup_timeout_seconds=300,
    # The secrets to pass to Pod, the Pod will fail to create if the
    # secrets you specify in a Secret object do not exist in Kubernetes.
    secrets=[secret_env, secret_volume],
    # Entrypoint of the container, if not specified the Docker container's
    # entrypoint is used. The cmds parameter is templated.
    cmds=["echo"],
    # env_vars allows you to specify environment variables for your
    # container to use. The env_vars parameter is templated.
    env_vars={
        "EXAMPLE_VAR": "/example/value",
        "GOOGLE_APPLICATION_CREDENTIALS": "/var/secrets/google/service-account.json",
    },
    # Specifies path to kubernetes config. The config_file is templated.
    config_file="/home/airflow/composer_kube_config",
    # Identifier of connection that should be used
    kubernetes_conn_id="kubernetes_default",
)

管理 Kubernetes ConfigMap

gcloud

创建 ConfigMap

如需创建 ConfigMap,请运行以下命令:

gcloud beta composer environments user-workloads-config-maps create \
  --environment ENVIRONMENT_NAME \
  --location LOCATION \
  --config-map-file-path CONFIG_MAP_FILE

替换以下内容:

  • ENVIRONMENT_NAME:您的环境的名称。
  • LOCATION:环境所在的区域。
  • CONFIG_MAP_FILE:包含 ConfigMap 配置的本地 YAML 文件的路径。

示例:

gcloud beta composer environments user-workloads-config-maps create \
  --environment example-environment \
  --location us-central1 \
  --config-map-file-path ./configs/example-configmap.yaml

更新 ConfigMap

如需更新 ConfigMap,请运行以下命令。ConfigMap 的名称将取自指定的 YAML 文件,并且 ConfigMap 的内容将被替换。

gcloud beta composer environments user-workloads-config-maps update \
  --environment ENVIRONMENT_NAME \
  --location LOCATION \
  --config-map-file-path CONFIG_MAP_FILE

替换以下内容:

  • ENVIRONMENT_NAME:您的环境的名称。
  • LOCATION:环境所在的区域。
  • CONFIG_MAP_FILE:包含 ConfigMap 配置的本地 YAML 文件的路径。在此文件的 metadata > name 字段中指定 ConfigMap 的名称。

列出 ConfigMap

如需获取环境的 ConfigMap 及其字段的列表,请运行以下命令。输出中的键值将按原样显示。

gcloud beta composer environments user-workloads-config-maps list \
  --environment ENVIRONMENT_NAME \
  --location LOCATION

替换以下内容:

  • ENVIRONMENT_NAME:您的环境的名称。
  • LOCATION:环境所在的区域。

获取 ConfigMap 的详细信息

如需获取有关 ConfigMap 的详细信息,请运行以下命令。输出中的键值将按原样显示。

gcloud beta composer environments user-workloads-config-maps describe \
  CONFIG_MAP_NAME \
  --environment ENVIRONMENT_NAME \
  --location LOCATION

替换以下内容:

  • CONFIG_MAP_NAME:ConfigMap 的名称,在包含 ConfigMap 配置的 YAML 文件中的 metadata > name 字段中定义。
  • ENVIRONMENT_NAME:您的环境的名称。
  • LOCATION:环境所在的区域。

删除 ConfigMap

如需删除 ConfigMap,请运行以下命令:

gcloud beta composer environments user-workloads-config-maps delete \
  CONFIG_MAP_NAME \
  --environment ENVIRONMENT_NAME \
  --location LOCATION
  • CONFIG_MAP_NAME:ConfigMap 的名称,在包含 ConfigMap 配置的 YAML 文件中的 metadata > name 字段中定义。
  • ENVIRONMENT_NAME:您的环境的名称。
  • LOCATION:环境所在的区域。

API

创建 ConfigMap

  1. 创建 environments.userWorkloadsConfigMaps.create API 请求。

  2. 在此请求中:

    1. 在请求正文的 name 字段中,指定新 ConfigMap 的 URI。
    2. 在请求正文的 data 字段中,指定 ConfigMap 的键和值。

示例:

// POST https://composer.googleapis.com/v1beta1/projects/example-project/
// locations/us-central1/environments/example-environment/userWorkloadsConfigMaps

{
  "name": "projects/example-project/locations/us-central1/environments/example-environment/userWorkloadsConfigMaps/example-configmap",
  "data": {
    "example_key": "example_value"
  }
}

更新 ConfigMap

  1. 创建 environments.userWorkloadsConfigMaps.update API 请求。

  2. 在此请求中:

    1. 在请求正文的 name 字段中,指定 ConfigMap 的 URI。
    2. 在请求正文的 data 字段中,指定 ConfigMap 的键和值。这些值将被替换。

示例:

// PUT https://composer.googleapis.com/v1beta1/projects/example-project/
// locations/us-central1/environments/example-environment/userWorkloadsConfigMaps/example-configmap

{
  "name": "projects/example-project/locations/us-central1/environments/example-environment/userWorkloadsConfigMaps/example-configmap",
  "data": {
    "example_key": "example_value",
    "another_key": "another_value"
  }
}

列出 ConfigMap

创建 environments.userWorkloadsConfigMaps.list API 请求。输出中的键值对将按原样显示。此请求可以使用分页,如需了解详情,请参阅请求的参考文档。

示例:

// GET https://composer.googleapis.com/v1beta1/projects/example-project/
// locations/us-central1/environments/example-environment/userWorkloadsConfigMaps

获取 ConfigMap 的详细信息

创建 environments.userWorkloadsConfigMaps.get API 请求。输出中的键值将按原样显示。

示例:

// GET https://composer.googleapis.com/v1beta1/projects/example-project/
// locations/us-central1/environments/example-environment/userWorkloadsConfigMaps/example-configmap

删除 ConfigMap

创建 environments.userWorkloadsConfigMaps.delete API 请求。

示例:

// DELETE https://composer.googleapis.com/v1beta1/projects/example-project/
// locations/us-central1/environments/example-environment/userWorkloadsConfigMaps/example-configmap

Terraform

google_composer_user_workloads_config_map 资源定义了一个 ConfigMap,其键和值在 data 块中定义。

resource "google_composer_user_workloads_config_map" "example_config_map" {
  provider = google-beta
  environment = google_composer_environment.ENVIRONMENT_RESOURCE_NAME.name
  name = "CONFIG_MAP_NAME"
  region = "LOCATION"

  data = {
    KEY_NAME: "KEY_VALUE"
  }
}
  • ENVIRONMENT_RESOURCE_NAME:环境资源的名称,其中包含 Terraform 中的环境定义。此资源中还指定了实际环境的名称。
  • LOCATION:环境所在的区域。
  • CONFIG_MAP_NAME:ConfigMap 的名称。
  • KEY_NAME:此 ConfigMap 的一个或多个键。
  • KEY_VALUE:键的值。

示例:

resource "google_composer_user_workloads_config_map" "example_config_map" {
  provider = google-beta

  name = "example-config-map"

  environment = google_composer_environment.example_environment.name
  region = "us-central1"

  data = {
    "example_key": "example_value"
  }
}

在 DAG 中使用 ConfigMap

此示例展示了如何在 DAG 中使用 ConfigMap。

在以下示例中,ConfigMap 在 configmaps 参数中传递。此 ConfigMap 的所有键均可用作环境变量:

import datetime

from airflow import models
from airflow.providers.cncf.kubernetes.operators.pod import KubernetesPodOperator

with models.DAG(
    dag_id="composer_kubernetes_pod_configmap",
    schedule=None,
    start_date=datetime.datetime(2024, 1, 1),
) as dag:

  KubernetesPodOperator(
    task_id='kpo_configmap_env_vars',
    image='busybox:1.28',
    cmds=['sh'],
    arguments=[
        '-c',
        'echo "Value: $example_key"',
    ],
    configmaps=["example-configmap"],
    config_file="/home/airflow/composer_kube_config",
  )

以下示例展示了如何将 ConfigMap 装载为卷:

import datetime

from airflow import models
from kubernetes.client import models as k8s
from airflow.providers.cncf.kubernetes.operators.pod import KubernetesPodOperator

volume_mount = k8s.V1VolumeMount(name='confmap-example',
  mount_path='/config',
  sub_path=None,
  read_only=False)

volume = k8s.V1Volume(name='confmap-example',
  config_map=k8s.V1ConfigMapVolumeSource(name='example-configmap'))

with models.DAG(
    dag_id="composer_kubernetes_pod_configmap",
    schedule=None,
    start_date=datetime.datetime(2024, 1, 1),
) as dag:

  KubernetesPodOperator(
    task_id='kpo_configmap_volume_mount',
    image='busybox:1.28',
    cmds=['sh'],
    arguments=[
        '-c',
        'ls /config'
    ],
    volumes=[volume],
    volume_mounts=[volume_mount],
    configmaps=["example-configmap"],
    config_file="/home/airflow/composer_kube_config",
  )

有关 CNCF Kubernetes 提供商的信息

KubernetesPodOperator 在 apache-airflow-providers-cncf-kubernetes 提供程序中实现。

如需查看 CNCF Kubernetes 提供程序的详细版本说明,请参阅 CNCF Kubernetes 提供程序网站。

资源要求

Managed Airflow(第 3 代)支持以下资源要求值。 如需查看使用资源要求的示例,请参阅其他配置。

资源 最小值 上限 步骤
CPU 0.25 32 步长值:0.25、0.5、1、2、4、6、8、10、…、32。请求的值会向上舍入到最接近的支持步长值(例如,5 向上舍入到 6)。
内存 2G (GB) 128G (GB) 步长值:2、3、4、5、…、128。请求的值会向上舍入到最接近的受支持步长值(例如,3.5G 向上舍入到 4G)。
存储 - 100G (GB) 任意值。如果请求的容量超过 100 GB,则仅提供 100 GB。

如需详细了解 Kubernetes 中的资源单位,请参阅 Kubernetes 中的资源单位。

问题排查

本部分提供了排查常见 KubernetesPodOperator 问题的建议:

查看日志

排查问题时,您可以按以下顺序查看日志:

  1. Airflow 任务日志:

    1. 在 Google Cloud 控制台中,前往环境页面。

      转到“环境”

    2. 在环境列表中,点击您的环境名称。环境详情页面会打开。

    3. 前往 DAG 标签页。

    4. 点击 DAG 的名称,然后点击 DAG 运行以查看详细信息和日志。

  2. Airflow 调度器日志:

    1. 前往环境详情页面。

    2. 前往日志标签页。

    3. 检查 Airflow 调度器日志。

  3. 用户工作负载日志:

    1. 前往环境详情页面。

    2. 转到监控标签页。

    3. 选择用户工作负载。

    4. 检查已执行的工作负载列表。您可以查看每个工作负载的日志和资源利用率信息。

非零返回代码

使用 KubernetesPodOperator(和 GKEStartPodOperator)时,容器入口点的返回代码决定了任务是否被视为成功。返回非零代码表示失败。

一种常见模式是将 shell 脚本作为容器入口点执行,以将容器内的多个操作组合在一起。

如果您正在编写此类脚本,我们建议您在脚本顶部添加 set -e 命令,以便脚本中失败的命令终止脚本并将失败传播到 Airflow 任务实例。

Pod 超时

KubernetesPodOperator 的默认超时时间为 120 秒,这可能会导致在下载较大映像之前发生超时。您可以在创建 KubernetesPodOperator 时通过更改 startup_timeout_seconds 参数来增加超时。

当 Pod 超时时,Airflow 界面中会提供特定于任务的日志。例如:

Executing <Task(KubernetesPodOperator): ex-all-configs> on 2018-07-23 19:06:58.133811
Running: ['bash', '-c', u'airflow run kubernetes-pod-example ex-all-configs 2018-07-23T19:06:58.133811 --job_id 726 --raw -sd DAGS_FOLDER/kubernetes_pod_operator_sample.py']
Event: pod-name-9a8e9d06 had an event of type Pending
...
...
Event: pod-name-9a8e9d06 had an event of type Pending
Traceback (most recent call last):
  File "/usr/local/bin/airflow", line 27, in <module>
    args.func(args)
  File "/usr/local/lib/python2.7/site-packages/airflow/bin/cli.py", line 392, in run
    pool=args.pool,
  File "/usr/local/lib/python2.7/site-packages/airflow/utils/db.py", line 50, in wrapper
    result = func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/airflow/models.py", line 1492, in _run_raw_task
    result = task_copy.execute(context=context)
  File "/usr/local/lib/python2.7/site-packages/airflow/contrib/operators/kubernetes_pod_operator.py", line 123, in execute
    raise AirflowException('Pod Launching failed: {error}'.format(error=ex))
airflow.exceptions.AirflowException: Pod Launching failed: Pod took too long to start

如果 Managed Airflow 服务账号缺少执行手头任务所需的 IAM 权限,也可能会发生 Pod 超时。如需验证这一点,请使用 GKE 信息中心查看 Pod 级错误,以查看特定工作负载的日志,或使用 Cloud Logging。

执行大量任务时,KubernetesPodOperator 任务失败

当您的环境同时执行大量 KubernetesPodOperator 或 KubernetesExecutor 任务时,Managed Airflow(第 3 代)在部分现有任务完成之前不会接受新任务。

如需详细了解如何排查此问题,请参阅排查 KubernetesExecutor 任务问题。

后续步骤