An alternative method for gaining access to KeyVault secrets from applications in Azure Kubernetes Service is through the use of Dapr. Dapr will run as a sidecar in your Kubernetes cluster. Although it is lightweight and provides much simpler access, it should be noted that Dapr effectively creates additional pods in your cluster and therefore may need some considerations in edge case scenarios where pod limits are being reached. It adds up to 7 different services and 4 different pods in its own namespace. It is something to always keep in mind for keeping an eye on resource usage/limits/quotas in the AKS cluster as you expand.
Note that it might be helpful to leverage knowledge on Workload Identity and Federated Identity from my previous post that will dovetail into what I write here. Ultimately, using Dapr with AKS will add more Infrastructre complexity but simplify application code complexity.
Install Dapr on AKS
Install Dapr onto your AKS Cluster with the following Azure CLI commands:
Set Managed Identity for AKS Cluster
Create a managed identity via the Azure CLI as follows:
Note down the 'id' and the 'ClientId' shown in the response json here.
Assign your ManagedIdentity to your existing AKS Cluster as an assigned Managed Identity:
Next you will need to make sure that you Federate your Managed Identity (see here on how we did this in a previous post)
Set Permissions for Managed Identity to KeyVault
Set the necessary permissions you need for your needs
Set a Secret Store Dapr Component
The AKS Cluster will need a secret store component applied to use for the KeyVault:
Deploy the above by using:
Deploy Deployment and Service YAML
We can use the following deployment and Service YAML, this time with specific annotations for the Dapr sidecar on our deployment YAML:
Deploy the above by using:
Set Service Account YAML
You will need a Service account for the AKS cluster to use where the Managed Identity you select is Federated (this links back to my previous post that included how )
Deploy the above by using:
kubectl apply -f [serviceaccount.yaml]
Access KeyVault Secret using Dapr in AKS
You can package and deploy an app with the following example code to a Docker Container Registry to access an existing secret from an Azure Key Vault using Dapr:
Add the Dapr nuget package to your project:
Example:
When you get to the LodBalancer External IP Address:
That's it. REMEMBER TO DELETE UNUSED RESOURCES IN AZURE!!!