Skip to content

Commit a338018

Browse files
authored
Merge pull request #200610 from alexandair/an-az-aks-eventgrid
Add PowerShell code equivalent to the CLI code
2 parents 05b0b2c + 4f03117 commit a338018

File tree

1 file changed

+122
-5
lines changed

1 file changed

+122
-5
lines changed

articles/aks/quickstart-event-grid.md

Lines changed: 122 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ In this quickstart, you'll create an AKS cluster and subscribe to AKS events.
1919
## Prerequisites
2020

2121
* An Azure subscription. If you don't have an Azure subscription, you can create a [free account](https://azure.microsoft.com/free).
22-
* [Azure CLI installed](/cli/azure/install-azure-cli).
22+
* [Azure CLI][azure-cli-install] or [Azure PowerShell][azure-powershell-install] installed.
2323

2424
### Register the `EventgridPreview` preview feature
2525

2626
To use the feature, you must also enable the `EventgridPreview` feature flag on your subscription.
2727

28+
### [Azure CLI](#tab/azure-cli)
29+
2830
Register the `EventgridPreview` feature flag by using the [az feature register][az-feature-register] command, as shown in the following example:
2931

3032
```azurecli-interactive
@@ -45,20 +47,60 @@ az provider register --namespace Microsoft.ContainerService
4547

4648
[!INCLUDE [event-grid-register-provider-cli.md](../../includes/event-grid-register-provider-cli.md)]
4749

50+
### [Azure PowerShell](#tab/azure-powershell)
51+
52+
Register the `EventgridPreview` feature flag by using the [Register-AzProviderPreviewFeature][register-azproviderpreviewfeature] cmdlet, as shown in the following example:
53+
54+
```azurepowershell-interactive
55+
Register-AzProviderPreviewFeature -ProviderNamespace Microsoft.ContainerService -Name EventgridPreview
56+
```
57+
58+
It takes a few minutes for the status to show *Registered*. Verify the registration status by using the [Get-AzProviderPreviewFeature][get-azproviderpreviewfeature] cmdlet:
59+
60+
```azurepowershell-interactive
61+
Get-AzProviderPreviewFeature -ProviderNamespace Microsoft.ContainerService -Name EventgridPreview |
62+
Format-Table -Property Name, @{name='State'; expression={$_.Properties.State}}
63+
```
64+
65+
When ready, refresh the registration of the *Microsoft.ContainerService* resource provider by using the [Register-AzResourceProvider][register-azresourceprovider] command:
66+
67+
```azurepowershell-interactive
68+
Register-AzResourceProvider -ProviderNamespace Microsoft.ContainerService
69+
```
70+
71+
[!INCLUDE [event-grid-register-provider-powershell.md](../../includes/event-grid-register-provider-powershell.md)]
72+
73+
---
74+
4875
## Create an AKS cluster
4976

77+
### [Azure CLI](#tab/azure-cli)
78+
5079
Create an AKS cluster using the [az aks create][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:
5180

52-
```azurecli
81+
```azurecli-interactive
5382
az group create --name MyResourceGroup --location eastus
5483
az aks create -g MyResourceGroup -n MyAKS --location eastus --node-count 1 --generate-ssh-keys
5584
```
5685

86+
### [Azure PowerShell](#tab/azure-powershell)
87+
88+
Create an AKS cluster using the [New-AzAksCluster][new-azakscluster] command. The following example creates a resource group *MyResourceGroup* and a cluster named *MyAKS* with one node in the *MyResourceGroup* resource group:
89+
90+
```azurepowershell-interactive
91+
New-AzResourceGroup -Name MyResourceGroup -Location eastus
92+
New-AzAksCluster -ResourceGroupName MyResourceGroup -Name MyAKS -Location eastus -NodeCount 1 -GenerateSshKey
93+
```
94+
95+
---
96+
5797
## Subscribe to AKS events
5898

