Integrate Workload Identity with AKS

Let Pods on AKS access Azure resources with a Managed Identity, with no passwords stored in the cluster. How Workload Identity works, how to set it up, and how to fix common problems.

Contents

1. Why Use Workload Identity

When a Pod needs to access Azure resources such as Key Vault or Storage, the old common approach was to create a Service Principal and put its Client Secret in a Kubernetes Secret. This works, but the secret expires and has to be renewed. And anyone who gets the secret can use it to sign in from anywhere.

With Workload Identity, the Pod takes a short-lived token issued by Kubernetes and trades it for an Azure token. The cluster stores no long-lived passwords. The identity the Pod gets is a Managed Identity: an identity in Entra ID whose credentials Azure manages for you. The Pod uses the Managed Identity to access resources. Workload Identity is the way the Pod gets that identity.

ApproachWhere the credential livesWatch out for
Service Principal + SecretKubernetes SecretMust be renewed; can be misused if leaked
Workload IdentityNowhereOne extra setup step: a Federated Credential

AKS nodes can also use a Managed Identity, but every Pod on that node can use it too, so you can't give Pods different permissions. Workload Identity works per Service Account, so each Pod can have its own identity and permissions.

The older AAD Pod Identity is no longer maintained. Use Workload Identity for new projects.

Azure AD was renamed Microsoft Entra ID in 2023, so this feature is now also called Microsoft Entra Workload ID. Older docs and annotations still use names like azure.workload.identity. They refer to the same thing.


2. How It Works

Before a Pod can access Azure resources, it needs a token from Microsoft Entra ID. Entra ID is the Azure service that manages accounts and permissions. Every Azure account already has it, so you don't create it.

Think of it like a company gym plan. The AKS cluster is the company, and Entra ID is the gym. First the gym sets up a company plan and signs a contract with the company. After that, an employee shows their company badge at the front desk and gets a day pass.

The process has two parts: a one-time setup by an admin, and then an automatic token exchange every time a Pod starts.

2.1 One-Time Setup

StepWhat it doesIn the gym story
Turn on OIDC Issuer and Workload IdentityMakes the cluster's tokens verifiable from outside, and installs the webhookThe company starts issuing badges that outsiders can verify, and HR hands them out
Create a Managed Identity and assign rolesCreates the Pod's identity in Entra ID and gives it permissionsThe gym sets up a company plan and decides which facilities it covers
Create a Federated CredentialTells Entra ID which Service Account in which cluster to trustThe company and the gym sign a contract
Create a Service AccountUses an annotation to say which Managed Identity it maps toThe badge says which company plan to use

The steps in section 4 follow this order.

2.2 When a Pod Starts

Pod starts
 ↓
The webhook adds environment variables and a Service Account token
(HR hands the new employee a badge)
 ↓
