Vault installation to Azure Kubernetes Service via Helm
Azure Kubernetes Service (AKS) can run Vault in a managed Kubernetes cluster with the Vault UI enabled for web-based secrets management.
In this tutorial, you create a cluster in AKS, install Vault with the Web UI enabled, and then configure the authentication between Vault and the cluster. Then you deploy a web application with deployment annotations so the application's secrets are installed via the Vault Agent injector service.
Prerequisites
This tutorial requires a Azure account, Azure command-line interface (CLI), Kubernetes CLI and the Helm CLI.
First, create a Azure account.
Next, install Azure CLI, kubectl CLI and helm CLI.
Next, connect the az
CLI with your Azure account.
This command launches the browser and requires that you authenticate with your Microsoft account credentials.
The Kubernetes cluster that you run needs to be created in a Azure resource group. A resource group is a container that holds related resources for an Azure solution. This resource group is created with a name and a location.
Display all the locations available to your account.
The output displays a list of all the location names. Select a name of a geographical location that serves you best.
Create a resource group named learn-vault
with the location you selected.
Example output:
The learn-vault
resource group is created in the eastus
location.
Start cluster
Kubernetes clusters may run one or more nodes. Each node contains the services to run pods. Vault running in standalone mode requires one node.
Create a cluster named learn-vault-cluster
with 1
node in the learn-vault
resource group.
The cluster is created.
High availability
Vault supports a high-availability mode. This requires a minimum of three nodes. Refer to the Vault installation to minikube via Helm with Consul tutorial for a Consul backend and Vault Installation to Google Kubernetes Engine via Helm tutorial for an integrated storage backend.
When the cluster is ready, the kubectl
CLI requires configuration to
communicate with this cluster.
Configure the kubectl
CLI to communicate to the learn-vault-cluster
cluster.
Managing multiple clusters
kubectl
enables you to manage multiple
clusters
through the context configuration. You display the available contexts kubectl config get-contexts
and set the context by name kubectl config use-context NAME
.
Display the nodes of the cluster.
The one node cluster is ready.
Install the Vault Helm chart
The recommended way to run Vault on Kubernetes is via the Helm chart.
Add the HashiCorp Helm repository.
Update all the repositories to ensure helm
is aware of the latest versions.
Search for all the Vault Helm chart versions.
The Vault Helm chart contains all the necessary components to run Vault in several different modes.
Default behavior
By default, it launches Vault in standalone mode with a file storage backend on a single pod. Enabling the Vault web UI requires that you override the defaults.
Install the latest version of the Vault Helm chart with the Web UI enabled.
The Vault pod, Vault Agent Injector pod, and Vault UI Kubernetes service are deployed in the default namespace.
Get all the pods within the default namespace.
The vault-0
pod deployed runs a Vault server and reports that it is Running
but that it is not ready (0/1
). This is because the status check defined in a
readinessProbe
returns a non-zero exit code.
The vault-agent-injector
pod deployed is a Kubernetes Mutation Webhook
Controller. The controller intercepts pod events and applies mutations to the
pod if specific annotations exist within the request.
Retrieve the status of Vault on the vault-0
pod.
The status command reports that Vault is not initialized and that it is sealed. For Vault to authenticate with Kubernetes and manage secrets requires that that is initialized and unsealed.
Initialize and unseal Vault
Vault starts uninitialized and in the sealed state. The process of initializing and unsealing Vault can be performed via the exposed Web UI.
Command-line interface
The Vault CLI on the vault-0
is able to
initialize and unseal the Vault server. For more information refer to the
Injecting Secrets into Kubernetes Pods via Vault Helm
Sidecar tutorial.
The Vault web UI is available through a Kubernetes service.
Display the vault-ui
service in the default namespace.
The EXTERNAL-IP
displays the IP address of the Vault UI. In this example
output this address is 20.62.223.255
.
Launch a web browser, and enter the EXTERNAL-IP with the port
8200
in the address. For example:http://20.62.223.255:8200
.Enter
5
in the Key shares and3
in the Key threshold text fields.Click Initialize.
When the root token and unseal key is presented, scroll down to the bottom and select Download keys. Save the generated unseal keys file to your computer.
The unseal process requires these keys and the access requires the root token.
Click Continue to Unseal to proceed.
Open the downloaded file.
Example key file:
Copy one of the
keys
(notkeys_base64
) and enter it in the Master Key Portion field. Click Unseal to proceed.The Unseal status shows
1/3 keys provided
.Enter another key and click Unseal.
The Unseal status shows
2/3 keys provided
.Enter another key and click Unseal.
After 3 out of 5 unseal keys are entered, Vault is unsealed and is ready to operate.
Copy the
root_token
and enter its value in the Token field. Click Sign In.
Set a secret in Vault
The web application that you deploy in the Deploy web
application step, expects Vault to store a username
and password at the path secret/webapp/config
. To create this secret requires
you to enable the key-value secret
engine, and store a
secret username and password at that defined path.
Select the Secrets tab in the Vault UI.
Under Secrets Engines, select Enable new engine.
Under Enable a Secrets Engine, select KV and Next.
Enter
secret
in the Path text field.Select Enable Engine.
The view changes to display all the secrets for this secrets engine.
Select the Create secret action.
Enter
devwebapp/config
in the Path for this secret.Under Version data, enter
username
in the key field andgiraffe
in the value field.Select Add to create another key and value field in Version data.
Enter
password
in the key field andsalsa
in the value field.Select Save to create the secret.
The view displays the contents of the newly created secret.
You successfully created the secret for the web application.
Configure Kubernetes authentication
The initial root token is a privileged user that can perform any operation at any path. The web application only requires the ability to read secrets defined at a single path. This application should authenticate and be granted a token with limited access.
Best practice
We recommend that root tokens are used only for initial setup of an authentication method and policies. Afterwards they should be revoked. This tutorial does not show you how to revoke the root token.
Vault provides a Kubernetes authentication method that enables clients to authenticate with a Kubernetes Service Account Token.
Select the Access tab in the Vault UI.
The view displays all the authentication methods that are enabled.
Under Authentication Methods, select Enable new method.
Under Enable an Authentication Method, select Kubernetes and Next.
The view displays the method options configuration for the authentication method.
Select Enable Method to create this authentication method with the default method options configuration.
The view displays the configuration settings that enable the auth method to communicate with the Kubernetes cluster. The Kubernetes host, CA Certificate, and Token Reviewer JWT require configuration. These values are defined on the
vault-0
pod.Enter the address returned from the following command in Kubernetes host field.
The command displays the environment variables defined on the node, filters to only the
KUBERNETES_PORT_443_TCP_ADDR
address and then prefixes thehttps://
protocol and appends the443
port.For the Kubernetes CA Certificate field, toggle the Enter as text.
Enter the certificate returned from the following command in Kubernetes CA Certificate entered as text.
Expand the Kubernetes Options section.
Enter the token returned from the following command in Token Reviewer JWT field.
The command displays the JSON Web Token (JWT) mounted by Kubernetes on the
vault-0
pod.Select Save.
The Kubernetes authentication method is configured to work within the cluster. The web application requires a Vault policy to read the secret and a Kubernetes role to grant it that policy.
Configure web application authentication
For a client of the Vault server to read the secret data defined in the Set a
secret in Vault step requires that the read
capability be granted for the path secret/data/devwebapp/config
.
Select the Policies tab in the Vault UI.
Under ACL Policies, select the Create ACL policy action.
Enter
devwebapp
in the Name field.Enter this policy in the Policy field.
Select Create policy.
The policy is created and the view displays its name and contents.
The policy is assigned to the web application through a Kubernetes role. This role also defines the Kubernetes service account and Kubernetes namespace that is allowed to authenticate.
Select the Access tab in the Vault UI.
Under Authentication Methods, click the ... for the kubernetes/ auth method. Select View configuration.
Under the kubernetes method, choose the Roles tab.
This view displays all the roles defined for this authentication method.
Select Create role.
Enter
devweb-app
in the Name field.Enter
internal-app
in the Bound service account names field.This field contains one or more Kubernetes service accounts that this role supports. This Kubernetes service account is created in the Deploy web application step.
Enter
default
in the Bound service account namespaces field and select Add.This field contains one or Kubernetes namespaces that this role supports.
Expand the Tokens section.
Enter
devwebapp
in the Generated Token's Policies.Select Save.
The role is created and the view displays its name in the roles view for this authentication method.
Deploy web application
The web application pod requires the creation of the internal-app
Kubernetes
service account specified in the Vault Kubernetes authentication role created in
the Configure Kubernetes authentication
step.
Define a Kubernetes service account named internal-app
.
Create the internal-app
service account.
Define a pod named devwebapp
with the web application.
This definition creates a pod with the specified container running with the
internal-app
Kubernetes service account. The container within the pod is
unaware of the Vault cluster. The Vault Injector service reads the
annotations
to find the secret path, stored within Vault at secret/data/devwebapp/config
and the file location, /vault/secrets/secret-credentials.txt
, to mount
that secret with the pod.
Learn more
For more information about annotations refer to the Injecting Secrets into Kubernetes Pods via Vault Agent Injector tutorial.
Create the devwebapp
pod.
Get all the pods within the default namespace.
Wait until the devwebapp
pod reports that is running and ready (2/2
).
Display the secrets written to the file /vault/secrets/secret-credentials.txt
on the devwebapp
pod.
Example output:
The result displays the unformatted secret data present on the container.
Format data
A template can be applied to structure this data to meet the needs of the application.
Clean up
Destroy the cluster.
The cluster is destroyed.
Next steps
You launched Vault in standalone mode with a Helm chart. Learn more about the Vault Helm chart by reading the documentation or exploring the project source code.
To install Vault in high-availability (HA) mode via the Helm chart, refer to the Vault Installation to Google Kubernetes Engine via Helm tutorial or the Vault Installation to Minikube via Helm with Consul tutorial for step-by-step instructions.
The pod you deployed used annotations to inject the secret into the file system. Explore how pods can retrieve secrets through the Vault Injector service via annotations, or secrets mounted on ephemeral volumes.