使用 KubernetesPodOperator

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

本頁面說明如何使用 KubernetesPodOperator,將 Managed Service for Apache Airflow 的 Kubernetes Pod 部署至 Google Kubernetes Engine 叢集,該叢集是 Managed Service for Apache Airflow 環境的一部分。

KubernetesPodOperator 會在環境叢集中啟動 Kubernetes Pod。相較之下,Google Kubernetes Engine 運算子會在指定叢集中執行 Kubernetes Pod,該叢集可以是與環境無關的獨立叢集。您也可以使用 Google Kubernetes Engine 運算子建立及刪除叢集。

如果您有下列需求,KubernetesPodOperator 是不錯的選擇:

  • 無法透過公開 PyPI 存放區取得的自訂 Python 依附元件。
  • 標準 Managed Airflow worker 映像檔中未提供的二進位依附元件。

事前準備

  • 建議使用最新版本 Managed Airflow。這個版本至少必須符合淘汰和支援政策的支援規定。
  • 確認環境有足夠的資源。在資源不足的環境中啟動 Pod,可能會導致 Airflow worker 和 Airflow 排程器發生錯誤。

設定 Managed Service for Apache Airflow 環境資源

建立 Managed Airflow 環境時,您需要指定效能參數,包括環境叢集的效能參數。將 Kubernetes Pod 啟動至環境叢集,可能會導致叢集資源 (例如 CPU 或記憶體) 競爭。由於 Airflow 排程器和 worker 位於同一個 GKE 叢集,如果競爭導致資源匱乏,排程器和 worker 就無法正常運作。

為避免資源匱乏,請採取下列一或多項行動:

建立節點集區

如要避免 Managed Airflow 環境中的資源耗盡,建議建立新的節點集區,並設定 Kubernetes Pod,使其僅使用該集區中的資源執行。

控制台

  1. 前往 Google Cloud 控制台的「Environments」(環境) 頁面。

    前往「Environments」(環境)

  2. 按一下環境名稱。

  3. 在「Environment details」(環境詳細資料) 頁面中,前往「Environment configuration」(環境設定) 分頁。

  4. 在「Resources」(資源) >「GKE cluster」(GKE 叢集) 區段中,點選「view cluster details」(查看叢集詳細資料) 連結。

  5. 按照「新增節點集區」一文中的說明建立節點集區。

gcloud

  1. 判斷環境叢集的名稱:

    gcloud composer environments describe ENVIRONMENT_NAME \
      --location LOCATION \
      --format="value(config.gkeCluster)"
    

    更改項目:

    • ENVIRONMENT_NAME:環境名稱。
    • 將 LOCATION 替換為環境所在的區域。
  2. 輸出內容會包含環境叢集的名稱。例如,這可以是 europe-west3-example-enviro-af810e25-gke。

  3. 按照「新增節點集區」一文中的說明建立節點集區。

增加環境中的節點數量

增加 Managed Airflow 環境中的節點數量,可提高工作負載可用的運算能力。這項增加不會為需要比指定機型提供更多 CPU 或 RAM 的工作提供額外資源。

如要增加節點數量,請更新環境。

指定合適的機型

建立 Managed Airflow 環境時,您可以指定機型。為確保資源可用,請指定 Managed Airflow 環境中運算類型的機器類型。

希望將設定需求減至最低

如要建立 KubernetesPodOperator,只需指定 Pod 的 name、要使用的 image 和 task_id 參數。/home/airflow/composer_kube_config 包含向 GKE 進行驗證所需的憑證。

Airflow 2

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, default namespace is
    # `default`. In Composer 1 there is the potential for
    # the resource starvation of Airflow workers and scheduler
    # within the Cloud Composer environment,
    # the recommended solution is to increase the amount of nodes in order
    # to satisfy the computing requirements. Alternatively, launching pods
    # into a custom namespace will stop fighting over resources,
    # and using Composer 2 will mean the environment will autoscale.
    namespace="default",
    # 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",
)

Airflow 1

