Exercise Overview: This exercise focuses on setting up an Azure Kubernetes Service (AKS) cluster with Azure Active Directory (Azure AD) integration. Azure AD integration enables user authentication, role-based access control (RBAC), and enhanced security for managing AKS access.
- Azure CLI
- kubectl
- Azure AD Account
- Azure AD Group (Admins)
- Azure AD User (Normal User)
Solution
Creates an Azure Resource Group for organizing and managing resources.
az group create --location westeurope --resource-group demo-weu-rgGenerates SSH RSA keys for secure communication.
ssh-keygen -t rsaCreate an empty Azure AD group named AKS-Admin to be used for cluster administration.
az ad group create --display-name AKS-Admin --mail-nickname AKS-AdminRetrieve its object ID:
ADMIN_GROUP_ID=$(az ad group show --group "AKS-Admin" --query id -o tsv)Deploy an AKS cluster and enable Azure AD integration using the admin group object ID.
az aks create \
--location westeurope \
--subscription <Your-Subscription-ID> \
--resource-group demo-weu-rg \
--name <Your-AKS-Cluster-Name> \
--ssh-key-value $HOME/.ssh/id_rsa.pub \
--network-plugin kubenet \
--load-balancer-sku standard \
--outbound-type loadBalancer \
--node-vm-size Standard_B2s \
--node-count 1 \
--tags 'ENV=Demo' 'OWNER=Corporation Inc.' \
--enable-aad \
--aad-admin-group-object-ids $ADMIN_GROUP_IDRetrieve the kubeconfig file for AKS cluster access.
az aks get-credentials \
--resource-group demo-weu-rg \
--name <Your-AKS-Cluster-Name> --admin
Verify cluster node status.
kubectl get nodesAdd a user to the AKS-Admin group to grant admin access.
az ad group member add --group AKS-Admin --member-id <MEMBER-ID>Check membership:
az ad group member check --group AKS-Admin --member-id <MEMBER-ID>Log in as the added user and confirm access to the cluster:
kubectl get nodesUSER_ID=$(az ad user show --id <ADDRESS-EMAIL> --query objectId -o tsv)Apply the ClusterRole and ClusterRoleBinding definitions for basic read access.
clusterrole.yaml:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: readonly-role
rules:
- apiGroups: ["*"]
resources: ["*"]
verbs: ["get", "watch", "list"]clusterrolebinding.yaml:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: readonly-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: readonly-role
subjects:
- kind: User
name: username@yourtenant.onmicrosoft.com # or use objectId
apiGroup: rbac.authorization.k8s.ioApply the manifests:
kubectl apply -f files/clusterrole.yaml
kubectl apply -f files/clusterrolebinding.yamlAllow the user to obtain kubeconfig credentials.
AKS_ID=$(az aks show --resource-group demo-weu-rg --name <Your-AKS-Cluster-Name> --query id -o tsv)
az role assignment create --assignee $USER_ID --role "Azure Kubernetes Service Cluster User Role" --scope $AKS_IDAs the normal user:
az aks get-credentials --resource-group demo-weu-rg --name <Your-AKS-Cluster-Name>Try running:
kubectl get pods --all-namespaces
kubectl delete po --all -n kube-systemExpected: Limited access only (read-only for listed resources).
az group delete -n demo-weu-rg --yes --no-wait