avatarLeon Jalfon

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2878

Abstract

/<span class="hljs-number">1</span> Running <span class="hljs-number">0</span> <span class="hljs-number">3</span>m <span class="hljs-attribute">keyvault</span>-flexvolume-rcxbl <span class="hljs-number">1</span>/<span class="hljs-number">1</span> Running <span class="hljs-number">0</span> <span class="hljs-number">3</span>m <span class="hljs-attribute">keyvault</span>-flexvolume-z6jm6 <span class="hljs-number">1</span>/<span class="hljs-number">1</span> Running <span class="hljs-number">0</span> <span class="hljs-number">3</span>m</pre></div><h1 id="4c23">Configuration</h1><p id="1065">There are 4 options to configure FlexVolume: Service Principal, Pod identity, VMSS User Assigned Managed Identity or VMSS System Assigned Managed Identity (in this tutorial we will use a service principal due it’s the simplest option)</p><p id="e89c">Add your service principal credentials as a Kubernetes secret</p><div id="2ea3"><pre>kubectl create<span class="hljs-built_in"> secret </span>generic kvcreds --from-literal <span class="hljs-attribute">clientid</span>=<span class="hljs-variable">{SERVICE_PRINCIPAL_CLIENT_ID}</span> --from-literal <span class="hljs-attribute">clientsecret</span>=<span class="hljs-variable">{SERVICE_PRINCIPAL_CLIENT_SECRET}</span> <span class="hljs-attribute">--type</span>=azure/kv</pre></div><h1 id="6448">Usage</h1><p id="5151">Let’s create a pod to test our configuration:</p><div id="5cad"><pre><span class="hljs-string">cat</span> <span class="hljs-string"><<</span> <span class="hljs-string">EOF</span> <span class="hljs-string">|</span> <span class="hljs-string">kubectl</span> <span class="hljs-string">apply</span> <span class="hljs-string">-f</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">apiVersion:</span> <span class="hljs-string">v1</span> <span class="hljs-attr">kind:</span> <span class="hljs-string">Pod</span> <span class="hljs-attr">metadata:</span> <span class="hljs-attr">name:</span> <span class="hljs-string">flex-kv-test</span> <span class="hljs-attr">spec:</span> <span class="hljs-attr">containers:</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">flex-kv-test</span> <span class="hljs-attr">image:</span> <span class="hljs-string">nginx</span> <span class="hljs-attr">volumeMounts:</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">test</span> <span class="hljs-attr">mountPath:</span> <span class="hljs-string">/kvmnt</span> <span class="hljs-attr">readOnly:</span> <span class="hljs-literal">true</span> <span class="hljs-attr">volumes:</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">test</span> <span class="hljs-attr">flexVolume

Options

:</span> <span class="hljs-attr">driver:</span> <span class="hljs-string">"azure/kv"</span> <span class="hljs-attr">secretRef:</span> <span class="hljs-attr">name:</span> <span class="hljs-string">kvcreds</span> <span class="hljs-attr">options:</span> <span class="hljs-attr">usepodidentity:</span> <span class="hljs-string">"false"</span>
<span class="hljs-attr">keyvaultname:</span> <span class="hljs-string">"{KEY_VAULT_NAME}"</span> <span class="hljs-attr">keyvaultobjectnames:</span> <span class="hljs-string">{KEY_VAULT_SECRET_NAME}</span> <span class="hljs-attr">keyvaultobjecttypes:</span> <span class="hljs-string">secret</span> <span class="hljs-attr">keyvaultobjectversions:</span> <span class="hljs-string">""</span> <span class="hljs-attr">tenantid:</span> <span class="hljs-string">"{SERVICE_PRINCIPAL_TENANT_ID}"</span> <span class="hljs-string">EOF</span></pre></div><p id="6c95">Note that the flexvolume configuration receive the following options:</p><ul><li><b>usepodidentity:</b> (optional) if not provided, will default to “false”</li><li><b>keyvaultname:</b> the name of the KeyVault</li><li><b>keyvaultobjectnames:</b> list of KeyVault object types: secret, key or cert (semi-colon separated)</li><li><b>keyvaultobjecttypes: </b>list of KeyVault object types: secret, key or cert (semi-colon separated)</li><li><b>keyvaultobjectversions:</b> (optional) list of KeyVault object versions (semi-colon separated), default:latest</li><li><b>tenantid:</b> the tenant ID of the KeyVault</li></ul><p id="6be1">Ensure that you have access to keyvault from the deployed pod</p><div id="a8da"><pre>kubectl <span class="hljs-built_in">exec</span> -it flex-kv-test <span class="hljs-built_in">cat</span> /kvmnt/<span class="hljs-variable">{KEY_VAULT_SECRET_NAME}</span></pre></div><h1 id="7cb8">Cleanup</h1><p id="7b98">Delete the test pod</p><div id="07c3"><pre>kubectl <span class="hljs-keyword">delete</span> pod flex-kv-test</pre></div><p id="0b15">Delete the secret that store the service principal</p><div id="f1d8"><pre>kubectl <span class="hljs-keyword">delete</span> secret kvcreds</pre></div><p id="354c">Uninstall FlexVolume from your AKS cluster</p><div id="d939"><pre>kubectl <span class="hljs-keyword">delete</span> daemonset keyvault-flexvolume -n kv kubectl <span class="hljs-keyword">delete</span> <span class="hljs-keyword">namespace</span> kv</pre></div><p id="cf10"><a href="https://readmedium.com/integrate-azure-key-vault-with-aks-introduction-part-1-3-72fedcf1ea2a">Integrate Azure Key Vault with AKS — Introduction (Part 1/3)</a></p><p id="019d"><a href="https://readmedium.com/integrate-azure-key-vault-with-aks-using-akv2k8s-part-3-3-8643ea0a7e5">Integrate Azure Key Vault with AKS — Using “akv2k8s” (Part 3/3)</a></p></article></body>