99+
### [Azure CLI](#tab/azure-cli)
100+
59101
Create a namespace and event hub using [az eventhubs namespace create][az-eventhubs-namespace-create] and [az eventhubs eventhub create][az-eventhubs-eventhub-create]. The following example creates a namespace *MyNamespace* and an event hub *MyEventGridHub* in *MyNamespace*, both in the *MyResourceGroup* resource group.
60102

61-
```azurecli
103+
```azurecli-interactive
62104
az eventhubs namespace create --location eastus --name MyNamespace -g MyResourceGroup
63105
az eventhubs eventhub create --name MyEventGridHub --namespace-name MyNamespace -g MyResourceGroup
64106
```
@@ -68,7 +110,7 @@ az eventhubs eventhub create --name MyEventGridHub --namespace-name MyNamespace
68110
69111
Subscribe to the AKS events using [az eventgrid event-subscription create][az-eventgrid-event-subscription-create]:
70112

71-
```azurecli
113+
```azurecli-interactive
72114
SOURCE_RESOURCE_ID=$(az aks show -g MyResourceGroup -n MyAKS --query id --output tsv)
73115
ENDPOINT=$(az eventhubs eventhub show -g MyResourceGroup -n MyEventGridHub --namespace-name MyNamespace --query id --output tsv)
74116
az eventgrid event-subscription create --name MyEventGridSubscription \
@@ -79,7 +121,7 @@ az eventgrid event-subscription create --name MyEventGridSubscription \
79121

80122
Verify your subscription to AKS events using `az eventgrid event-subscription list`:
81123

82-
```azurecli
124+
```azurecli-interactive
83125
az eventgrid event-subscription list --source-resource-id $SOURCE_RESOURCE_ID
84126
```
85127

