你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Azure 事件网格订阅 Azure Kubernetes 服务 (AKS) 事件

Azure 事件网格是一个完全托管的事件路由服务,可以通过发布-订阅模型提供一致的事件使用数据。

在此快速入门中,你将创建 AKS 群集并订阅 AKS 事件。

先决条件

注意

如果出现 EventGrid 通知相关问题(可在服务中断处查看),请注意,AKS 操作不会受到影响,它们独立于事件网格中断。

创建 AKS 群集

使用 az aks create 命令创建 AKS 群集。 以下示例会在使用 MyResourceGroup 资源组中的一个节点创建 MyResourceGroup 资源组和名为 MyAKS 的群集 :

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

订阅 AKS 事件

使用 az eventhubs namespace createaz eventhubs eventhub create 创建命名空间和事件中心。 以下示例会在 MyNamespace 和 MyResourceGroup 资源组中创建 MyNamespace 命名空间和 MyEventGridHub 事件中心 。

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

注意

命名空间的名称必须具有唯一性。

使用 az eventgrid event-subscription create 订阅 AKS 事件:

SOURCE_RESOURCE_ID=$(az aks show -g MyResourceGroup -n MyAKS --query id --output tsv)
ENDPOINT=$(az eventhubs eventhub show -g MyResourceGroup -n 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 服务 (AKS) 用作事件网格源

删除群集和订阅

可以使用 az group delete 命令删除资源组、AKS 群集、命名空间、事件中心及所有相关资源。

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

注意

删除群集时,AKS 群集使用的 Microsoft Entra 服务主体不会被删除。 有关如何删除服务主体的步骤,请参阅 AKS 服务主体的注意事项和删除

如果你使用了托管标识,则该标识由平台托管,不需要删除。

后续步骤

在此快速入门中,你部署了 Kubernetes 群集并在 Azure 事件中心订阅了 AKS 事件。

若要详细了解 AKS 并演练部署示例的完整代码,请继续阅读“Kubernetes 群集”教程。