Integrate Azure Key Vault with AKS — Using “FlexVolume” (Part 2/3)

In this 3-parts tutorial we will explain how to integrate AKS with Azure Key Vault using “FlexVolumes” and “Azure Key Vault to Kubernetes”. However, before we get down to work let’s talk a little about each approach.

In this section

With FlexVolumes Key Vault secrets, keys, and certificates become a volume accessible to pods. Once the volume is mounted, its data is available directly in the container filesystem for your application.

For more information visit the official documentation: https://github.com/Azure/kubernetes-keyvault-flexvol

What will we do in this tutorial?

  • Configure your environment (set some environment variables)
  • Install Key Vault FlexVolume in your AKS cluster
  • Configure FlexVolume (create a secret with service principal details)
  • Deploy a pod that access to a Key Vault secret
  • Cleanup

Configure your environment

Let’s configure some environment variables that will be used during the tutorial

KEY_VAULT_NAME=<your-key-vault-name>
KEY_VAULT_SECRET_NAME=<your-secret-name>
SERVICE_PRINCIPAL_CLIENT_ID=<your-service-principal-client-id>
SERVICE_PRINCIPAL_CLIENT_SECRET=<your-service-principal-secret>
SERVICE_PRINCIPAL_TENANT_ID=<your-service-principal-tenant-id>

Installation

Deploy Key Vault FlexVolume to your AKS cluster

kubectl create -f https://raw.githubusercontent.com/Azure/kubernetes-keyvault-flexvol/master/deployment/kv-flexvol-installer.yaml

To validate Key Vault FlexVolume is running as expected, run the following command:

kubectl get pods -n kv

The output should show keyvault-flexvolume pods running on each agent node:

NAME                        READY     STATUS    RESTARTS   AGE
keyvault-flexvolume-f7bx8   1/1       Running   0          3m
keyvault-flexvolume-rcxbl   1/1       Running   0          3m
keyvault-flexvolume-z6jm6   1/1       Running   0          3m

Configuration

There are 4 options to configure FlexVolume: Service Principal, Pod identity, VMSS User Assigned Managed Identity or VMSS System Assigned Managed Identity (in this tutorial we will use a service principal due it’s the simplest option)

Add your service principal credentials as a Kubernetes secret

kubectl create secret generic kvcreds --from-literal clientid=${SERVICE_PRINCIPAL_CLIENT_ID} --from-literal clientsecret=${SERVICE_PRINCIPAL_CLIENT_SECRET} --type=azure/kv

Usage

Let’s create a pod to test our configuration:

cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: flex-kv-test
spec:
  containers:
  - name: flex-kv-test
    image: nginx
    volumeMounts:
    - name: test
      mountPath: /kvmnt
      readOnly: true
  volumes:
  - name: test
    flexVolume:
      driver: "azure/kv"
      secretRef:
        name: kvcreds
      options:
        usepodidentity: "false"                         
        keyvaultname: "${KEY_VAULT_NAME}"     
        keyvaultobjectnames: ${KEY_VAULT_SECRET_NAME}
        keyvaultobjecttypes: secret
        keyvaultobjectversions: ""
        tenantid: "${SERVICE_PRINCIPAL_TENANT_ID}"
EOF

Note that the flexvolume configuration receive the following options:

  • usepodidentity: (optional) if not provided, will default to “false”
  • keyvaultname: the name of the KeyVault
  • keyvaultobjectnames: list of KeyVault object types: secret, key or cert (semi-colon separated)
  • keyvaultobjecttypes: list of KeyVault object types: secret, key or cert (semi-colon separated)
  • keyvaultobjectversions: (optional) list of KeyVault object versions (semi-colon separated), default:latest
  • tenantid: the tenant ID of the KeyVault

Ensure that you have access to keyvault from the deployed pod

kubectl exec -it flex-kv-test cat /kvmnt/${KEY_VAULT_SECRET_NAME}

Cleanup

Delete the test pod

kubectl delete pod flex-kv-test

Delete the secret that store the service principal

kubectl delete secret kvcreds

Uninstall FlexVolume from your AKS cluster

kubectl delete daemonset keyvault-flexvolume -n kv
kubectl delete namespace kv

Integrate Azure Key Vault with AKS — Introduction (Part 1/3)

Integrate Azure Key Vault with AKS — Using “akv2k8s” (Part 3/3)

DevOps
Kubernetes
Azure
Keyvault
Devsecops
Recommended from ReadMedium