kubernetes_min_pod = kubernetes_pod_operator.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, default namespace is
    # `default`. There is the potential for the resource starvation of
    # Airflow workers and scheduler within the Cloud Composer environment,
    # the recommended solution is to increase the amount of nodes in order
    # to satisfy the computing requirements. Alternatively, launching pods
    # into a custom namespace will stop fighting over resources.
    namespace="default",
    # 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",
)

Pod 親和性設定

在 KubernetesPodOperator 中設定 affinity 參數,即可控管要將 Pod 排定在哪些節點上,例如僅限特定節點集區中的節點。在這個範例中,運算子只會在名為 pool-0 和 pool-1 的節點集區上執行。Managed Airflow (舊版第 1 代) 環境節點位於 default-pool,因此 Pod 不會在環境中的節點上執行。

Airflow 2

# 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
kubernetes_affinity_ex = KubernetesPodOperator(
    task_id="ex-pod-affinity",
    name="ex-pod-affinity",
    namespace="default",
    image="perl:5.34.0",
    cmds=["perl"],
    arguments=["-Mbignum=bpi", "-wle", "print bpi(2000)"],
    # affinity allows you to constrain which nodes your pod is eligible to
    # be scheduled on, based on labels on the node. In this case, if the
    # label 'cloud.google.com/gke-nodepool' with value
    # 'nodepool-label-value' or 'nodepool-label-value2' is not found on any
    # nodes, it will fail to schedule.
    affinity={
        "nodeAffinity": {
            # requiredDuringSchedulingIgnoredDuringExecution means in order
            # for a pod to be scheduled on a node, the node must have the
            # specified labels. However, if labels on a node change at
            # runtime such that the affinity rules on a pod are no longer
            # met, the pod will still continue to run on the node.
            "requiredDuringSchedulingIgnoredDuringExecution": {
                "nodeSelectorTerms": [
                    {
                        "matchExpressions": [
                            {
                                # When nodepools are created in Google Kubernetes
                                # Engine, the nodes inside of that nodepool are
                                # automatically assigned the label
                                # 'cloud.google.com/gke-nodepool' with the value of
                                # the nodepool's name.
                                "key": "cloud.google.com/gke-nodepool",
                                "operator": "In",
                                # The label key's value that pods can be scheduled
                                # on.
                                "values": [
                                    "pool-0",
                                    "pool-1",
                                ],
                            }
                        ]
                    }
                ]
            }
        }
    },
)

Airflow 1

kubernetes_affinity_ex = kubernetes_pod_operator.KubernetesPodOperator(
    task_id="ex-pod-affinity",
    name="ex-pod-affinity",
    namespace="default",
    image="perl:5.34.0",
    cmds=["perl"],
    arguments=["-Mbignum=bpi", "-wle", "print bpi(2000)"],
    # affinity allows you to constrain which nodes your pod is eligible to
    # be scheduled on, based on labels on the node. In this case, if the
    # label 'cloud.google.com/gke-nodepool' with value
    # 'nodepool-label-value' or 'nodepool-label-value2' is not found on any
    # nodes, it will fail to schedule.
    affinity={
        "nodeAffinity": {
            # requiredDuringSchedulingIgnoredDuringExecution means in order
            # for a pod to be scheduled on a node, the node must have the
            # specified labels. However, if labels on a node change at
            # runtime such that the affinity rules on a pod are no longer
            # met, the pod will still continue to run on the node.
            "requiredDuringSchedulingIgnoredDuringExecution": {
                "nodeSelectorTerms": [
                    {
                        "matchExpressions": [
                            {
                                # When nodepools are created in Google Kubernetes
                                # Engine, the nodes inside of that nodepool are
                                # automatically assigned the label
                                # 'cloud.google.com/gke-nodepool' with the value of
                                # the nodepool's name.
                                "key": "cloud.google.com/gke-nodepool",
                                "operator": "In",
                                # The label key's value that pods can be scheduled
                                # on.
                                "values": [
                                    "pool-0",
                                    "pool-1",
                                ],
                            }
                        ]
                    }
                ]
            }
        }
    },
)

按照範例設定,工作會失敗。查看記錄時,您會發現工作失敗,因為節點集區 pool-0 和 pool-1 不存在。

