avatarLeon Jalfon

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

3031

Abstract

et injection you need to add the label “azure-key-vault-env-injection: enabled” at the namespace level. Let’s create a new namespace called “akv2k8s-test” with the required label for our test:</p><div id="e920"><pre><span class="hljs-attribute">cat << EOF | kubectl apply -f - apiVersion</span><span class="hljs-punctuation">:</span> <span class="hljs-string">v1</span> <span class="hljs-attribute">kind</span><span class="hljs-punctuation">:</span> <span class="hljs-string">Namespace</span> <span class="hljs-attribute">metadata</span><span class="hljs-punctuation">:</span> <span class="hljs-attribute">name</span><span class="hljs-punctuation">:</span> <span class="hljs-string">akv2k8s-test</span> <span class="hljs-attribute">labels</span><span class="hljs-punctuation">:</span> <span class="hljs-attribute">azure-key-vault-env-injection</span><span class="hljs-punctuation">:</span> <span class="hljs-string">enabled</span> EOF</pre></div><p id="a599">Then, let’s create a “AzureKeyVaultSecret” CRD to sync akv2k8s with the Key Vault secret</p><div id="fb9e"><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">spv.no/v1alpha1</span> <span class="hljs-attr">kind:</span> <span class="hljs-string">AzureKeyVaultSecret</span> <span class="hljs-attr">metadata:</span> <span class="hljs-attr">name:</span> <span class="hljs-string">{KEY_VAULT_SECRET_NAME}</span> <span class="hljs-attr">namespace:</span> <span class="hljs-string">akv2k8s-test</span> <span class="hljs-attr">spec:</span> <span class="hljs-attr">vault:</span> <span class="hljs-attr">name:</span> <span class="hljs-string">{KEY_VAULT_NAME}</span> <span class="hljs-comment"># name of key vault</span> <span class="hljs-attr">object:</span> <span class="hljs-attr">name:</span> <span class="hljs-string">${KEY_VAULT_SECRET_NAME}</span> <span class="hljs-comment"># name of the akv object</span> <span class="hljs-attr">type:</span> <span class="hljs-string">secret</span> <span class="hljs-comment"># akv object type</span> <span class="hljs-string">EOF</span></pre></div><h1 id="9892">Usage</h1><p id="ed57">Now we can create a pod to test our configuration</p><div id="d8a2"><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">m

Options

etadata:</span> <span class="hljs-attr">name:</span> <span class="hljs-string">akv2k8s-test</span> <span class="hljs-attr">namespace:</span> <span class="hljs-string">akv2k8s-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">akv2k8s-env-test</span> <span class="hljs-attr">image:</span> <span class="hljs-string">spvest/akv2k8s-env-test:2.0.1</span> <span class="hljs-attr">args:</span> [<span class="hljs-string">"TEST_SECRET"</span>] <span class="hljs-attr">env:</span> <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">TEST_SECRET</span> <span class="hljs-attr">value:</span> <span class="hljs-string">"{KEY_VAULT_SECRET_NAME}@azurekeyvault"</span> <span class="hljs-comment"># ref to akvs</span> <span class="hljs-string">EOF</span></pre></div><p id="053c">Finally, see the log output from your Pod</p><div id="cf8f"><pre>kubectl logs akv2k8s-<span class="hljs-keyword">test</span> -n akv2k8s-<span class="hljs-keyword">test</span></pre></div><p id="2aee">If you try to access the environment variable using “kubectl exec” you won’t be able to see the value (only the container main process have access to it)</p><div id="6ce8"><pre>kubectl <span class="hljs-built_in">exec</span> -it flex-kv-test -n akv2k8s-test <span class="hljs-built_in">echo</span> <span class="hljs-variable">{TEST_SECRET}</span></pre></div><h1 id="88e8">Cleanup</h1><p id="dccb">Delete the test pod</p><div id="eedb"><pre>kubectl delete pod flex-kv-<span class="hljs-keyword">test</span> -n akv2k8s-<span class="hljs-keyword">test</span></pre></div><p id="328a">Delete the AzureKeyVaultSecret resource</p><div id="fbc3"><pre>kubectl <span class="hljs-keyword">delete</span> AzureKeyVaultSecret <span class="hljs-variable">${KEY_VAULT_SECRET_NAME}</span> -n akv2k8s-test</pre></div><p id="d0a9">Delete the test namespace</p><div id="8253"><pre>kubectl delete ns akv2k8s-test -n akv2k8s</pre></div><p id="b2b0">Delete the akv2k8s Controller</p><div id="7560"><pre>helm <span class="hljs-keyword">delete</span> azure-<span class="hljs-built_in">key</span>-vault-controller -n akv2k8s</pre></div><p id="f44e">Delete the akv2k8s Env-Injector</p><div id="7ede"><pre>helm <span class="hljs-keyword">delete</span> azure-<span class="hljs-built_in">key</span>-vault-env-injector -n akv2k8s</pre></div><p id="120e">Delete the dedicated namespace for akv2k8s</p><div id="176c"><pre>kubectl <span class="hljs-keyword">delete</span> ns akv2k8s</pre></div><p id="9921"><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-2-3-ee1d6682bf37">Integrate Azure Key Vault with AKS — Using “FlexVolume” (Part 2/3)</a></p></article></body>

