Skip to content

Commit 316c62c

Browse files
Merge pull request #219107 from naioja/aj_azwi
Adding resource group to cluster creation and variables to "Create a managed identity and grant permissions to access Azure Key Vault" section
2 parents ad9815f + 6a36457 commit 316c62c

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ az provider register --namespace Microsoft.ContainerService
6767
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*:
6868

6969
```azurecli-interactive
70+
az group create --name myResourceGroup --location eastus
71+
7072
az aks create -g myResourceGroup -n myAKSCluster --node-count 1 --enable-oidc-issuer --enable-workload-identity --generate-ssh-keys
7173
```
7274

@@ -95,44 +97,49 @@ You can retrieve this information using the Azure CLI command: [az keyvault list
9597
1. 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.
9698

9799
```azurecli
98-
az account set --subscription "subscriptionID"
99-
```
100+
export SUBSCRIPTION_ID="$(az account show --query id --output tsv)"
101+
export USER_ASSIGNED_IDENTITY_NAME="myIdentity"
102+
export RG_NAME="myResourceGroup"
103+
export LOCATION="eastus"
100104
101-
```azurecli
102-
az identity create --name "userAssignedIdentityName" --resource-group "resourceGroupName" --location "location" --subscription "subscriptionID"
105+
az identity create --name "${USER_ASSIGNED_IDENTITY_NAME}" --resource-group "${RG_NAME}" --location "${LOCATION}" --subscription "${SUBSCRIPTION_ID}"
103106
```
104107
105108
2. Set an access policy for the managed identity to access secrets in your Key Vault by running the following commands:
106109
107-
```bash
108-
export USER_ASSIGNED_CLIENT_ID="$(az identity show --resource-group "resourceGroupName" --name "userAssignedIdentityName" --query 'clientId' -otsv)"
109-
```
110-
111110
```azurecli
112-
az keyvault set-policy --name "keyVaultName" --secret-permissions get --spn "${USER_ASSIGNED_CLIENT_ID}"
111+
export RG_NAME="myResourceGroup"
112+
export USER_ASSIGNED_IDENTITY_NAME="myIdentity"
113+
export KEYVAULT_NAME="myKeyVault"
114+
export USER_ASSIGNED_CLIENT_ID="$(az identity show --resource-group "${RG_NAME}" --name "${USER_ASSIGNED_IDENTITY_NAME}" --query 'clientId' -otsv)"
115+
116+
az keyvault set-policy --name "${KEYVAULT_NAME}" --secret-permissions get --spn "${USER_ASSIGNED_CLIENT_ID}"
113117
```
114118
115119
## Create Kubernetes service account
116120
117121
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.
118122
119123
```azurecli
120-
az aks get-credentials -n myAKSCluster -g MyResourceGroup
124+
az aks get-credentials -n myAKSCluster -g myResourceGroup
121125
```
122126

123-
Copy and paste the following multi-line input in the Azure CLI, and update the values for `serviceAccountName` and `serviceAccountNamespace` with the Kubernetes service account name and its namespace.
127+
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.
124128

125129
```bash
130+
export SERVICE_ACCOUNT_NAME="workload-identity-sa"
131+
export SERVICE_ACCOUNT_NAMESPACE="my-namespace"
132+
126133
cat <<EOF | kubectl apply -f -
127134
apiVersion: v1
128135
kind: ServiceAccount
129136
metadata:
130137
annotations:
131-
azure.workload.identity/client-id: ${USER_ASSIGNED_CLIENT_ID}
138+
azure.workload.identity/client-id: "${USER_ASSIGNED_CLIENT_ID}"
132139
labels:
133140
azure.workload.identity/use: "true"
134-
name: serviceAccountName
135-
namespace: serviceAccountNamspace
141+
name: "${SERVICE_ACCOUNT_NAME}"
142+
namespace: "${SERVICE_ACCOUNT_NAMESPACE}"
136143
EOF
137144
```
138145

@@ -144,10 +151,10 @@ Serviceaccount/workload-identity-sa created
144151

145152
## Establish federated identity credential
146153

147-
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. Replace the values `resourceGroupName`, `userAssignedIdentityName`, `federatedIdentityName`, `serviceAccountNamespace`, and `serviceAccountName`.
154+
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.
148155

149156
```azurecli
150-
az identity federated-credential create --name federatedIdentityName --identity-name userAssignedIdentityName --resource-group resourceGroupName --issuer ${AKS_OIDC_ISSUER} --subject system:serviceaccount:serviceAccountNamespace:serviceAccountName
157+
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}"
151158
```
152159

153160
> [!NOTE]

0 commit comments

Comments
 (0)