為確保 values 中的節點集區存在,請進行下列任一設定變更:

  • 如果您先前已建立節點集區,請將 pool-0 和 pool-1 替換為節點集區的名稱,然後再次上傳 DAG。

  • 建立節點集區,並命名為 pool-0 或 pool-1。您可以同時建立這兩者,但工作只需要其中一個就能成功。

  • 將 pool-0 和 pool-1 替換為 default-pool,這是 Airflow 使用的預設集區。接著,再次上傳 DAG。

變更後,請稍候幾分鐘,讓環境更新。接著再次執行 ex-pod-affinity 工作,確認 ex-pod-affinity 工作成功。

更多設定

這個範例顯示您可以在 KubernetesPodOperator 中設定的其他參數。

如需更多資訊,請參考下列資源:

Airflow 2

kubernetes_full_pod = KubernetesPodOperator(
    task_id="ex-all-configs",
    name="pi",
    namespace="default",
    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 120.
    startup_timeout_seconds=120,
    # The environment variables to be initialized in the container
    # env_vars are 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(
        limits={"memory": "250M", "cpu": "100m"},
    ),
    # Specifies path to kubernetes config. If no config is specified will
    # default to '~/.kube/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=[],
    # 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={},
)

Airflow 1

kubernetes_full_pod = kubernetes_pod_operator.KubernetesPodOperator(
    task_id="ex-all-configs",
    name="pi",
    namespace="default",
    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 120.
    startup_timeout_seconds=120,
    # The environment variables to be initialized in the container
    # env_vars are 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 1.10.4, resource specifications were
    # passed as a Pod Resources Class object,
    # If using this example on a version of Airflow prior to 1.10.4,
    # import the "pod" package from airflow.contrib.kubernetes and use
    # resources = pod.Resources() instead passing a dict
    # For more info see:
    # https://github.com/apache/airflow/pull/4551
    resources={"limit_memory": "250M", "limit_cpu": "100m"},
    # Specifies path to kubernetes config. If no config is specified will
    # default to '~/.kube/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=[],
    # 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/
    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 UI 或使用 Google Cloud CLI 建立這個變數:

Airflow UI

  1. 前往 Airflow UI。

  2. 在工具列中,依序選取「Admin」>「Variables」。

  3. 在「List Variable」(清單變數) 頁面,點選「Add a new record」(新增記錄)。

  4. 在「Add Variable」(新增變數) 頁面中,輸入下列資訊:

    • 金鑰:my_value
    • Val:example_value
  5. 按一下「儲存」。

如果環境使用 Airflow 1,請改為執行下列指令:

  1. 前往 Airflow UI。

  2. 在工具列中,依序選取「Admin」>「Variables」。

  3. 在「變數」頁面,點選「建立」分頁標籤。

  4. 在「Variable」(變數) 頁面中,輸入下列資訊:

    • 金鑰:my_value
    • Val:example_value
  5. 按一下「儲存」。

gcloud

輸入下列指令:

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

如果環境使用 Airflow 1,請改為執行下列指令:

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

更改項目:

  • ENVIRONMENT:環境名稱。
  • 將 LOCATION 替換為環境所在的區域。

以下範例說明如何搭配使用 Jinja 範本和 KubernetesPodOperator:

Airflow 2

kubernetes_template_ex = KubernetesPodOperator(
    task_id="ex-kube-templates",
    name="ex-kube-templates",
    namespace="default",
    image="bash",
    # All parameters below are able to be templated with jinja -- cmds,
    # arguments, env_vars, and config_file. For more information visit:
    # https://airflow.apache.org/docs/apache-airflow/stable/macros-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.
    env_vars={"MY_VALUE": "{{ var.value.my_value }}"},
    # Sets the config file to a kubernetes config file specified in
    # airflow.cfg. If the configuration file does not exist or does
    # not provide validcredentials the pod will fail to launch. If not
    # specified, config_file defaults to ~/.kube/config
    config_file="{{ conf.get('core', 'kube_config') }}",
)

Airflow 1

kubernetes_template_ex = kubernetes_pod_operator.KubernetesPodOperator(
    task_id="ex-kube-templates",
    name="ex-kube-templates",
    namespace="default",
    image="bash",
    # All parameters below are able to be templated with jinja -- cmds,
    # arguments, env_vars, and config_file. For more information visit:
    # https://airflow.apache.org/docs/apache-airflow/stable/macros-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.
    env_vars={"MY_VALUE": "{{ var.value.my_value }}"},
    # Sets the config file to a kubernetes config file specified in
    # airflow.cfg. If the configuration file does not exist or does
    # not provide validcredentials the pod will fail to launch. If not
    # specified, config_file defaults to ~/.kube/config
    config_file="{{ conf.get('core', 'kube_config') }}",
)

使用 Kubernetes Secret 和 ConfigMap

Kubernetes Secret 是包含機密資料的物件。Kubernetes ConfigMap 是包含非機密資料的物件,以鍵/值組合的形式呈現。

在 Managed Airflow (第 2 代) 中,您可以使用 Google Cloud CLI、API 或 Terraform 建立 Secret 和 ConfigMap,然後透過 KubernetesPodOperator 存取。

關於 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

在 Managed Airflow (第 2 代) 中,您可以使用 Google Cloud CLI 和 kubectl 建立 Secret:

  1. 取得環境叢集的相關資訊:

    1. 執行下列指令:

      gcloud composer environments describe ENVIRONMENT \
          --location LOCATION \
          --format="value(config.gkeCluster)"
      

      更改項目:

      • 將 ENVIRONMENT 替換為環境名稱。
      • 將 LOCATION 替換為 Managed Airflow 環境所在的區域。

      這個指令的輸出內容會採用下列格式:projects/<your-project-id>/zones/<zone-of-composer-env>/clusters/<your-cluster-id>。

    2. 如要取得 GKE 叢集 ID,請複製 /clusters/ 之後的輸出內容 (結尾為 -gke)。

    3. 如要取得可用區,請複製 /zones/ 後方的輸出內容。

  2. 使用下列指令連線至 GKE 叢集:

    gcloud container clusters get-credentials CLUSTER_ID \
      --project PROJECT \
      --zone ZONE
    

    更改項目:

    • CLUSTER_ID:環境的叢集 ID。
    • PROJECT_ID:專案 ID。
    • ZONE 替換為環境叢集所在的可用區。
  3. 建立 Kubernetes Secret:

    下列指令示範建立 Kubernetes Secret 的兩種不同方法。--from-literal 方法會使用鍵/值組合。--from-file 方法會使用檔案內容。

    • 如要透過提供鍵/值組合來建立 Kubernetes Secret,請執行下列指令。這個範例會建立名為 airflow-secrets 的 Secret,其中包含 sql_alchemy_conn 欄位,值為 test_value。

      kubectl create secret generic airflow-secrets \
        --from-literal sql_alchemy_conn=test_value
      
    • 如要透過提供檔案內容來建立 Kubernetes Secret,請執行下列指令。這個範例會建立名為 service-account 的 Secret,其中包含 service-account.json 欄位,其值取自本機 ./key.json 檔案的內容。

      kubectl create secret generic service-account \
        --from-file service-account.json=./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 物件如下所示:

Airflow 2

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",
)

