Azure Kubernetes Service (AKS) and Managed Identities
In this blog I will be exploring the use of Azure Manged Identities in Azure Kubernetes Service (AKS). We will then discuss how we can use managed identities according to security best practice. We will look at how we configure the managed identities for the AKS cluster so it can in turn manage other Azure resources. We will explore how we can configure managed identities for our services/applications that are running on AKS so pods can reach out to other Azure services.
Managed Identities
Why are we using managed identities? The alternative is to use Service Principal accounts (SPNs). The issues with SPNs is you have a client secret which you have to manage and keep secure. Your cluster apps and services will need to access the SPNs you have created so this means potentially saving it in a few places so it is available to CI/CD pipelines. The secret attached to an SPN rotates so you need to ensure it is valid to ensure your cluster and services continue to run.Managed identities essentially are using SPNs under the hood but they make the management simpler. Managed identities manage key rotation which occurs every 46 days. Instead of constantly having a account with a client ID and secret to access something services reach out to managed identities to request a token when they need it.
In the scenario we will talk about we will be configuring the AKS cluster to use managed identities and we will use managed identities for the services we deploy to our AKS cluster. To see how to create an AKS cluster that uses managed identities read this Microsoft guide
AKS Identities
There are a couple of identities for AKS;Control plane - Used by AKS control plane components to manage cluster resources including ingress load balancers and AKS managed public IPs, and Cluster auto-scaler operations
Kubelet - AKS Cluster name-agentpool
To view the details of these identities to extract IDs you can use the following Azure cli commands
All AKS details
az aks show -g <rg-name> -n <cluster-name>AKS Identity - control pane
az aks show -g <rg-name> -n <cluster-name> --query "identity"Kubelet Identity
az aks show -g <rg-name> -n <cluster-name> --query "identityProfile"Below are some of the common roles and scopes I have assigned to AKS clusters, notice how we limit the scope and roles to provide least privilege access;
Allow AKS to manage the Load Balance
Scope: AKS Subnet where the LB is deployed
Role: "Network Contributor"
Identity: AKS Identity
Allow AKS to access the Azure container registry
Scope: Azure Container registry
Role: "acrpull"
Identity: Kubelet Identity
Allow AKS to manage DNS records
Scope: DNS zone
Role: "DNS Zone Contributor""
Identity: Kubelet Identity
Ideally you should be including this config in your Infrastructure as Code (IaC) configuration. An example of how I would do this in Terraform for the two different identity type
AAD Pod identity
The next challenge is how do the Pods you are running in AKS reach out to other Azure services. The answer to this is AAD Pod Identity. AAD Pod identity is a service that you run on your AKS cluster which provides a way for pods to access Azure resources using Azure Active Directory and the managed identities we configure for our roles. Review the identity best practices here.
You have a resource group for the AKS service and one for the resources like the Virtual Machine Scale set (VMSS) that hosts the AKS node pool. The resource group below relates to the resource group containing the AKS VMSS.
The AAD Pod Identity role assignment wiki provides the roles we need to add to allow AAD Pod Identity access to assign permissions for the resources in the VMSS. There are two additional roles we need to assign to the kubelet identity during the initial AAD Pod identity setup. Again, you can add this to your AKS IaC config so it is in place .
Scope: AKS RG containing VMSS
Role: "Managed Identity Operator"
Identity: Kubelet Identity
Scope: AKS RG containing VMSS
Role: "Virtual Machine Contributor"
Identity: Kubelet Identity
Install AAD Pod Identity on AKS
Create Managed Identities for our application
The next task is to create managed identities for you application. To make this secure you should make new managed identities per service/application and give these the least privileges to carry out the operations they need with a limited scope.
Scopes
Roles
Storage
Service Bus
Enabling AAD Pod Identity on our Services
/subscriptions/<subscription_id>/resourcegroups/<cluster-rg>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<managed-id-name>
apiVersion: "aadpodidentity.k8s.io/v1"
kind: AzureIdentityBinding
metadata:
name: ${IDENTITY_NAME}-binding
spec:
azureIdentity: ${IDENTITY_NAME}
selector: ${IDENTITY_NAME}
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: demo
labels:
aadpodidbinding: $IDENTITY_NAME
spec:
containers:
- name: demo
image: mcr.microsoft.com/oss/azure/aad-pod-identity/demo:v1.7.1
args:
- --subscriptionid=${SUBSCRIPTION_ID}
- --clientid=${IDENTITY_CLIENT_ID}
- --resourcegroup=${IDENTITY_RESOURCE_GROUP}
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: MY_POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: MY_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
nodeSelector:
kubernetes.io/os: linux
We have covered some of the basics of managed identities, the common roles we may need to assign to the cluster. How we can use managed identities AAD pod identity to assign granular permissions to our applications running on AKS. I hope this article has provided a good overview of the role of managed identities in AKS deployments.
Comments
Post a Comment