Skip to content

Commit f0a82f4

Browse files
Merge pull request #234742 from MGoedtel/issue107935
Standardize variables and commands for WI
2 parents 5a49f6f + aa76d70 commit f0a82f4

File tree

2 files changed

+60
-95
lines changed

2 files changed

+60
-95
lines changed

articles/aks/learn/tutorial-kubernetes-workload-identity.md

Lines changed: 27 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Tutorial - Use a workload identity with an application on Azure Kubernete
33
description: In this Azure Kubernetes Service (AKS) tutorial, you deploy an Azure Kubernetes Service cluster and configure an application to use a workload identity.
44
ms.topic: tutorial
55
ms.custom: devx-track-azurecli
6-
ms.date: 01/11/2023
6+
ms.date: 04/18/2023
77
---
88

99
# Tutorial: Use a workload identity with an application on Azure Kubernetes Service (AKS)
@@ -20,23 +20,19 @@ This tutorial assumes a basic understanding of Kubernetes concepts. For more inf
2020

2121
[!INCLUDE [quickstarts-free-trial-note](../../../includes/quickstarts-free-trial-note.md)]
2222

23-
[!INCLUDE [preview features callout](../includes/preview/preview-callout.md)]
24-
2523
- This article requires version 2.40.0 or later of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed.
2624

27-
- You have installed the latest version of the `aks-preview` extension, version 0.5.102 or later.
28-
29-
- The identity you are using to create your cluster has the appropriate minimum permissions. For more information on access and identity for AKS, see [Access and identity options for Azure Kubernetes Service (AKS)][aks-identity-concepts].
25+
- The identity you're using to create your cluster has the appropriate minimum permissions. For more information on access and identity for AKS, see [Access and identity options for Azure Kubernetes Service (AKS)][aks-identity-concepts].
3026

3127
- If you have multiple Azure subscriptions, select the appropriate subscription ID in which the resources should be billed using the
3228
[az account][az-account] command.
3329

3430
## Create a resource group
3531

36-
An [Azure resource group][azure-resource-group] is a logical group in which Azure resources are deployed and managed. When you create a resource group, you are prompted to specify a location. This location is:
32+
An [Azure resource group][azure-resource-group] is a logical group in which Azure resources are deployed and managed. When you create a resource group, you're prompted to specify a location. This location is:
3733

3834
* The storage location of your resource group metadata.
39-
* Where your resources will run in Azure if you don't specify another region during resource creation.
35+
* Where your resources run in Azure if you don't specify another region during resource creation.
4036

4137
The following example creates a resource group named *myResourceGroup* in the *eastus* location.
4238

@@ -61,85 +57,42 @@ The following output example resembles successful creation of the resource group
6157
}
6258
```
6359

64-
## Install the aks-preview Azure CLI extension
65-
66-
[!INCLUDE [preview features callout](../includes/preview/preview-callout.md)]
67-
68-
To install the aks-preview extension, run the following command:
69-
70-
```azurecli-interactive
71-
az extension add --name aks-preview
72-
```
73-
74-
Run the following command to update to the latest version of the extension released:
75-
76-
```azurecli-interactive
77-
az extension update --name aks-preview
78-
```
79-
80-
## Register the 'EnableWorkloadIdentityPreview' feature flag
81-
82-
Register the `EnableWorkloadIdentityPreview` feature flag by using the [az feature register][az-feature-register] command, as shown in the following example:
83-
84-
```azurecli-interactive
85-
az feature register --namespace "Microsoft.ContainerService" --name "EnableWorkloadIdentityPreview"
86-
```
87-
88-
It takes a few minutes for the status to show *Registered*. Verify the registration status by using the [az feature list][az-feature-list] command:
60+
## Export environmental variables
8961

90-
```azurecli-interactive
91-
az feature show --namespace "Microsoft.ContainerService" --name "EnableWorkloadIdentityPreview"
92-
```
62+
To help simplify steps to configure the identities required, the steps below define
63+
environmental variables for reference on the cluster.
9364

94-
When the status shows *Registered*, refresh the registration of the *Microsoft.ContainerService* resource provider by using the [az provider register][az-provider-register] command:
65+
Run the following commands to create these variables. Replace the default values for `RESOURCE_GROUP`, `LOCATION`, `SERVICE_ACCOUNT_NAME`, `SUBSCRIPTION`, `USER_ASSIGNED_IDENTITY_NAME`, and `FEDERATED_IDENTITY_CREDENTIAL_NAME`.
9566

96-
```azurecli-interactive
97-
az provider register --namespace Microsoft.ContainerService
67+
```bash
68+
export RESOURCE_GROUP="myResourceGroup"
69+
export LOCATION="westcentralus"
70+
export SERVICE_ACCOUNT_NAMESPACE="default"
71+
export SERVICE_ACCOUNT_NAME="workload-identity-sa"
72+
export SUBSCRIPTION="$(az account show --query id --output tsv)"
73+
export USER_ASSIGNED_IDENTITY_NAME="myIdentity"
74+
export FEDERATED_IDENTITY_CREDENTIAL_NAME="myFedIdentity"
75+
export KEYVAULT_NAME="azwi-kv-tutorial"
76+
export KEYVAULT_SECRET_NAME="my-secret"
9877
```
9978

