Skip to content

Commit 84fb3c5

Browse files
authored
Merge pull request #229333 from schaffererin/aks-service-principal
Addressing GitIssues
2 parents a9f68b3 + 2637a44 commit 84fb3c5

File tree

1 file changed

+33
-36
lines changed

1 file changed

+33
-36
lines changed

articles/aks/kubernetes-service-principal.md

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Azure PowerShell version 5.0.0 or later. Run `Get-InstalledModule -Name Az` to f
3232

3333
### [Azure CLI](#tab/azure-cli)
3434

35-
To manually create a service principal with the Azure CLI, use the [az ad sp create-for-rbac][az-ad-sp-create] command.
35+
To manually create a service principal with the Azure CLI, use the [`az ad sp create-for-rbac`][az-ad-sp-create] command.
3636

3737
```azurecli-interactive
3838
az ad sp create-for-rbac --name myAKSClusterServicePrincipal
@@ -52,7 +52,7 @@ The output is similar to the following example. Copy the values for `appId` and
5252

5353
### [Azure PowerShell](#tab/azure-powershell)
5454

55-
To manually create a service principal with Azure PowerShell, use the [New-AzADServicePrincipal][new-azadserviceprincipal] command.
55+
To manually create a service principal with Azure PowerShell, use the [`New-AzADServicePrincipal`][new-azadserviceprincipal] command.
5656

5757
```azurepowershell-interactive
5858
New-AzADServicePrincipal -DisplayName myAKSClusterServicePrincipal -OutVariable sp
@@ -85,7 +85,7 @@ For more information, see [Create an Azure service principal with Azure PowerShe
8585

8686
### [Azure CLI](#tab/azure-cli)
8787

88-
To use an existing service principal when you create an AKS cluster using the [az aks create][az-aks-create] command, use the `--service-principal` and `--client-secret` parameters to specify the `appId` and `password` from the output of the [az ad sp create-for-rbac][az-ad-sp-create] command:
88+
To use an existing service principal when you create an AKS cluster using the [`az aks create`][az-aks-create] command, use the `--service-principal` and `--client-secret` parameters to specify the `appId` and `password` from the output of the [`az ad sp create-for-rbac`][az-ad-sp-create] command:
8989

9090
```azurecli-interactive
9191
az aks create \
@@ -123,7 +123,7 @@ The service principal for the AKS cluster can be used to access other resources.
123123

124124
### [Azure CLI](#tab/azure-cli)
125125

126-
To delegate permissions, create a role assignment using the [az role assignment create][az-role-assignment-create] command. Assign the `appId` to a particular scope, such as a resource group or virtual network resource. A role then defines what permissions the service principal has on the resource, as shown in the following example:
126+
To delegate permissions, create a role assignment using the [`az role assignment create`][az-role-assignment-create] command. Assign the `appId` to a particular scope, such as a resource group or virtual network resource. A role then defines what permissions the service principal has on the resource, as shown in the following example:
127127

128128
```azurecli
129129
az role assignment create --assignee <appId> --scope <resourceScope> --role Contributor
@@ -133,7 +133,7 @@ The `--scope` for a resource needs to be a full resource ID, such as */subscript
133133

134134
### [Azure PowerShell](#tab/azure-powershell)
135135

136-
To delegate permissions, create a role assignment using the [New-AzRoleAssignment][new-azroleassignment] command. Assign the `ApplicationId` to a particular scope, such as a resource group or virtual network resource. A role then defines what permissions the service principal has on the resource, as shown in the following example:
136+
To delegate permissions, create a role assignment using the [`New-AzRoleAssignment`][new-azroleassignment] command. Assign the `ApplicationId` to a particular scope, such as a resource group or virtual network resource. A role then defines what permissions the service principal has on the resource, as shown in the following example:
137137

138138
```azurepowershell-interactive
139139
New-AzRoleAssignment -ApplicationId <ApplicationId> -Scope <resourceScope> -RoleDefinitionName Contributor
@@ -153,11 +153,11 @@ The following sections detail common delegations that you may need to assign.
153153

154154
### [Azure CLI](#tab/azure-cli)
155155

156-
If you use Azure Container Registry (ACR) as your container image store, you need to grant permissions to the service principal for your AKS cluster to read and pull images. Currently, the recommended configuration is to use the [az aks create][az-aks-create] or [az aks update][az-aks-update] command to integrate with a registry and assign the appropriate role for the service principal. For detailed steps, see [Authenticate with Azure Container Registry from Azure Kubernetes Service][aks-to-acr].
156+
If you use Azure Container Registry (ACR) as your container image store, you need to grant permissions to the service principal for your AKS cluster to read and pull images. Currently, the recommended configuration is to use the [`az aks create`][az-aks-create] or [`az aks update`][az-aks-update] command to integrate with a registry and assign the appropriate role for the service principal. For detailed steps, see [Authenticate with Azure Container Registry from Azure Kubernetes Service][aks-to-acr].
157157

158158
### [Azure PowerShell](#tab/azure-powershell)
159159

160-
If you use Azure Container Registry (ACR) as your container image store, you need to grant permissions to the service principal for your AKS cluster to read and pull images. Currently, the recommended configuration is to use the [New-AzAksCluster][new-azakscluster] or [Set-AzAksCluster][set-azakscluster] command to integrate with a registry and assign the appropriate role for the service principal. For detailed steps, see [Authenticate with Azure Container Registry from Azure Kubernetes Service][aks-to-acr].
160+
If you use Azure Container Registry (ACR) as your container image store, you need to grant permissions to the service principal for your AKS cluster to read and pull images. Currently, the recommended configuration is to use the [`New-AzAksCluster`][new-azakscluster] or [`Set-AzAksCluster`][set-azakscluster] command to integrate with a registry and assign the appropriate role for the service principal. For detailed steps, see [Authenticate with Azure Container Registry from Azure Kubernetes Service][aks-to-acr].
161161

162162
---
163163

@@ -189,15 +189,12 @@ When using AKS and an Azure AD service principal, consider the following:
189189
- Every service principal is associated with an Azure AD application. The service principal for a Kubernetes cluster can be associated with any valid Azure AD application name (for example: *https://www.contoso.org/example*). The URL for the application doesn't have to be a real endpoint.
190190
- When you specify the service principal **Client ID**, use the value of the `appId`.
191191
- On the agent node VMs in the Kubernetes cluster, the service principal credentials are stored in the file `/etc/kubernetes/azure.json`
192-
- When you use the [az aks create][az-aks-create] command to generate the service principal automatically, the service principal credentials are written to the file `~/.azure/aksServicePrincipal.json` on the machine used to run the command.
193-
- If you don't specify a service principal with AKS CLI commands, the default service principal located at `~/.azure/aksServicePrincipal.json` is used.
194-
- You can optionally remove the `aksServicePrincipal.json` file, and AKS creates a new service principal.
195-
- When you delete an AKS cluster that was created by [az aks create][az-aks-create], the service principal created automatically isn't deleted.
196-
- To delete the service principal, query for your clusters *servicePrincipalProfile.clientId* and then delete it using the [az ad sp delete][az-ad-sp-delete] command. Replace the values for the `-g` parameter for the resource group name, and `-n` parameter for the cluster name:
192+
- When you delete an AKS cluster that was created by [`az aks create`][az-aks-create], the service principal created automatically isn't deleted.
193+
- To delete the service principal, query for your clusters *servicePrincipalProfile.clientId* and then delete it using the [`az ad sp delete`][az-ad-sp-delete] command. Replace the values for the `-g` parameter for the resource group name, and `-n` parameter for the cluster name:
197194

198-
```azurecli
199-
az ad sp delete --id $(az aks show -g myResourceGroup -n myAKSCluster --query servicePrincipalProfile.clientId -o tsv)
200-
```
195+
```azurecli
196+
az ad sp delete --id $(az aks show -g myResourceGroup -n myAKSCluster --query servicePrincipalProfile.clientId -o tsv)
197+
```
201198
202199
### [Azure PowerShell](#tab/azure-powershell)
203200
@@ -208,59 +205,57 @@ When using AKS and an Azure AD service principal, consider the following:
208205
- Every service principal is associated with an Azure AD application. The service principal for a Kubernetes cluster can be associated with any valid Azure AD application name (for example: *https://www.contoso.org/example*). The URL for the application doesn't have to be a real endpoint.
209206
- When you specify the service principal **Client ID**, use the value of the `ApplicationId`.
210207
- On the agent node VMs in the Kubernetes cluster, the service principal credentials are stored in the file `/etc/kubernetes/azure.json`
211-
- When you use the [New-AzAksCluster][new-azakscluster] command to generate the service principal automatically, the service principal credentials are written to the file `~/.azure/acsServicePrincipal.json` on the machine used to run the command.
212-
- If you don't specify a service principal with AKS PowerShell commands, the default service principal located at `~/.azure/acsServicePrincipal.json` is used.
213-
- You can optionally remove the `acsServicePrincipal.json` file, and AKS creates a new service principal.
214-
- When you delete an AKS cluster that was created by [New-AzAksCluster][new-azakscluster], the service principal created automatically isn't deleted.
215-
- To delete the service principal, query for your clusters *ServicePrincipalProfile.ClientId* and then delete it using the [Remove-AzADServicePrincipal][remove-azadserviceprincipal] command. Replace the values for the `-ResourceGroupName` parameter for the resource group name, and `-Name` parameter for the cluster name:
216-
217-
```azurepowershell-interactive
218-
$ClientId = (Get-AzAksCluster -ResourceGroupName myResourceGroup -Name myAKSCluster ).ServicePrincipalProfile.ClientId
219-
Remove-AzADServicePrincipal -ApplicationId $ClientId
220-
```
208+
- When you delete an AKS cluster that was created by [`New-AzAksCluster`][new-azakscluster], the service principal created automatically isn't deleted.
209+
- To delete the service principal, query for your clusters *ServicePrincipalProfile.ClientId* and then delete it using the [`Remove-AzADServicePrincipal`][remove-azadserviceprincipal] command. Replace the values for the `-ResourceGroupName` parameter for the resource group name, and `-Name` parameter for the cluster name:
210+
211+
```azurepowershell-interactive
212+
$ClientId = (Get-AzAksCluster -ResourceGroupName myResourceGroup -Name myAKSCluster ).ServicePrincipalProfile.ClientId
213+
Remove-AzADServicePrincipal -ApplicationId $ClientId
214+
```
215+
221216
---
222217
223218
## Troubleshoot
224219
225220
### [Azure CLI](#tab/azure-cli)
226221
227-
The service principal credentials for an AKS cluster are cached by the Azure CLI. If these credentials have expired, you encounter errors during deployment of the AKS cluster. The following error message when running [az aks create][az-aks-create] may indicate a problem with the cached service principal credentials:
222+
The service principal credentials for an AKS cluster are cached by the Azure CLI. If these credentials have expired, you encounter errors during deployment of the AKS cluster. The following error message when running [`az aks create`][az-aks-create] may indicate a problem with the cached service principal credentials:
228223
229-
```console
224+
```azurecli
230225
Operation failed with status: 'Bad Request'.
231226
Details: The credentials in ServicePrincipalProfile were invalid. Please see https://aka.ms/aks-sp-help for more details.
232227
(Details: adal: Refresh request failed. Status Code = '401'.
233228
```
234229

235-
Check the age of the credentials file by running the following command:
230+
Check the expiration date of your service principal credentials using the [`az ad app credential list`][az-ad-app-credential-list] command with the `"[].endDateTime"` query.
236231

237-
```console
238-
ls -la $HOME/.azure/aksServicePrincipal.json
232+
```azurecli
233+
az ad app credential list --id <app-id> --query "[].endDateTime" -o tsv
239234
```
240235

241-
The default expiration time for the service principal credentials is one year. If your *aksServicePrincipal.json* file is older than one year, delete the file and retry deploying the AKS cluster.
236+
The default expiration time for the service principal credentials is one year. If your credentials are older than one year, you can [reset the existing credentials](/update-credentials#reset-the-existing-service-principal-credentials) or [create a new service principal](/update-credentials#create-a-new-service-principal).
242237

243238
**General Azure CLI troubleshooting**
244239

245240
[!INCLUDE [azure-cli-troubleshooting.md](../../includes/azure-cli-troubleshooting.md)]
246241

247242
### [Azure PowerShell](#tab/azure-powershell)
248243

249-
The service principal credentials for an AKS cluster are cached by Azure PowerShell. If these credentials have expired, you encounter errors during deployment of the AKS cluster. The following error message when running [New-AzAksCluster][new-azakscluster] may indicate a problem with the cached service principal credentials:
244+
The service principal credentials for an AKS cluster are cached by Azure PowerShell. If these credentials have expired, you encounter errors during deployment of the AKS cluster. The following error message when running [`New-AzAksCluster`][new-azakscluster] may indicate a problem with the cached service principal credentials:
250245

251-
```console
246+
```azurepowershell-interactive
252247
Operation failed with status: 'Bad Request'.
253248
Details: The credentials in ServicePrincipalProfile were invalid. Please see https://aka.ms/aks-sp-help for more details.
254249
(Details: adal: Refresh request failed. Status Code = '401'.
255250
```
256251

257-
Check the age of the credentials file by running the following command:
252+
Check the expiration date of your service principal credentials using the [Get-AzADAppCredential][get-azadappcredential] command. The output will show you the `StartDateTime` of your credentials.
258253

259254
```azurepowershell-interactive
260-
Get-ChildItem -Path $HOME/.azure/aksServicePrincipal.json
255+
Get-AzADAppCredential -ApplicationId <ApplicationId>
261256
```
262257

263-
The default expiration time for the service principal credentials is one year. If your *aksServicePrincipal.json* file is older than one year, delete the file and retry deploying the AKS cluster.
258+
The default expiration time for the service principal credentials is one year. If your credentials are older than one year, you can [reset the existing credentials](/update-credentials#reset-the-existing-service-principal-credentials) or [create a new service principal](/update-credentials#create-a-new-service-principal).
264259

265260
---
266261

@@ -275,6 +270,7 @@ For information on how to update the credentials, see [Update or rotate the cred
275270
[acr-intro]: ../container-registry/container-registry-intro.md
276271
[az-ad-sp-create]: /cli/azure/ad/sp#az_ad_sp_create_for_rbac
277272
[az-ad-sp-delete]: /cli/azure/ad/sp#az_ad_sp_delete
273+
[az-ad-app-credential-list]: /cli/azure/ad/app/credential#az_ad_app_credential_list
278274
[azure-load-balancer-overview]: ../load-balancer/load-balancer-overview.md
279275
[install-azure-cli]: /cli/azure/install-azure-cli
280276
[service-principal]:../active-directory/develop/app-objects-and-service-principals.md
@@ -294,6 +290,7 @@ For information on how to update the credentials, see [Update or rotate the cred
294290
[install-the-azure-az-powershell-module]: /powershell/azure/install-az-ps
295291
[new-azakscluster]: /powershell/module/az.aks/new-azakscluster
296292
[new-azadserviceprincipal]: /powershell/module/az.resources/new-azadserviceprincipal
293+
[get-azadappcredential]: /powershell/module/az.resources/get-azadappcredential
297294
[create-an-azure-service-principal-with-azure-powershell]: /powershell/azure/create-azure-service-principal-azureps
298295
[new-azroleassignment]: /powershell/module/az.resources/new-azroleassignment
299296
[set-azakscluster]: /powershell/module/az.aks/set-azakscluster

0 commit comments

Comments
 (0)