Mount secrets from Key Vault
In this task you'll give a pod a secret from Key Vault without putting it in YAML.
The Secrets Store CSI driver with the Azure Key Vault provider mounts secrets as files when a pod starts. The secret lives in Key Vault; the YAML only names it in a SecretProviderClass.
On AKS it's an add-on with its own managed identity: give that identity Key Vault Secrets User on the one vault it needs.
Example. SS Kaiser's database password moved out of the deployment YAML into Key Vault; the pod reads /mnt/secrets/db-password, and rotating the password needs no code change.
Costs money. This task creates resources that are charged while they exist. Delete them as soon as you finish.
Before you start
You need: An AKS cluster and a Key Vault. Time: 30 minutes.
Where to look
- az aks enable-addons
- SecretProviderClass
- pod volume
Steps
- Run
az aks enable-addons --addons azure-keyvault-secrets-provider -g <group> -n <cluster>. - Give the add-on's identity (addonProfiles.azureKeyvaultSecretsProvider.identity) Key Vault Secrets User on the vault.
- Apply a SecretProviderClass with keyvaultName, tenantId, useVMManagedIdentity true and the identity's client ID.
- Add a csi volume (driver secrets-store.csi.k8s.io) to the pod and mount it read-only.
Commands
# Connect kubectl to the cluster first
az aks get-credentials -g <group> -n <cluster>
az aks enable-addons --addons azure-keyvault-secrets-provider -g <group> -n <cluster>
az aks show -g <group> -n <cluster> --query addonProfiles.azureKeyvaultSecretsProvider.identity
kubectl get secretproviderclass -A
Watch out for
- Cost
- The add-on is free; Key Vault charges per 10,000 operations (pennies).
- Security
- Scope the role to one vault. Treat any password that was ever in YAML as leaked and rotate it.
- Common mistakes
- Using the identity's object ID where the client ID is needed, and forgetting the vault uses RBAC, not access policies.
Clean up
Delete the SecretProviderClass, disable the add-on, and remove the role assignment.
Words to know
- CSI driver
- A plug-in that mounts storage (here, secrets) into pods.
- SecretProviderClass
- Says which vault and which secrets to mount.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On