airflow.providers.microsoft.azure.operators.container_instances

模組內容

類別

AzureContainerInstancesOperator

在 Azure Container Instances 上啟動容器。

屬性

Volume

DEFAULT_ENVIRONMENT_VARIABLES

DEFAULT_SECURED_VARIABLES

DEFAULT_VOLUMES

DEFAULT_MEMORY_IN_GB

DEFAULT_CPU

airflow.providers.microsoft.azure.operators.container_instances.Volume[source]
airflow.providers.microsoft.azure.operators.container_instances.DEFAULT_ENVIRONMENT_VARIABLES: dict[str, str][source]
airflow.providers.microsoft.azure.operators.container_instances.DEFAULT_SECURED_VARIABLES: collections.abc.Sequence[str] = [][source]
airflow.providers.microsoft.azure.operators.container_instances.DEFAULT_VOLUMES: collections.abc.Sequence[Volume] = [][source]
airflow.providers.microsoft.azure.operators.container_instances.DEFAULT_MEMORY_IN_GB = 2.0[source]
airflow.providers.microsoft.azure.operators.container_instances.DEFAULT_CPU = 1.0[source]
class airflow.providers.microsoft.azure.operators.container_instances.AzureContainerInstancesOperator(*, ci_conn_id, resource_group, name, image, region, registry_conn_id=None, environment_variables=None, secured_variables=None, volumes=None, memory_in_gb=None, cpu=None, gpu=None, command=None, remove_on_error=True, fail_if_exists=True, tags=None, xcom_all=None, os_type='Linux', restart_policy='Never', ip_address=None, ports=None, subnet_ids=None, dns_config=None, diagnostics=None, priority='Regular', **kwargs)[source]

基底類別: airflow.models.BaseOperator

在 Azure Container Instances 上啟動容器。

參數
  • ci_conn_id (str) – 服務主體的連線 ID,將用於啟動容器實例

  • registry_conn_id (str | None) – 可以登入私有 Docker 倉庫的使用者連線 ID。對於 Azure,請使用 Azure 連線 ID

  • resource_group (str) – 容器實例應啟動所在的資源群組名稱

  • name (str) – 此容器實例的名稱。請注意,此名稱必須是唯一的,才能平行執行容器。

  • image (str) – 要使用的 Docker 映像

  • region (str) – 容器實例應啟動所在的區域

  • environment_variables (dict | None) – 包含環境變數的鍵值對,將傳遞到正在執行的容器

  • secured_variables (str | None) – 不應在容器外部公開的環境變數名稱 (通常為密碼)。

  • volumes (list | None) – 要掛載到容器的 Volume 元組列表。目前僅支援 Azure 檔案共用。

  • memory_in_gb (Any | None) – 要分配給此容器的記憶體量

  • cpu (Any | None) – 要分配給此容器的 CPU 數量

  • gpu (Any | None) – 容器的 GPU 資源。

  • command (list[str] | None) – 在容器內執行的命令

  • container_timeout – 容器實例執行允許的最長時間。

  • tags (dict[str, str] | None) – Azure 標籤,格式為 dict of str:str

  • xcom_all (bool | None) – 控制是否將日誌推送到 XCOM,類似於 DockerOperator 的方式。可能的值包括:‘None’、‘True’、‘False’。預設值為 ‘None’,表示不將日誌推送到 XCOM,這是歷史行為。‘True’ 表示將所有日誌推送到 XCOM,這可能會遇到 XCOM 大小限制的風險。‘False’ 表示僅將日誌的最後一行推送到 XCOM。但是,日誌會被推送到 “logs” 下的 XCOM,而不是 return_value,以避免破壞現有行為。

  • os_type (str) – 容器群組中容器所需的作業系統類型。可能的值包括:‘Windows’、‘Linux’

  • restart_policy (str) – 容器群組中所有容器的重新啟動策略。可能的值包括:‘Always’、‘OnFailure’、‘Never’

  • ip_address (azure.mgmt.containerinstance.models.IpAddress | None) – 容器群組的 IP 位址類型。

  • subnet_ids (list[azure.mgmt.containerinstance.models.ContainerGroupSubnetId] | None) – 容器群組的子網路資源 ID

  • dns_config (azure.mgmt.containerinstance.models.DnsConfiguration | None) – 容器群組的 DNS 組態。

  • diagnostics (azure.mgmt.containerinstance.models.ContainerGroupDiagnostics | None) – 容器群組診斷資訊 (Log Analytics)。

  • priority (str | None) – 容器群組優先順序,可能的值包括:‘Regular’、‘Spot’

範例:

AzureContainerInstancesOperator(
    ci_conn_id="azure_service_principal",
    registry_conn_id="azure_registry_user",
    resource_group="my-resource-group",
    name="my-container-name-{{ ds }}",
    image="myprivateregistry.azurecr.io/my_container:latest",
    region="westeurope",
    environment_variables={
        "MODEL_PATH": "my_value",
        "POSTGRES_LOGIN": "{{ macros.connection('postgres_default').login }}",
        "POSTGRES_PASSWORD": "{{ macros.connection('postgres_default').password }}",
        "JOB_GUID": "{{ ti.xcom_pull(task_ids='task1', key='guid') }}",
    },
    secured_variables=["POSTGRES_PASSWORD"],
    volumes=[
        ("azure_container_instance_conn_id", "my_storage_container", "my_fileshare", "/input-data", True),
    ],
    memory_in_gb=14.0,
    cpu=4.0,
    gpu=GpuResource(count=1, sku="K80"),
    subnet_ids=[
        {
            "id": "/subscriptions/00000000-0000-0000-0000-00000000000/resourceGroups/my_rg/providers/Microsoft.Network/virtualNetworks/my_vnet/subnets/my_subnet"
        }
    ],
    dns_config={"name_servers": ["10.0.0.10", "10.0.0.11"]},
    diagnostics={
        "log_analytics": {
            "workspaceId": "workspaceid",
            "workspaceKey": "workspaceKey",
        }
    },
    priority="Regular",
    command=["/bin/echo", "world"],
    task_id="start_container",
)
template_fields: collections.abc.Sequence[str] = ('name', 'image', 'command', 'environment_variables', 'volumes')[source]
template_fields_renderers[source]
execute(context)[source]

在建立運算子時衍生。

Context 是與渲染 Jinja 模板時使用的相同字典。

請參閱 get_template_context 以取得更多上下文。

on_kill()[source]

覆寫此方法以在任務實例被終止時清理子進程。

在運算子中使用的任何 threading、subprocess 或 multiprocessing 模組都需要清理,否則會留下孤立進程。

此條目是否有幫助?