@@ -124,16 +166,81 @@ The following example output shows you're subscribed to events from the *MyAKS*
124166
]
125167
```
126168

169+
### [Azure PowerShell](#tab/azure-powershell)
170+
171+
Create a namespace and event hub using [New-AzEventHubNamespace][new-azeventhubnamespace] and [New-AzEventHub][new-azeventhub]. The following example creates a namespace *MyNamespace* and an event hub *MyEventGridHub* in *MyNamespace*, both in the *MyResourceGroup* resource group.
172+
173+
```azurepowershell-interactive
174+
New-AzEventHubNamespace -Location eastus -Name MyNamespace -ResourceGroupName MyResourceGroup
175+
New-AzEventHub -Name MyEventGridHub -Namespace MyNamespace -ResourceGroupName MyResourceGroup
176+
```
177+
178+
> [!NOTE]
179+
> The *name* of your namespace must be unique.
180+
181+
Subscribe to the AKS events using [New-AzEventGridSubscription][new-azeventgridsubscription]:
182+
183+
```azurepowershell-interactive
184+
$SOURCE_RESOURCE_ID = (Get-AzAksCluster -ResourceGroupName MyResourceGroup -Name MyAKS).Id
185+
$ENDPOINT = (Get-AzEventHub -ResourceGroupName MyResourceGroup -EventHubName MyEventGridHub -Namespace MyNamespace).Id
186+
$params = @{
187+
EventSubscriptionName = 'MyEventGridSubscription'
188+
ResourceId = $SOURCE_RESOURCE_ID
189+
EndpointType = 'eventhub'
190+
Endpoint = $ENDPOINT
191+
}
192+
New-AzEventGridSubscription @params
193+
```
194+
195+
Verify your subscription to AKS events using `Get-AzEventGridSubscription`:
196+
197+
```azurepowershell-interactive
198+
Get-AzEventGridSubscription -ResourceId $SOURCE_RESOURCE_ID | Select-Object -ExpandProperty PSEventSubscriptionsList
199+
```
200+
201+
The following example output shows you're subscribed to events from the *MyAKS* cluster and those events are delivered to the *MyEventGridHub* event hub:
202+
203+
```Output
204+
EventSubscriptionName : MyEventGridSubscription
205+
Id : /subscriptions/SUBSCRIPTION_ID/resourceGroups/MyResourceGroup/providers/Microsoft.ContainerService/managedClusters/MyAKS/providers/Microsoft.EventGrid/eventSubscriptions/MyEventGridSubscription
206+
Type : Microsoft.EventGrid/eventSubscriptions
207+
Topic : /subscriptions/SUBSCRIPTION_ID/resourceGroups/myresourcegroup/providers/microsoft.containerservice/managedclusters/myaks
208+
Filter : Microsoft.Azure.Management.EventGrid.Models.EventSubscriptionFilter
209+
Destination : Microsoft.Azure.Management.EventGrid.Models.EventHubEventSubscriptionDestination
210+
ProvisioningState : Succeeded
211+
Labels :
212+
EventTtl : 1440
213+
MaxDeliveryAttempt : 30
214+
EventDeliverySchema : EventGridSchema
215+
ExpirationDate :
216+
DeadLetterEndpoint :
217+
Endpoint : /subscriptions/SUBSCRIPTION_ID/resourceGroups/MyResourceGroup/providers/Microsoft.EventHub/namespaces/MyNamespace/eventhubs/MyEventGridHub
218+
```
219+
220+
---
221+
127222
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][aks-events].
128223

129224
## Delete the cluster and subscriptions
130225

226+
### [Azure CLI](#tab/azure-cli)
227+
131228
Use the [az group delete][az-group-delete] command to remove the resource group, the AKS cluster, namespace, and event hub, and all related resources.
132229

133230
```azurecli-interactive
134231
az group delete --name MyResourceGroup --yes --no-wait
135232
```
136233

234+
### [Azure PowerShell](#tab/azure-powershell)
235+
236+
Use the [Remove-AzResourceGroup][remove-azresourcegroup] cmdlet to remove the resource group, the AKS cluster, namespace, and event hub, and all related resources.
237+
238+
```azurepowershell-interactive
239+
Remove-AzResourceGroup -Name MyResourceGroup
240+
```
241+
242+
---
243+
137244
> [!NOTE]
138245
> 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][sp-delete].
139246
>
@@ -148,14 +255,24 @@ To learn more about AKS, and walk through a complete code to deployment example,
148255
> [!div class="nextstepaction"]
149256
> [AKS tutorial][aks-tutorial]
150257
258+
[azure-cli-install]: /cli/azure/install-azure-cli
259+
[azure-powershell-install]: /powershell/azure/install-az-ps
151260
[aks-events]: ../event-grid/event-schema-aks.md
152261
[aks-tutorial]: ./tutorial-kubernetes-prepare-app.md
153262
[az-aks-create]: /cli/azure/aks#az_aks_create
263+
[new-azakscluster]: /powershell/module/az.aks/new-azakscluster
154264
[az-eventhubs-namespace-create]: /cli/azure/eventhubs/namespace#az-eventhubs-namespace-create
265+
[new-azeventhubnamespace]: /powershell/module/az.eventhub/new-azeventhubnamespace
155266
[az-eventhubs-eventhub-create]: /cli/azure/eventhubs/eventhub#az-eventhubs-eventhub-create
267+
[new-azeventhub]: /powershell/module/az.eventhub/new-azeventhub
156268
[az-eventgrid-event-subscription-create]: /cli/azure/eventgrid/event-subscription#az-eventgrid-event-subscription-create
269+
[new-azeventgridsubscription]: /powershell/module/az.eventgrid/new-azeventgridsubscription
157270
[az-feature-register]: /cli/azure/feature#az_feature_register
271+
[register-azproviderpreviewfeature]: /powershell/module/az.resources/register-azproviderpreviewfeature
158272
[az-feature-list]: /cli/azure/feature#az_feature_list
273+
[get-azproviderpreviewfeature]: /powershell/module/az.resources/get-azproviderpreviewfeature
159274
[az-provider-register]: /cli/azure/provider#az_provider_register
275+
[register-azresourceprovider]: /powershell/module/az.resources/register-azresourceprovider
160276
[az-group-delete]: /cli/azure/group#az_group_delete
161277
[sp-delete]: kubernetes-service-principal.md#other-considerations
278+
[remove-azresourcegroup]: /powershell/module/az.resources/remove-azresourcegroup

0 commit comments

Comments
 (0)