avatarLeon Jalfon

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

2018

Abstract

ol">https:</span>//docs.microsoft.com/en-us/<span class="hljs-keyword">cli</span>/azure/install-azure-<span class="hljs-keyword">cli</span>?view=azure-<span class="hljs-keyword">cli</span>-latest</pre></div><ul><li>Configure your local environment (env variables to use in the next steps)</li></ul><div id="3355"><pre><span class="hljs-attr">SERVICE_PRINCIPAL_NAME</span>=aks-keyvault-tutorial <span class="hljs-attr">RESOURCE_GROUP_NAME</span>=aks-keyvault-tutorial <span class="hljs-attr">AKS_CLUSTER_NAME</span>=aks-keyvault-tutorial <span class="hljs-attr">KEY_VAULT_NAME</span>=akskeyvaulttutorial <span class="hljs-comment"># Must be globally unique</span> <span class="hljs-attr">KEY_VAULT_SECRET_NAME</span>=mySecret <span class="hljs-attr">KEY_VAULT_SECRET_VALUE</span>=myValue <span class="hljs-attr">AZURE_LOCATION</span>=westeurope</pre></div><ul><li>Create a Service Principal (store the service principal details retrieved in the output in a secure place, we will need them later)</li></ul><div id="e33f"><pre><span class="hljs-string">az</span> <span class="hljs-string">ad</span> <span class="hljs-string">sp</span> <span class="hljs-built_in">create-for-rbac</span> <span class="hljs-built_in">--name</span> {<span class="hljs-string">SERVICE_PRINCIPAL_NAME</span>}</pre></div><ul><li>Create a Resource Group</li></ul><div id="5aac"><pre>az <span class="hljs-keyword">group</span> <span class="hljs-title">create</span> --name {RESOURCE_GROUP_NAME} --<span class="hljs-keyword">location</span> <span class="hljs-title"></span>{AZURE_LOCATION}</pre></div><ul><li>Create an AKS cluster</li></ul><div id="0498"><pre>az aks create --resource-<span class="hljs-keyword">group</span> <span class="hljs-title"></span>{RESOURCE_GROUP_NAME} --name ${AKS_CLUSTER_NAME} --<span class="hljs-keyword">node</span><span class="hljs-title">-count</span> <span class="hljs-number">1</span></pre></div><ul><li>Create a Key Vault</li></ul><div id="a95f"><pre><span class="hljs-attribute">az</span> keyvault create -

Options

n <span class="hljs-variable">{KEY_VAULT_NAME}</span> -g <span class="hljs-variable">{RESOURCE_GROUP_NAME}</span></pre></div><ul><li>Create a Key Vault Secret</li></ul><div id="f470"><pre>az keyvault<span class="hljs-built_in"> secret </span><span class="hljs-built_in">set</span> --vault-name <span class="hljs-variable">{KEY_VAULT_NAME}</span> --name <span class="hljs-variable">{KEY_VAULT_SECRET_NAME}</span> --value <span class="hljs-variable">{KEY_VAULT_SECRET_VALUE}</span></pre></div><ul><li>Authorize Access to Secrets for your service principal:</li></ul><div id="ed98"><pre><span class="hljs-string">az</span> <span class="hljs-string">keyvault</span> <span class="hljs-built_in">set-policy</span> <span class="hljs-built_in">--n</span> {<span class="hljs-string">KEY_VAULT_NAME</span>} <span class="hljs-built_in">--spn</span> {<span class="hljs-string">SERVICE_PRINCIPAL_NAME</span>} <span class="hljs-built_in">--secret-permissions</span> <span class="hljs-string">get</span></pre></div><ul><li>Connect to the cluster (I assume you have kubectl already installed)</li></ul><div id="b382"><pre><span class="hljs-string">az</span> <span class="hljs-string">aks</span> <span class="hljs-built_in">get-credentials</span> <span class="hljs-built_in">--resource-group</span> {<span class="hljs-string">RESOURCE_GROUP_NAME</span>} <span class="hljs-built_in">--name</span> ${<span class="hljs-string">AKS_CLUSTER_NAME</span>}</pre></div><ul><li>Test connection</li></ul><div id="cb78"><pre>kubectl <span class="hljs-built_in">get</span> nodes</pre></div><h1 id="7d96">And that’s it, we are ready to start!</h1><p id="6fd1"><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><p id="d16b"><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 — Introduction (Part 1/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.

FlexVolumes

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.

Pros: Easy to use and configure

Cons: Anyone inside the container can see the secrets and the secrets are available only from the file system (not as environment variable)

Azure Key Vault to Kubernetes (akv2k8s)

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.

Pros: Only the main process of the container can access the secrets and it’s available as environment variable

Cons: More complicated infrastructure that requires a kubernetes CRD and a Mutating Admission Webhook

Prerequisites

For this tutorial we will need:

  • AKS Cluster (v0.0.14 or later)
  • Key Vault (including a secret)
  • Service Principal with “get” Access to Key Vault

If you don’t have the prerequisites above you can configure them following the instructions below

Environment Configuration

  • Install and configure Azure CLI (I am using version 2.1.0)
https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest
  • Configure your local environment (env variables to use in the next steps)
SERVICE_PRINCIPAL_NAME=aks-keyvault-tutorial
RESOURCE_GROUP_NAME=aks-keyvault-tutorial
AKS_CLUSTER_NAME=aks-keyvault-tutorial
KEY_VAULT_NAME=akskeyvaulttutorial   # Must be globally unique
KEY_VAULT_SECRET_NAME=mySecret
KEY_VAULT_SECRET_VALUE=myValue
AZURE_LOCATION=westeurope
  • Create a Service Principal (store the service principal details retrieved in the output in a secure place, we will need them later)
az ad sp create-for-rbac --name ${SERVICE_PRINCIPAL_NAME}
  • Create a Resource Group
az group create --name ${RESOURCE_GROUP_NAME} --location ${AZURE_LOCATION}
  • Create an AKS cluster
az aks create --resource-group ${RESOURCE_GROUP_NAME} --name ${AKS_CLUSTER_NAME} --node-count 1
  • Create a Key Vault
az keyvault create -n ${KEY_VAULT_NAME} -g ${RESOURCE_GROUP_NAME}
  • Create a Key Vault Secret
az keyvault secret set --vault-name ${KEY_VAULT_NAME} --name ${KEY_VAULT_SECRET_NAME} --value ${KEY_VAULT_SECRET_VALUE}
  • Authorize Access to Secrets for your service principal:
az keyvault set-policy --n ${KEY_VAULT_NAME} --spn ${SERVICE_PRINCIPAL_NAME} --secret-permissions get
  • Connect to the cluster (I assume you have kubectl already installed)
az aks get-credentials --resource-group ${RESOURCE_GROUP_NAME} --name ${AKS_CLUSTER_NAME}
  • Test connection
kubectl get nodes

And that’s it, we are ready to start!

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

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

Kubernetes
Azure
Keyvault
Devsecops
DevOps
Recommended from ReadMedium