Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Azure Kubernetes Service (AKS) can be configured to use Microsoft Entra ID for user authentication. In this configuration, you sign in to an AKS cluster using a Microsoft Entra authentication token. Once authenticated, you can use the built-in Kubernetes role-based access control (RBAC) to manage access to namespaces and cluster resources based on a user's identity or group membership.
This article shows you how to:
Control access using Kubernetes RBAC in an AKS cluster based on Microsoft Entra group membership.
Create example groups and users in Microsoft Entra ID.
Create Roles and RoleBindings in an AKS cluster granting the appropriate permissions, such as to create and view resources.
Prerequisites
You have an existing AKS cluster with Microsoft Entra integration enabled. If you need an AKS cluster with this configuration, see Integrate Microsoft Entra ID with AKS.
Kubernetes RBAC is enabled by default during AKS cluster creation. To upgrade an existing cluster with Microsoft Entra integration and Kubernetes RBAC, see Enable Microsoft Entra integration on your existing AKS cluster.
Make sure that Azure CLI version 2.0.61 or later is installed and configured. To find the version, run
az --version. To install or upgrade, see Install Azure CLI.If using Terraform, install Terraform version 2.99.0 or later.
Use the Azure portal or Azure CLI to verify Microsoft Entra integration with Kubernetes RBAC is enabled.
To verify using the Azure portal:
- Sign-in to the Azure portal and navigate to your AKS cluster resource.
- In the service menu, under Settings, select Security configuration.
- Under the Authentication and Authorization section, verify the Microsoft Entra authentication with Kubernetes RBAC option is selected.
Create groups in Microsoft Entra ID
This section teaches you how to create two user roles to show how Kubernetes RBAC and Microsoft Entra ID control access cluster resources. The following two example roles are:
Application developer
- A user named aksdev that's part of the appdev group.
Site reliability engineer (SRE)
- A user named akssre that's part of the opssre group.
In production environments, you can use existing users and groups within a Microsoft Entra tenant.
First, get the resource ID of your AKS cluster using the
az aks showcommand. Then, assign the resource ID to a variable named AKS_ID so it can be referenced in other commands.AKS_ID=$(az aks show \ --resource-group myResourceGroup \ --name myAKSCluster \ --query id -o tsv)Create the first example group in Microsoft Entra ID for the application developers using the
az ad group createcommand. The following example creates a group named appdev:APPDEV_ID=$(az ad group create --display-name appdev --mail-nickname appdev --query id -o tsv)Create an Azure role assignment for the appdev group using the
az role assignment createcommand. This assignment lets any member of the group usekubectlto interact with an AKS cluster by granting them the Azure Kubernetes Service Cluster User Role.az role assignment create \ --assignee $APPDEV_ID \ --role "Azure Kubernetes Service Cluster User Role" \ --scope $AKS_IDTip
If you receive an error such as
Principal 35bfec9328bd4d8d9b54dea6dac57b82 doesn't exist in the directory a5443dcd-cd0e-494d-a387-3039b419f0d5., wait a few seconds for the Microsoft Entra group object ID to propagate through the directory then try theaz role assignment createcommand again.
Create users in Microsoft Entra ID
After you create the example Microsoft Entra ID groups for application developers and SREs, the next step is to create two corresponding user accounts. These users are used to sign in to the AKS cluster and validate the Kubernetes RBAC integration described later in this article.
Before you begin, you must set the user principal name (UPN) and password for the application developers. The UPN must include the verified domain name of your tenant. For example, an application developer user, aksdev@contoso.com. In order to figure out (or set) the verified domain names in your tenant, see Managing custom domain names in your Microsoft Entra ID.
The following command prompts you for the UPN and sets it to AAD_DEV_UPN so it can be used in a later command:
echo "Please enter the UPN for application developers: " && read AAD_DEV_UPN
The following command prompts you for the password and sets it to AAD_DEV_PW for use in a later command:
echo "Please enter the secure password for application developers: " && read AAD_DEV_PW
Create user accounts
Create the first user account in Microsoft Entra ID using the
az ad user createcommand. The following example creates a user with the display name AKS Dev, the UPN, and secure password using the values in AAD_DEV_UPN and AAD_DEV_PW:AKSDEV_ID=$(az ad user create \ --display-name "AKS Dev" \ --user-principal-name $AAD_DEV_UPN \ --password $AAD_DEV_PW \ --query id -o tsv)Add the user to the appdev group created in the previous section using the
az ad group member addcommand:az ad group member add --group appdev --member-id $AKSDEV_ID
Create AKS cluster resources
We have our Microsoft Entra groups, users, and Azure role assignments created. Now, you configure the AKS cluster to allow these different groups access to specific resources.
Get the cluster admin credentials using the
az aks get-credentialscommand. In one of the following sections, you get the regular user cluster credentials to see the Microsoft Entra authentication flow in action.az aks get-credentials --resource-group myResourceGroup --name myAKSCluster --adminCreate a namespace in the AKS cluster using the
kubectl create namespacecommand. The following example creates a namespace name dev:kubectl create namespace devNote
In Kubernetes, Roles define the permissions to grant, and RoleBindings apply them to desired users or groups. These assignments can be applied to a given namespace, or across the entire cluster. For more information, see Using Kubernetes RBAC authorization.
If the user you grant the Kubernetes RBAC binding for is in the same Microsoft Entra tenant, assign permissions based on the UPN. If the user is in a different Microsoft Entra tenant, query for and use the objectId property instead.
Create a Role for the dev namespace, which grants full permissions to the namespace. In production environments, you can specify more granular permissions for different users or groups. Create a file named
role-dev-namespace.yamland paste the following YAML manifest:kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: name: dev-user-full-access namespace: dev rules: - apiGroups: ["", "extensions", "apps"] resources: ["*"] verbs: ["*"] - apiGroups: ["batch"] resources: - jobs - cronjobs verbs: ["*"]Create the Role using the
kubectl applycommand and specify the filename of your YAML manifest.kubectl apply -f role-dev-namespace.yamlGet the resource ID for the appdev group using the
az ad group showcommand. This group is set as the subject of a RoleBinding in the next step.az ad group show --group appdev --query id -o tsvCreate a RoleBinding for the appdev group to use the previously created Role for namespace access. Create a file named
rolebinding-dev-namespace.yamland paste the following YAML manifest. On the last line, replace groupObjectId with the group object ID output from the previous command.kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: dev-user-access namespace: dev roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: dev-user-full-access subjects: - kind: Group namespace: dev name: groupObjectIdTip
If you want to create the RoleBinding for a single user, specify kind: User and replace groupObjectId with the UPN in the previous sample.
Create the RoleBinding using the
kubectl applycommand and specify the filename of your YAML manifest:kubectl apply -f rolebinding-dev-namespace.yaml
Access AKS cluster resources with Microsoft Entra identities
Now, test that the expected permissions work when you create and manage resources in an AKS cluster. In these examples, you schedule and view pods in the user's assigned namespace, and try to schedule and view pods outside of the assigned namespace.
Reset the kubeconfig context using the
az aks get-credentialscommand. In a previous section, you set the context using the cluster admin credentials. The admin user bypasses Microsoft Entra sign-in prompts. Without the--adminparameter, the user context is applied that requires all requests to be authenticated using Microsoft Entra ID.az aks get-credentials --resource-group myResourceGroup --name myAKSCluster --overwrite-existingSchedule a basic NGINX pod using the
kubectl runcommand in the dev namespace:kubectl run nginx-dev --image=mcr.azk8s.cn/oss/nginx/nginx:1.15.5-alpine --namespace devEnter the credentials for the appdev group account (enter your own credentials) at the sign-in prompt. Once you're successfully signed in, the account token is cached for future
kubectlcommands. The NGINX is successfully scheduled as shown in the following example output:$ kubectl run nginx-dev --image=mcr.azk8s.cn/oss/nginx/nginx:1.15.5-alpine --namespace dev To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code B24ZD6FP8 to authenticate. pod/nginx-dev createdUse the
kubectl get podscommand to view pods in the dev namespace:kubectl get pods --namespace devEnsure the status of the NGINX pod is Running. The output looks like the following output:
$ kubectl get pods --namespace dev NAME READY STATUS RESTARTS AGE nginx-dev 1/1 Running 0 4m
Test SRE access to AKS cluster resources
To confirm that our Microsoft Entra group membership and Kubernetes RBAC work correctly between different users and groups, try the previous commands when signed in as the akssre user.
Reset the kubeconfig context using the
az aks get-credentialscommand that clears the previously cached authentication token for the aksdev user.az aks get-credentials --resource-group myResourceGroup --name myAKSCluster --overwrite-existingSchedule and view pods in the assigned SRE namespace. When prompted, sign in with the opssre group account credentials (enter your own credentials).
kubectl run nginx-sre --image=mcr.azk8s.cn/oss/nginx/nginx:1.15.5-alpine --namespace sre kubectl get pods --namespace sreAs shown in the following example output, you can successfully create and view the pods:
$ kubectl run nginx-sre --image=mcr.azk8s.cn/oss/nginx/nginx:1.15.5-alpine --namespace sreTo sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code BM4RHP3FD to authenticate.
pod/nginx-sre created $ kubectl get pods --namespace sre NAME READY STATUS RESTARTS AGE nginx-sre 1/1 Running 0Try to view or schedule pods outside of assigned SRE namespace.
kubectl get pods --all-namespaces kubectl run nginx-sre --image=mcr.azk8s.cn/oss/nginx/nginx:1.15.5-alpine --namespace devThese
kubectlcommands fail, as shown in the following example output. The user's group membership and Kubernetes Role and RoleBindings don't grant permissions to create or manager resources in other namespaces.$ kubectl get pods --all-namespaces Error from server (Forbidden): pods is forbidden: User "akssre@contoso.com" cannot list pods at the cluster scope $ kubectl run nginx-sre --image=mcr.azk8s.cn/oss/nginx/nginx:1.15.5-alpine --namespace dev Error from server (Forbidden): pods is forbidden: User "akssre@contoso.com" cannot create pods in the namespace "dev"
Create and view cluster resources outside of the assigned namespace
To view pods outside of the dev namespace. Use the kubectl get pods command using --all-namespaces:
kubectl get pods --all-namespaces
The user's group membership doesn't have a Kubernetes Role that allows this action, as shown in the following example output:
Error from server (Forbidden): pods is forbidden: User "aksdev@contoso.com" cannot list resource "pods" in API group "" at the cluster scope
In the same way, schedule a pod in a different namespace, such as the SRE namespace. The user's group membership doesn't align with a Kubernetes Role and RoleBinding to grant these permissions, as shown in the following example output:
$ kubectl run nginx-dev --image=mcr.azk8s.cn/oss/nginx/nginx:1.15.5-alpine --namespace sre
Error from server (Forbidden): pods is forbidden: User "akssre@contoso.com" cannot create resource "pods" in API group "" in the namespace "sre"
Clean up cluster resources
To clean up all of the resources, run the following commands:
# Get the admin kubeconfig context to delete the necessary cluster resources.
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster --admin
# Delete the dev and SRE namespaces. This also deletes the pods, Roles, and RoleBindings.
kubectl delete namespace dev
kubectl delete namespace sre
# Delete the Azure AD user accounts for aksdev and akssre.
az ad user delete --upn-or-object-id $AKSDEV_ID
az ad user delete --upn-or-object-id $AKSSRE_ID
# Delete the Azure AD groups for appdev and opssre. This also deletes the Azure role assignments.
az ad group delete --group appdev
az ad group delete --group opssre
Next steps
For more information about how to secure Kubernetes clusters, see Access and identity options for AKS.
For best practices on identity and resource control, see Best practices for authentication and authorization in AKS.