Airflow 1

secret_env = secret.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.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。

運算子設定如下所示:

Airflow 2

kubernetes_secret_vars_ex = KubernetesPodOperator(
    task_id="ex-kube-secrets",
    name="ex-kube-secrets",
    namespace="default",
    image="ubuntu",
    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],
    # env_vars allows you to specify environment variables for your
    # container to use. env_vars is templated.
    env_vars={
        "EXAMPLE_VAR": "/example/value",
        "GOOGLE_APPLICATION_CREDENTIALS": "/var/secrets/google/service-account.json ",
    },
)

Airflow 1

kubernetes_secret_vars_ex = kubernetes_pod_operator.KubernetesPodOperator(
    task_id="ex-kube-secrets",
    name="ex-kube-secrets",
    namespace="default",
    image="ubuntu",
    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],
    # env_vars allows you to specify environment variables for your
    # container to use. env_vars is templated.
    env_vars={
        "EXAMPLE_VAR": "/example/value",
        "GOOGLE_APPLICATION_CREDENTIALS": "/var/secrets/google/service-account.json ",
    },
)

CNCF Kubernetes 供應商相關資訊

KubernetesPodOperator 是在 apache-airflow-providers-cncf-kubernetes 供應器中實作。

如需 CNCF Kubernetes 供應商的詳細版本資訊,請參閱 CNCF Kubernetes 供應商網站。