The app (usually through the Azure SDK) sends the token to Entra ID
(The employee shows the badge at the gym's front desk)
 ↓
Entra ID checks the Issuer and Subject in the Federated Credential
(The front desk checks the contract for this employee)
 ├─ Match: Entra ID returns an access token (a day pass)
 └─ No match: request denied
 ↓
The app uses the access token to call Azure
(The employee uses the day pass)

The Issuer is the cluster that issued the token. The Subject is the Service Account and its namespace. Entra ID only returns a token when both match. So even in the same cluster, a different Service Account can't get these permissions.


3. Before You Start

  • You're signed in to the Azure CLI with your own account, and you can create Managed Identities and assign roles.
  • Azure CLI 2.47.0 or later.
  • You have an AKS cluster, or permission to create one.

The commands below use these environment variables:

export RESOURCE_GROUP="myResourceGroup"
export CLUSTER_NAME="myAKSCluster"
export USER_ASSIGNED_IDENTITY_NAME="myIdentity"
export SERVICE_ACCOUNT_NAMESPACE="default"
export SERVICE_ACCOUNT_NAME="workload-identity-sa"

4. Setup Steps

4.1 Turn On OIDC Issuer and Workload Identity

  • Turning on the OIDC Issuer lets Entra ID verify the cluster's tokens (the company starts issuing badges that outsiders can verify).
  • Turning on Workload Identity installs a mutating webhook. It adds environment variables and a token to Pods when they're created (HR hands the badge to each new employee).

For a new cluster:

az aks create \
  --resource-group "${RESOURCE_GROUP}" \
  --name "${CLUSTER_NAME}" \
  --node-count 1 \
  --enable-oidc-issuer \
  --enable-workload-identity \
  --generate-ssh-keys

For an existing cluster:

az aks update \
  --resource-group "${RESOURCE_GROUP}" \
  --name "${CLUSTER_NAME}" \
  --enable-oidc-issuer \
  --enable-workload-identity

Then get the cluster's issuer URL. You'll need it for the Federated Credential:

export SERVICE_ACCOUNT_ISSUER="$(az aks show \
  --resource-group "${RESOURCE_GROUP}" \
  --name "${CLUSTER_NAME}" \
  --query "oidcIssuerProfile.issuerUrl" -o tsv)"

Clusters outside AKS, such as self-managed Kubernetes, don't have --enable-workload-identity. Install the webhook with Helm instead.

4.2 Create a Managed Identity and Assign Roles

Create the Pod's identity in Azure (the gym sets up a company plan):

az identity create \
  --name "${USER_ASSIGNED_IDENTITY_NAME}" \
  --resource-group "${RESOURCE_GROUP}"
 
export USER_ASSIGNED_IDENTITY_CLIENT_ID="$(az identity show \
  --name "${USER_ASSIGNED_IDENTITY_NAME}" \
  --resource-group "${RESOURCE_GROUP}" \
  --query clientId -o tsv)"
 
export USER_ASSIGNED_IDENTITY_OBJECT_ID="$(az identity show \
  --name "${USER_ASSIGNED_IDENTITY_NAME}" \
  --resource-group "${RESOURCE_GROUP}" \
  --query principalId -o tsv)"

Then assign roles based on what the Pod needs to do (which facilities the plan covers). For example, if the Pod needs this cluster's kubeconfig, give it the Azure Kubernetes Service Cluster Admin Role, limited to this cluster:

az role assignment create \
  --assignee-object-id "${USER_ASSIGNED_IDENTITY_OBJECT_ID}" \
  --assignee-principal-type ServicePrincipal \
  --role "Azure Kubernetes Service Cluster Admin Role" \
  --scope "$(az aks show \
    --resource-group "${RESOURCE_GROUP}" \
    --name "${CLUSTER_NAME}" \
    --query id -o tsv)"

Only give the access the Pod needs. If it only reads Key Vault secrets, give it Key Vault Secrets User, limited to that Key Vault.

4.3 Create a Federated Credential

Tell Entra ID that tokens from this Service Account (Subject) in this cluster (Issuer) can be traded for this Managed Identity (the company and the gym sign a contract).

You create the Service Account in the next step. Here you only need its name.

az identity federated-credential create \
  --name "kubernetes-federated-credential" \
  --identity-name "${USER_ASSIGNED_IDENTITY_NAME}" \
  --resource-group "${RESOURCE_GROUP}" \
  --issuer "${SERVICE_ACCOUNT_ISSUER}" \
  --subject "system:serviceaccount:${SERVICE_ACCOUNT_NAMESPACE}:${SERVICE_ACCOUNT_NAME}"

It can take a few seconds to take effect, so a test right away might fail.

4.4 Create a Service Account

Add an annotation to the Service Account that says which Managed Identity it maps to (the badge says which company plan to use):

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ServiceAccount
metadata:
  name: ${SERVICE_ACCOUNT_NAME}
  namespace: ${SERVICE_ACCOUNT_NAMESPACE}
  annotations:
    azure.workload.identity/client-id: ${USER_ASSIGNED_IDENTITY_CLIENT_ID}
EOF

If the Managed Identity is in a different tenant, also add an azure.workload.identity/tenant-id annotation with that tenant's ID.

4.5 Deploy a Pod and Test It

The Pod must use the Service Account from the last step and have the azure.workload.identity/use: "true" label (the employee takes the badge to get a day pass). Without the label, the webhook skips the Pod.

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: quick-start
  namespace: ${SERVICE_ACCOUNT_NAMESPACE}
  labels:
    azure.workload.identity/use: "true"
spec:
  serviceAccountName: ${SERVICE_ACCOUNT_NAME}
  containers:
    - name: azure-cli
      image: mcr.microsoft.com/azure-cli
      command: ["tail", "-f", "/dev/null"] # keep the container running so you can open a shell in it
  nodeSelector:
    kubernetes.io/os: linux
EOF

Run kubectl describe pod quick-start. You should see these four environment variables added by the webhook:

VariableValue
AZURE_CLIENT_IDThe Managed Identity's client ID
AZURE_TENANT_IDThe tenant ID
AZURE_FEDERATED_TOKEN_FILEPath to the Service Account token file
AZURE_AUTHORITY_HOSTThe Entra ID sign-in URL

These variables are only the inputs. The webhook doesn't get the token for you. The code in the Pod does: it reads the token file (the badge), sends it to Entra ID with the client ID, and gets back an access token (the day pass).

Try it once by hand with the Azure CLI. Open a shell in the Pod and run:

kubectl exec -it quick-start -- bash
 
az login --service-principal \
  --tenant "$AZURE_TENANT_ID" \
  --username "$AZURE_CLIENT_ID" \
  --federated-token "$(cat "$AZURE_FEDERATED_TOKEN_FILE")"

az login is the token exchange. If it succeeds, Entra ID accepted the badge, and everything is set up correctly.

Your own code usually lets the Azure SDK do this. Here's Python reading from Key Vault:

from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
 
client = SecretClient("https://my-vault.vault.azure.net", DefaultAzureCredential())
secret = client.get_secret("db-password")

When get_secret runs, DefaultAzureCredential reads the environment variables and the token file, gets an access token, and renews it before it expires. Your code contains no passwords at all.


5. Common Problems

ProblemUsual causeFix
No AZURE_* variables in the PodMissing azure.workload.identity/use labelAdd the label and recreate the Pod
Sign-in says no matching Federated CredentialWrong Issuer or SubjectCheck the namespace and Service Account name
Sign-in fails right after creating the Federated CredentialNot active yetWait a moment and try again
Sign-in works but access is deniedThe Managed Identity is missing a roleCheck the role and scope of the role assignment

The Subject format is system:serviceaccount:<namespace>:<name>. One typo in the namespace breaks the match, and this is the most common mistake.


Wrap-Up

With Workload Identity, the cluster doesn't store any long-lived passwords. There are several setup steps, but they mostly come down to getting three things right: which Service Account, which Managed Identity it maps to, and which roles that identity has. When something breaks, check these three first.


References