10079
## Create AKS cluster
10180

10281
Create an AKS cluster using the [az aks create][az-aks-create] command with the `--enable-oidc-issuer` parameter to use the OIDC Issuer. The following example creates a cluster named *myAKSCluster* with one node in the *myResourceGroup*:
10382

10483
```azurecli-interactive
105-
az aks create -g myResourceGroup -n myAKSCluster --node-count 1 --enable-oidc-issuer --enable-workload-identity --generate-ssh-keys
84+
az aks create -g "${RESOURCE_GROUP}" -n myAKSCluster --node-count 1 --enable-oidc-issuer --enable-workload-identity
10685
```
10786

10887
After a few minutes, the command completes and returns JSON-formatted information about the cluster.
10988

11089
> [!NOTE]
11190
> When you create an AKS cluster, a second resource group is automatically created to store the AKS resources. For more information, see [Why are two resource groups created with AKS?][aks-two-resource-groups].
11291
113-
To get the OIDC Issuer URL and save it to an environmental variable, run the following command. Replace the default value for the arguments `-n`, which is the name of the cluster and `-g`, the resource group name:
92+
To get the OIDC Issuer URL and save it to an environmental variable, run the following command. Replace the default value for the arguments `-n`, which is the name of the cluster:
11493

11594
```azurecli-interactive
116-
export AKS_OIDC_ISSUER="$(az aks show -n myAKSCluster -g myResourceGroup --query "oidcIssuerProfile.issuerUrl" -otsv)"
117-
```
118-
119-
## Export environmental variables
120-
121-
To help simplify steps to configure creating Azure Key Vault and other identities required, the steps below define
122-
environmental variables for reference on the cluster.
123-
124-
Run the following commands to create these variables. Replace the default values for `RESOURCE_GROUP`, `LOCATION`, `KEYVAULT_SECRET_NAME`, `SERVICE_ACCOUNT_NAME`, `SUBSCRIPTION`, `UAID`, and `FICID`.
125-
126-
```bash
127-
# environment variables for the Azure Key Vault resource
128-
export KEYVAULT_NAME="azwi-kv-tutorial"
129-
export KEYVAULT_SECRET_NAME="my-secret"
130-
export RESOURCE_GROUP="resourceGroupName"
131-
export LOCATION="westcentralus"
132-
133-
# environment variables for the Kubernetes Service account & federated identity credential
134-
export SERVICE_ACCOUNT_NAMESPACE="default"
135-
export SERVICE_ACCOUNT_NAME="workload-identity-sa"
136-
137-
# environment variables for the Federated Identity
138-
export SUBSCRIPTION="{your subscription ID}"
139-
# user assigned identity name
140-
export UAID="fic-test-ua"
141-
# federated identity name
142-
export FICID="fic-test-fic-name"
95+
export AKS_OIDC_ISSUER="$(az aks show -n myAKSCluster -g "${RESOURCE_GROUP}" --query "oidcIssuerProfile.issuerUrl" -otsv)"
14396
```
14497

