在 Apache Airflow 作業中新增 Kubernetes 秘密
注意
Apache Airflow 作業是由 Apache Airflow 提供電源。
Kubernetes 秘密是一個物件,其設計目的是安全地儲存敏感性資訊,例如密碼、令牌或密鑰。 藉由利用秘密,您可以避免將機密數據直接內嵌在應用程式程序代碼中。
本指南會逐步引導您在裝載於 Microsoft Fabric 的 Apache Airflow 環境中新增 Kubernetes 秘密,以從私人登錄提取容器映像。 在此範例中,我們將使用 Azure Container Registry 來建立自定義映射,然後會在 Airflow DAG 內提取該映像。
必要條件
- Azure Container Registry:使用您想要在有向非循環圖 (DAG) 中使用的自訂 映像,設定 Azure Container Registry。 如需推送和提取容器映像的詳細資訊,請參閱 推送和提取容器映像 - Azure Container Registry。
新增 Kubernetes 祕密。
- 按下 [
Configure Airflow
] 以瀏覽至 [Environment configuration
] 頁面。 - 在 [
Kubernetes secrets
] 區段下,按下 [New
] 按鈕。 - 填寫對話框中出現的欄位:
- 在填寫所有欄位後,按下 [
Create
] 按鈕以完成建立 Kubernetes 祕密。
使用預存 Kubernetes 秘密從 ACR 提取自定義映像的範例 DAG。
from datetime import datetime, timedelta
from airflow import DAG
from airflow.providers.cncf.kubernetes.operators.pod import KubernetesPodOperator
from kubernetes.client import models as kubernetes
default_args = {
"retries": 1,
"retry_delay": timedelta(minutes=3),
}
dag = DAG(
dag_id="pull_custom_image_from_acr",
start_date=datetime(2022, 5, 14),
schedule_interval="@daily",
catchup=False,
default_args=default_args,
)
acr_kubernetes = KubernetesPodOperator(
task_id="task-one",
namespace="adf",
image="<docker_image_you_wish_to_launch>",
image_pull_secrets=[kubernetes.V1LocalObjectReference("<stored_kubernetes_password")],
cmds=["echo", "10"],
labels={"foo": "bar"},
name="private-image-pod",
in_cluster=True,
dag=dag,
)