title | description | services | author | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|
Subscribe to Azure Kubernetes Service events with Azure Event Grid (Preview) |
Use Azure Event Grid to subscribe to Azure Kubernetes Service events |
container-service |
zr-msft |
article |
07/12/2021 |
zarhoads |
Azure Event Grid is a fully managed event routing service that provides uniform event consumption using a publish-subscribe model.
In this quickstart, you'll create an AKS cluster and subscribe to AKS events.
[!INCLUDE preview features callout]
- An Azure subscription. If you don't have an Azure subscription, you can create a free account.
- Azure CLI installed.
To use the feature, you must also enable the EventgridPreview
feature flag on your subscription.
Register the EventgridPreview
feature flag by using the az feature register command, as shown in the following example:
az feature register --namespace "Microsoft.ContainerService" --name "EventgridPreview"
It takes a few minutes for the status to show Registered. Verify the registration status by using the az feature list command:
az feature list -o table --query "[?contains(name, 'Microsoft.ContainerService/EventgridPreview')].{Name:name,State:properties.state}"
When ready, refresh the registration of the Microsoft.ContainerService resource provider by using the az provider register command:
az provider register --namespace Microsoft.ContainerService
[!INCLUDE event-grid-register-provider-cli.md]
Create an AKS cluster using the az aks create command. The following example creates a resource group MyResourceGroup and a cluster named MyAKS with one node in the MyResourceGroup resource group:
az group create --name MyResourceGroup --location eastus
az aks create -g MyResourceGroup -n MyAKS --location eastus --node-count 1 --generate-ssh-keys
Create a namespace and event hub using az eventhubs namespace create and az eventhubs eventhub create. The following example creates a namespace MyNamespace and an event hub MyEventGridHub in MyNamespace, both in the MyResourceGroup resource group.
az eventhubs namespace create --location eastus --name MyNamespace -g MyResourceGroup
az eventhubs eventhub create --name MyEventGridHub --namespace-name MyNamespace -g MyResourceGroup
Note
The name of your namespace must be unique.
Subscribe to the AKS events using az eventgrid event-subscription create:
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
Verify your subscription to AKS events using az eventgrid event-subscription list
:
az eventgrid event-subscription list --source-resource-id $SOURCE_RESOURCE_ID
The following example output shows you're subscribed to events from the MyAKS cluster and those events are delivered to the MyEventGridHub event hub:
[
{
"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"
],
"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"
}
]
When AKS events occur, you'll see those events appear in your event hub. For example, when the list of available Kubernetes versions for your clusters changes, you'll see a Microsoft.ContainerService.NewKubernetesVersionAvailable
event. For more information on the events AKS emits, see Azure Kubernetes Service (AKS) as an Event Grid source.
Use the az group delete command to remove the resource group, the AKS cluster, namespace, and event hub, and all related resources.
az group delete --name MyResourceGroup --yes --no-wait
Note
When you delete the cluster, the Azure Active Directory service principal used by the AKS cluster is not removed. For steps on how to remove the service principal, see AKS service principal considerations and deletion.
If you used a managed identity, the identity is managed by the platform and does not require removal.
In this quickstart, you deployed a Kubernetes cluster and then subscribed to AKS events in Azure Event Hub.
To learn more about AKS, and walk through a complete code to deployment example, continue to the Kubernetes cluster tutorial.
[!div class="nextstepaction"] AKS tutorial