14598
## Create an Azure Key Vault and secret
@@ -166,7 +119,7 @@ az keyvault secret set --vault-name "${KEYVAULT_NAME}" --name "${KEYVAULT_SECRET
166119
To add the Key Vault URL to the environment variable `KEYVAULT_URL`, you can run the Azure CLI [az keyvault show][az-keyvault-show] command.
167120

168121
```bash
169-
export KEYVAULT_URL="$(az keyvault show -g ${RESOURCE_GROUP} -n ${KEYVAULT_NAME} --query properties.vaultUri -o tsv)"
122+
export KEYVAULT_URL="$(az keyvault show -g "${RESOURCE_GROUP}" -n ${KEYVAULT_NAME} --query properties.vaultUri -o tsv)"
170123
```
171124

172125
## Create a managed identity and grant permissions to access the secret
@@ -178,13 +131,13 @@ az account set --subscription "${SUBSCRIPTION}"
178131
```
179132

180133
```azurecli-interactive
181-
az identity create --name "${UAID}" --resource-group "${RESOURCE_GROUP}" --location "${LOCATION}" --subscription "${SUBSCRIPTION}"
134+
az identity create --name "${USER_ASSIGNED_IDENTITY_NAME}" --resource-group "${RESOURCE_GROUP}" --location "${LOCATION}" --subscription "${SUBSCRIPTION}"
182135
```
183136

184137
Next, you need to set an access policy for the managed identity to access the Key Vault secret by running the following commands:
185138

186139
```azurecli-interactive
187-
export USER_ASSIGNED_CLIENT_ID="$(az identity show --resource-group "${RESOURCE_GROUP}" --name "${UAID}" --query 'clientId' -otsv)"
140+
export USER_ASSIGNED_CLIENT_ID="$(az identity show --resource-group "${RESOURCE_GROUP}" --name "${USER_ASSIGNED_IDENTITY_NAME}" --query 'clientId' -otsv)"
188141
```
189142

190143
```azurecli-interactive
@@ -226,7 +179,7 @@ Serviceaccount/workload-identity-sa created
226179
Use the [az identity federated-credential create][az-identity-federated-credential-create] command to create the federated identity credential between the managed identity, the service account issuer, and the subject.
227180

228181
```azurecli-interactive
229-
az identity federated-credential create --name ${FICID} --identity-name ${UAID} --resource-group ${RESOURCE_GROUP} --issuer ${AKS_OIDC_ISSUER} --subject system:serviceaccount:${SERVICE_ACCOUNT_NAMESPACE}:${SERVICE_ACCOUNT_NAME}
182+
az identity federated-credential create --name ${FEDERATED_IDENTITY_CREDENTIAL_NAME} --identity-name ${USER_ASSIGNED_IDENTITY_NAME} --resource-group ${RESOURCE_GROUP} --issuer ${AKS_OIDC_ISSUER} --subject system:serviceaccount:${SERVICE_ACCOUNT_NAMESPACE}:${SERVICE_ACCOUNT_NAME}
230183
```
231184

232185
> [!NOTE]
@@ -243,8 +196,6 @@ kind: Pod
243196
metadata:
244197
name: quick-start
245198
namespace: ${SERVICE_ACCOUNT_NAMESPACE}
246-
labels:
247-
azure.workload.identity/use: "true"
248199
spec:
249200
serviceAccountName: ${SERVICE_ACCOUNT_NAME}
250201
containers:
@@ -266,7 +217,7 @@ The following output resembles successful creation of the pod:
266217
pod/quick-start created
267218
```
268219

269-
To check whether all properties are injected properly by the webhook, use
220+
To check whether all properties are injected properly with the webhook, use
270221
the [kubectl describe][kubelet-describe] command:
271222

272223
```bash
@@ -307,7 +258,7 @@ az group delete --name "${RESOURCE_GROUP}"
307258
## Next steps
308259

309260
In this tutorial, you deployed a Kubernetes cluster and then deployed a simple container application to
310-
test working with an Azure AD workload identity (preview).
261+
test working with an Azure AD workload identity.
311262

312263
This tutorial is for introductory purposes. For guidance on a creating full solutions with AKS for production, see [AKS solution guidance][aks-solution-guidance].
313264

