0

To preface, I am new to leveraging Kubernetes in Azure, so I might get some terms wrong. Sorry in advance.

I have been attempting to find a way to create Kubernetes secret objects that are populated with values from Azure key vault (AKV) in Azure Kubernetes services (AKS). The goal is to be able to create an imagePullSecret using values populated from AKV to allow my cluster to pull *images* from an Azure container registry (ACR). I have tested this out with manual creation and got it working, but am open to other solutions to integrating AKS and ACR.

I have found several articles pointing out that I can manage secrets from AKV by using the API secrets-store.csi.x-k8s.io/v1. I attempted to find a way to leverage this, but these appear to be secrets that can be mounted by a pod and not leveraged by the AKS management layer.

Whenever I attempt to search for a way to generate Kubernetes imagePullSecrets, I always find either the API above or manual secret creation. At this point, I am starting to think it is not possible create the imagePullSecrets with a Kubernetes manifest file. If it is possible, a link to documentation would be appreciated. If the mentioned API can achieve the desired goal, I might need help understanding how to leverage it for this task.

TLDR: Can I create a Kubernetes secret object without the need for pods in AKS using AKV?
Alternatively, Is there another integration solution for AKS and ACR that would avoid the need to manual linkage creation?

Edit: Changed secret to image

5w3rv0
  • 7
  • 5
  • You can use the kubelet msi and give it AcrPull role on you registry. That way you don't need any pull secrets. – The Fool Jan 02 '23 at 13:36
  • Thanks for the feedback @TheFool, I think this is similar to the selected answer in that it leverages an identity attached to the AKS cluster. Had this been posted as an answer and perhaps included the ACR AKS integration stuff as well, I would have selected it most likely. I would up vote your comment, but I do not think I am able to yet. – 5w3rv0 Jan 04 '23 at 20:33

3 Answers3

0

Let's clarify your objective...
You want to pull images from ACR and in order to do so you need to set up imagePullSecrets in your pods/deployments. I am assuming you want to limit access to the ACR to specific namespaces in your cluster. If you want it cluster wide - then you may just go with direct integration setup with AKS --> ACS

One would not pull secrets from ACR as you indicated in your question, they would pull the secret from Azure Key Vault using the Azure version of the secrets store connector you listed above (https://learn.microsoft.com/en-us/azure/aks/csi-secrets-store-driver) - this will set up secrets in your cluster namespace the pods can use for imagePullSecrets to pull container images from ACR.

In short:

  1. set up Azure CSI
  2. use Azure CSI to create secret holding your pull secrets for your ACS. The creation happens on pod deployment
  3. when defining pod spec - set the imagePullSecrets secret name to the one you create in step 2

I would do it this way.

  1. Reconfigure your cluster with the Azure Key Vault provider - you can do this when you create your cluster so you don't have to go through the effort of adding it manually. If you are doing it through the portal it's under the Advanced Tab in the cluster setup pictured below...

enter image description here

or if you are setting up your cluster in terraform like I do then set up the key_vault_secrets_provider section in the resource.

  1. Read the instructions in the link above on how to set up secret pulling from AKV. you will need to mount a volume to your pods to create them. If you haven't done that before it's pretty easy to get the hang of. Once you do, every time you deploy a pod, a kubernetes secret will be created with the value of the secret pulled from AKV.

  2. When you need to assign the image pull secret - you use the secret you created via the mounted volume

your deployment yaml may look something like this..

apiVersion: apps/v1
kind: Deployment
metadata:
  name: <your deployment name> 
  namespace: <namespace name>
  <more meta data stuff here>
spec:
  <some deployment spec stuff here>
  template:
    metadata:
      <some common metadata for your pods in this deployment>
    spec:
      imagePullSecrets:
        - name: <your secret name that holds the value from AKV here>
      containers:
        - name: <your container name>
          image: <ACR_registry_name_here.repo_name:version>
          volumeMounts:
          - mountPath: /mnt/<image-pull-secret-mount-for-csi here>
            name: <whatever name you want>
            readOnly: true
        <some other container stuff here>
      volumes:
      - name: <whatever name you want - must be same as above volume mount> 
        csi:
          driver: secrets-store.csi.k8s.io
          readOnly: true
          volumeAttributes:
            secretProviderClass: <the name of your secretProviderClass resource - see below>  

---
apiVersion: secrets-store.csi.x-k8s.io/v1
kind: SecretProviderClass
metadata:
  name: <your secretProviderClass name>
  namespace: <namespace name>
spec:
  parameters:
    keyvaultName: <the name of your Azure Key Vault>
    objects: <read instructions in the link on how to create these>
    resourceGroup: <the azure resource group name of your key vault>
    subscriptionId: <your Azure Subscription GUID>
    tenantId: <your Azure tenant GUID>
    usePodIdentity: "false"
    useVMManagedIdentity: "true"
    userAssignedIdentityID: <the managed identity id for your core cluster virtual machines - look in the resource group that is automatically created for your cluster VMs when you create your cluster>
  provider: azure
  secretObjects:
  - <this depends upon the secret type you want - again - read instructions in the link I provided>




there are other resources out there i found that this guy's videos are helpful https://www.youtube.com/watch?v=S2wzCD5L9Mo&ab_channel=HoussemDellai

good luck - I hope this helps

williamohara
  • 57
  • 1
  • 8
  • Thanks for the answer, and sorry that my post had a small type that caused some confusion. In the end, I did not select this answer because while it did clarify how to use an API to create secrets, I did mention that I had that API set up on the cluster and my issues with using it. This answer did not overall address the question in part due to my typo. – 5w3rv0 Jan 04 '23 at 20:36
0

No, the secret CSI always need a pod to create Kubernetes secrets.

Yes, you can easily attach your ACR to your AKS leverage the Kubelet identity to pull images from your ACR. You just need to export the Identity and add a Role Assignment on your ACR (for an existing ACR and AKS):

export KUBE_ID=$(az aks show -g <resource group> -n <aks cluster name> --query identityProfile.kubeletidentity.objectId -o tsv)
export ACR_ID=$(az acr show -g <resource group> -n <acr name> --query id -o tsv)
az role assignment create --assignee $KUBE_ID --role "AcrPull" --scope $ACR_ID

For a new AKS simply run :

MYACR=myContainerRegistry
az acr create -n $MYACR -g myContainerRegistryResourceGroup --sku basic
az aks create -n myAKSCluster -g myResourceGroup --generate-ssh-keys --attach-acr $MYACR

This can be also done with IaC like Bicep or Terraform (just create the Role Assignment on the Kubelet Identity)

Here you can also find the documentation.

Philip Welz
  • 2,449
  • 5
  • 12
0

If your objective is to securely pull an image from a ACR on AKS Cluster

imo, the cleanest would be to grant AcrPull (https://learn.microsoft.com/en-us/azure/container-registry/container-registry-roles?tabs=azure-cli) to your AKS NodePool identity ( this can be done at the point of deployment of the cluster, or grant it later using CLI or the portal)

but as an alternative if you want to read some service principal secret from the Azure key vault and directly want it to be mapped to a key-vault secret --> kubernetes secret , you can use external-secrets.io (https://external-secrets.io/v0.7.0/provider/azure-key-vault/)

  • Thank you for this answer. If I could accept two answers, yours would have been selected as well. The only reason I chose the other answer over this one was that the chosen answer contained more specifics on the integration solution you eluded to. I appreciated the additional API solution, but as it was not at a v1.0+ release, I did not think it appropriate for a business solution. I would up vote your answer as well, but when I click on the up vote, I get a message stating I have not met the prerequisites to do so. – 5w3rv0 Jan 04 '23 at 20:46