6.0.0 版

在 CNCF Kubernetes Provider 套件 6.0.0 版中,KubernetesPodOperator 預設會使用 kubernetes_default 連線。

如果您在 5.0.0 版中指定自訂連線,運算子仍會使用該自訂連線。如要切換回使用 kubernetes_default 連線,建議您視情況調整 DAG。

5.0.0 版

與 4.4.0 版相比,這個版本有一些回溯不相容的變更。其中最重要的是與 kubernetes_default 連線相關的變更,5.0.0 版不會使用該連線。

  • 必須修改 kubernetes_default 連線。Kubernetes 設定路徑必須設為 /home/airflow/composer_kube_config (如下圖所示)。或者,您必須將 config_file 新增至 KubernetesPodOperator 設定 (如以下程式碼範例所示)。
Airflow UI 中的 Kube 設定路徑欄位
圖 1. Airflow UI,修改 kubernetes_default 連線 (按一下即可放大)
  • 請按照下列方式,修改使用 KubernetesPodOperator 的工作程式碼:
KubernetesPodOperator(
  # config_file parameter - can be skipped if connection contains this setting
  config_file="/home/airflow/composer_kube_config",
  # definition of connection to be used by the operator
  kubernetes_conn_id='kubernetes_default',
  ...
)

如要進一步瞭解 5.0.0 版,請參閱 CNCF Kubernetes 供應商版本資訊。

疑難排解

本節提供 KubernetesPodOperator 常見問題的疑難排解建議:

查看記錄

排解問題時,您可以按照下列順序查看記錄:

  1. Airflow 工作記錄:

    1. 前往 Google Cloud 控制台的「Environments」(環境) 頁面。

      前往「Environments」(環境)

    2. 在環境清單中,按一下環境名稱。「Environment details」(環境詳細資料) 頁面隨即開啟。

    3. 前往「DAGs」分頁。

    4. 按一下 DAG 名稱,然後點選 DAG 執行作業,即可查看詳細資料和記錄。

  2. Airflow 排程器記錄:

    1. 前往「Environment details」(環境詳細資料) 頁面。

    2. 前往「記錄」分頁。

    3. 檢查 Airflow 排程器記錄。

  3. Google Cloud 控制台 GKE 工作負載下方的 Pod 記錄。這些記錄包括 Pod 定義 YAML 檔案、Pod 事件和 Pod 詳細資料。

非零傳回代碼

使用 KubernetesPodOperator (和 GKEStartPodOperator) 時,容器進入點的傳回代碼會決定工作是否成功。非零傳回代碼表示失敗。

常見的模式是執行殼層指令碼做為容器進入點,將容器內的多項作業分組。

如果您正在編寫這類指令碼,建議您在指令碼頂端加入 set -e 指令,這樣一來,指令碼中失敗的指令就會終止指令碼,並將失敗傳播至 Airflow 工作執行個體。

Pod 逾時

KubernetesPodOperator 的預設逾時時間為 120 秒,因此在下載較大的映像檔之前,可能會發生逾時。建立 KubernetesPodOperator 時,您可以變更 startup_timeout_seconds 參數來增加逾時時間。

Pod 逾時後,您可以在 Airflow UI 中查看特定工作的記錄。例如:

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。

無法建立新連線

GKE 叢集預設會啟用自動升級功能。如果節點集區位於正在升級的叢集中,您可能會看到下列錯誤:

<Task(KubernetesPodOperator): gke-upgrade> Failed to establish a new
connection: [Errno 111] Connection refused

如要確認叢集是否正在升級,請前往 Google Cloud 控制台的「Kubernetes clusters」(Kubernetes 叢集) 頁面,查看環境叢集名稱旁是否有載入圖示。

後續步驟