Share via


快速入門:使用 Azure 事件方格訂閱 Azure Kubernetes Service (AKS) 事件

Azure 事件方格是完全受控的事件路由服務,可使用發佈-訂閱模型提供統一的事件耗用量。

在本快速入門指南中,您將建立 AKS 叢集並訂閱 AKS 事件。

必要條件

注意

如果 EventGrid 通知有特別的問題,如服務 中斷所見,請注意,AKS 作業不會受到影響,且與事件方格中斷無關。

建立 AKS 叢集

使用 az aks create 命令來建立 AKS 叢集。 下列範例使用 MyResourceGroup 資源群組中的一個節點,建立資源群組 MyResourceGroup 和名稱為 MyAKS 的叢集:

az group create --name MyResourceGroup --location eastus
az aks create --resource-group yResourceGroup --name MyAKS --location eastus  --node-count 1 --generate-ssh-keys

訂閱 SMS 事件

使用 az eventhubs namespace createaz eventhubs eventhub create建立命名空間和事件中樞。 下列範例會在 MyResourceGroup 資源群組中建立命名空間 MyNamespace,以及命名空間 MyNamespace 中的事件中樞 MyEventGridHub

az eventhubs namespace create --location eastus --name MyNamespace --resource-group MyResourceGroup
az eventhubs eventhub create --name MyEventGridHub --namespace-name MyNamespace --resource-group MyResourceGroup

注意

命名空間名稱必須為唯一名稱。

使用 az eventgrid event-subscription create 訂閱 AKS 事件:

SOURCE_RESOURCE_ID=$(az aks show --resource-group MyResourceGroup --name MyAKS --query id --output tsv)
ENDPOINT=$(az eventhubs eventhub show --resource-group MyResourceGroup --name MyEventGridHub --namespace-name MyNamespace --query id --output tsv)
az eventgrid event-subscription create --name MyEventGridSubscription \
--source-resource-id $SOURCE_RESOURCE_ID \
--endpoint-type eventhub \
--endpoint $ENDPOINT

使用 az eventgrid event-subscription list 驗證 AKS 事件的訂用帳戶:

az eventgrid event-subscription list --source-resource-id $SOURCE_RESOURCE_ID

下列範例輸出顯示您已訂閱 MyAKS 叢集的事件,且這些事件會傳送至 MyEventGridHub 事件中樞:

[
  {
    "deadLetterDestination": null,
    "deadLetterWithResourceIdentity": null,
    "deliveryWithResourceIdentity": null,
    "destination": {
      "deliveryAttributeMappings": null,
      "endpointType": "EventHub",
      "resourceId": "/subscriptions/SUBSCRIPTION_ID/resourceGroups/MyResourceGroup/providers/Microsoft.EventHub/namespaces/MyNamespace/eventhubs/MyEventGridHub"
    },
    "eventDeliverySchema": "EventGridSchema",
    "expirationTimeUtc": null,
    "filter": {
      "advancedFilters": null,
      "enableAdvancedFilteringOnArrays": null,
      "includedEventTypes": [
        "Microsoft.ContainerService.NewKubernetesVersionAvailable","Microsoft.ContainerService.ClusterSupportEnded","Microsoft.ContainerService.ClusterSupportEnding","Microsoft.ContainerService.NodePoolRollingFailed","Microsoft.ContainerService.NodePoolRollingStarted","Microsoft.ContainerService.NodePoolRollingSucceeded"
      ],
      "isSubjectCaseSensitive": null,
      "subjectBeginsWith": "",
      "subjectEndsWith": ""
    },
    "id": "/subscriptions/SUBSCRIPTION_ID/resourceGroups/MyResourceGroup/providers/Microsoft.ContainerService/managedClusters/MyAKS/providers/Microsoft.EventGrid/eventSubscriptions/MyEventGridSubscription",
    "labels": null,
    "name": "MyEventGridSubscription",
    "provisioningState": "Succeeded",
    "resourceGroup": "MyResourceGroup",
    "retryPolicy": {
      "eventTimeToLiveInMinutes": 1440,
      "maxDeliveryAttempts": 30
    },
    "systemData": null,
    "topic": "/subscriptions/SUBSCRIPTION_ID/resourceGroups/MyResourceGroup/providers/microsoft.containerservice/managedclusters/MyAKS",
    "type": "Microsoft.EventGrid/eventSubscriptions"
  }
]

當 AKS 事件發生時,這些事件便會顯示於事件中樞。 例如,當叢集的 Kubernetes 可用版本清單變更時,您將看到 Microsoft.ContainerService.NewKubernetesVersionAvailable 事件。 現在也有新的事件可供升級和叢集內部支援使用。 如需 AKS 發出事件的詳細資訊,請參閱 Azure Kubernetes Service (AKS) 作為事件方格來源

刪除叢集和訂用帳戶

使用 az group delete 命令移除資源群組、AKS 叢集、命名空間、事件中樞及所有相關資源。

az group delete --name MyResourceGroup --yes --no-wait

注意

當您刪除叢集時,系統不會移除 AKS 叢集所使用的 Microsoft Entra 服務主體。 如需有關如何移除服務主體的步驟,請參閱 AKS 服務主體的考量和刪除

如果您使用受控識別,則身分識別會由平台負責管理,您不需要刪除。

下一步

在本快速入門指南中,您已部署 Kubernetes 叢集,並訂閱 Azure 事件中樞中的 AKS 事件。

若要深入了解 AKS,並逐步完成部署範例的完整程式碼,請繼續 Kubernetes 叢集教學課程。