Integrate Azure Key Vault with AKS — Using “akv2k8s” (Part 3/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

Azure Key Vault to Kubernetes (akv2k8s) use two main components (Azure Key Vault Controller and Azure Key Vault Env Injector) to inject a secret, key or certificate as environment variable accessible only for the main process of the container.

For more information visit the official documentation: https://github.com/SparebankenVest/azure-key-vault-to-kubernetes

What will we do in this tutorial?

  • Configure your environment (set some environment variables)
  • Install akv2k8s in your AKS cluster
  • Configure akv2k8s (create a namespace with the required annotation and create an AzureKeyVaultSecret resource)
  • 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>

Installation

Create a dedicated namespace for akv2k8s

kubectl create ns akv2k8s

Add the helm repository (I assume you have helm already installed)

helm repo add spv-charts http://charts.spvapi.no
helm repo update

Install the Controller (and the “AzureKeyVaultSecret” CRD):

helm install azure-key-vault-controller \
  spv-charts/azure-key-vault-controller \
  --namespace akv2k8s

Install the Env-Injector:

helm install azure-key-vault-env-injector \
  spv-charts/azure-key-vault-env-injector \
  --set installCrd=false \
  --namespace akv2k8s

Configuration

To allow the secret injection you need to add the label “azure-key-vault-env-injection: enabled” at the namespace level. Let’s create a new namespace called “akv2k8s-test” with the required label for our test:

cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Namespace
metadata:
  name: akv2k8s-test
  labels:
    azure-key-vault-env-injection: enabled
EOF

Then, let’s create a “AzureKeyVaultSecret” CRD to sync akv2k8s with the Key Vault secret

cat << EOF | kubectl apply -f -
apiVersion: spv.no/v1alpha1
kind: AzureKeyVaultSecret
metadata:
  name: ${KEY_VAULT_SECRET_NAME}
  namespace: akv2k8s-test
spec:
  vault:
    name: ${KEY_VAULT_NAME}             # name of key vault
    object:
      name: ${KEY_VAULT_SECRET_NAME}    # name of the akv object
      type: secret                      # akv object type
EOF

Usage

Now we can create a pod to test our configuration

cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: akv2k8s-test
  namespace: akv2k8s-test
spec:
  containers:
  - name: akv2k8s-env-test
    image: spvest/akv2k8s-env-test:2.0.1
    args: ["TEST_SECRET"]
    env:
    - name: TEST_SECRET
      value: "${KEY_VAULT_SECRET_NAME}@azurekeyvault" # ref to akvs
EOF

Finally, see the log output from your Pod

kubectl logs akv2k8s-test -n akv2k8s-test

If you try to access the environment variable using “kubectl exec” you won’t be able to see the value (only the container main process have access to it)

kubectl exec -it flex-kv-test -n akv2k8s-test echo ${TEST_SECRET}

Cleanup

Delete the test pod

kubectl delete pod flex-kv-test -n akv2k8s-test

Delete the AzureKeyVaultSecret resource

kubectl delete AzureKeyVaultSecret ${KEY_VAULT_SECRET_NAME} -n akv2k8s-test

Delete the test namespace

kubectl delete ns akv2k8s-test -n akv2k8s

Delete the akv2k8s Controller

helm delete azure-key-vault-controller -n akv2k8s

Delete the akv2k8s Env-Injector

helm delete azure-key-vault-env-injector -n akv2k8s

Delete the dedicated namespace for akv2k8s

kubectl delete ns akv2k8s

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

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

DevOps
Kubernetes
Azure
Keyvault
Devsecops
Recommended from ReadMedium