articles/aks/workload-identity-deploy-cluster.md

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: Deploy and configure an Azure Kubernetes Service (AKS) cluster with workload identity
33
description: In this Azure Kubernetes Service (AKS) article, you deploy an Azure Kubernetes Service cluster and configure it with an Azure AD workload identity.
44
ms.topic: article
5+
ms.custom: devx-track-azurecli
56
ms.date: 04/18/2023
6-
77
---
88

99
# Deploy and configure workload identity on an Azure Kubernetes Service (AKS) cluster
@@ -23,55 +23,69 @@ This article assumes you have a basic understanding of Kubernetes concepts. For
2323

2424
- If you have multiple Azure subscriptions, select the appropriate subscription ID in which the resources should be billed using the [az account][az-account] command.
2525

26+
## Export environmental variables
27+
28+
To help simplify steps to configure the identities required, the steps below define
29+
environmental variables for reference on the cluster.
30+
31+
Run the following commands to create these variables. Replace the default values for `RESOURCE_GROUP`, `LOCATION`, `SERVICE_ACCOUNT_NAME`, `SUBSCRIPTION`, `USER_ASSIGNED_IDENTITY_NAME`, and `FEDERATED_IDENTITY_CREDENTIAL_NAME`.
32+
33+
```bash
34+
export RESOURCE_GROUP="myResourceGroup"
35+
export LOCATION="westcentralus"
36+
export SERVICE_ACCOUNT_NAMESPACE="default"
37+
export SERVICE_ACCOUNT_NAME="workload-identity-sa"
38+
export SUBSCRIPTION="$(az account show --query id --output tsv)"
39+
export USER_ASSIGNED_IDENTITY_NAME="myIdentity"
40+
export FEDERATED_IDENTITY_CREDENTIAL="myFedIdentity"
41+
```
42+
2643
## Create AKS cluster
2744

2845
Create an AKS cluster using the [az aks create][az-aks-create] command with the `--enable-oidc-issuer` parameter to use the OIDC Issuer. The following example creates a cluster named *myAKSCluster* with one node in the *myResourceGroup*:
2946

3047
```azurecli-interactive
31-
az group create --name myResourceGroup --location eastus
48+
az group create --name "${RESOURCE_GROUP}" --location "${LOCATION}"
3249
33-
az aks create -g myResourceGroup -n myAKSCluster --enable-oidc-issuer --enable-workload-identity
50+
az aks create -g "${RESOURCE_GROUP}" -n myAKSCluster --enable-oidc-issuer --enable-workload-identity
3451
```
3552

3653
After a few minutes, the command completes and returns JSON-formatted information about the cluster.
3754

3855
> [!NOTE]
3956
> When you create an AKS cluster, a second resource group is automatically created to store the AKS resources. For more information, see [Why are two resource groups created with AKS?][aks-two-resource-groups].
4057
41-
To get the OIDC Issuer URL and save it to an environmental variable, run the following command. Replace the default values for the cluster name and the resource group name.
58+
To get the OIDC Issuer URL and save it to an environmental variable, run the following command. Replace the default value for the arguments `-n`, which is the name of the cluster:
4259

4360
```bash
44-
export AKS_OIDC_ISSUER="$(az aks show -n myAKSCluster -g myResourceGroup --query "oidcIssuerProfile.issuerUrl" -otsv)"
61+
export AKS_OIDC_ISSUER="$(az aks show -n myAKSCluster -g "${RESOURCE_GROUP}" --query "oidcIssuerProfile.issuerUrl" -otsv)"
4562
```
4663

4764
## Create a managed identity
4865

4966
Use the Azure CLI [az account set][az-account-set] command to set a specific subscription to be the current active subscription. Then use the [az identity create][az-identity-create] command to create a managed identity.
5067

5168
```azurecli
52-
export SUBSCRIPTION_ID="$(az account show --query id --output tsv)"
53-
export USER_ASSIGNED_IDENTITY_NAME="myIdentity"
54-
export RG_NAME="myResourceGroup"
55-
export LOCATION="eastus"
69+
az identity create --name "${USER_ASSIGNED_IDENTITY_NAME}" --resource-group "${RESOURCE_GROUP}" --location "${LOCATION}" --subscription "${SUBSCRIPTION}"
70+
```
71+
72+
Next, let's create a variable for the managed identity ID.
5673

57-
az identity create --name "${USER_ASSIGNED_IDENTITY_NAME}" --resource-group "${RG_NAME}" --location "${LOCATION}" --subscription "${SUBSCRIPTION_ID}"
74+
```bash
75+
export USER_ASSIGNED_CLIENT_ID="$(az identity show --resource-group "${RESOURCE_GROUP}" --name "${USER_ASSIGNED_IDENTITY_NAME}" --query 'clientId' -otsv)"
5876
```
5977

6078
## Create Kubernetes service account
6179

6280
Create a Kubernetes service account and annotate it with the client ID of the managed identity created in the previous step. Use the [az aks get-credentials][az-aks-get-credentials] command and replace the values for the cluster name and the resource group name.
6381

6482
```azurecli
65-
az aks get-credentials -n myAKSCluster -g myResourceGroup
83+
az aks get-credentials -n myAKSCluster -g "${RESOURCE_GROUP}"
6684
```
6785

68-
Copy and paste the following multi-line input in the Azure CLI, and update the values for `SERVICE_ACCOUNT_NAME` and `SERVICE_ACCOUNT_NAMESPACE` with the Kubernetes service account name and its namespace.
86+
Copy and paste the following multi-line input in the Azure CLI.
6987

7088
```bash
71-
export SERVICE_ACCOUNT_NAME="workload-identity-sa"
72-
export SERVICE_ACCOUNT_NAMESPACE="my-namespace"
73-
export USER_ASSIGNED_CLIENT_ID="$(az identity show --resource-group "${RESOURCE_GROUP}" --name "${UAID}" --query 'clientId' -otsv)"
74-
7589
cat <<EOF | kubectl apply -f -
7690
apiVersion: v1
7791
kind: ServiceAccount
@@ -94,7 +108,7 @@ Serviceaccount/workload-identity-sa created
94108
Use the [az identity federated-credential create][az-identity-federated-credential-create] command to create the federated identity credential between the managed identity, the service account issuer, and the subject.
95109

96110
```azurecli
97-
az identity federated-credential create --name myfederatedIdentity --identity-name "${USER_ASSIGNED_IDENTITY_NAME}" --resource-group "${RG_NAME}" --issuer "${AKS_OIDC_ISSUER}" --subject system:serviceaccount:"${SERVICE_ACCOUNT_NAMESPACE}":"${SERVICE_ACCOUNT_NAME}" --audience api://AzureADTokenExchange
111+
az identity federated-credential create --name ${FEDERATED_IDENTITY_CREDENTIAL_NAME} --identity-name "${USER_ASSIGNED_IDENTITY_NAME}" --resource-group "${RESOURCE_GROUP}" --issuer "${AKS_OIDC_ISSUER}" --subject system:serviceaccount:"${SERVICE_ACCOUNT_NAMESPACE}":"${SERVICE_ACCOUNT_NAME}" --audience api://AzureADTokenExchange
98112
```
99113

100114
> [!NOTE]
@@ -151,10 +165,10 @@ You can retrieve this information using the Azure CLI command: [az keyvault list
151165
1. Set an access policy for the managed identity to access secrets in your Key Vault by running the following commands:
152166

153167
```azurecli
154-
export RG_NAME="myResourceGroup"
168+
export RESOURCE_GROUP="myResourceGroup"
155169
export USER_ASSIGNED_IDENTITY_NAME="myIdentity"
156170
export KEYVAULT_NAME="myKeyVault"
157-
export USER_ASSIGNED_CLIENT_ID="$(az identity show --resource-group "${RG_NAME}" --name "${USER_ASSIGNED_IDENTITY_NAME}" --query 'clientId' -otsv)"
171+
export USER_ASSIGNED_CLIENT_ID="$(az identity show --resource-group "${RESOURCE_GROUP}" --name "${USER_ASSIGNED_IDENTITY_NAME}" --query 'clientId' -otsv)"
158172
159173
az keyvault set-policy --name "${KEYVAULT_NAME}" --secret-permissions get --spn "${USER_ASSIGNED_CLIENT_ID}"
160174
```

0 commit comments

Comments
 (0)