Secure Your Kubernetes Secrets with Vault: A Comprehensive Setup Guide
In the dynamic world of Kubernetes, managing secrets securely is a critical aspect of ensuring the confidentiality and integrity of your applications. HashiCorp's Vault is a popular choice for handling secrets in Kubernetes, providing a secure and dynamic secret management system. This guide will walk you through the process of setting up Vault for your Kubernetes cluster, ensuring your secrets are protected and easily accessible when needed.
Understanding Vault and Its Role in Kubernetes
Vault is a tool designed to securely store and manage secrets such as API keys, passwords, and certificates. In a Kubernetes environment, Vault can be used to store and distribute secrets dynamically, ensuring they remain secure and easily accessible to the applications that need them. By using Vault, you can eliminate the need to hardcode secrets into your application code or store them in plaintext configuration files.
Prerequisites
- An up-and-running Kubernetes cluster (version 1.14 or later)
- Bash or a similar shell for running commands
- Basic understanding of Kubernetes and its core concepts
- Vault version 1.6.1 or later
Setting Up Vault on Kubernetes
To set up Vault on your Kubernetes cluster, you'll first need to create a Kubernetes secret containing the Vault configuration file. This file will specify the Vault server's configuration, including its storage backend, listener settings, and secret engines.

Create a Kubernetes Secret for Vault Configuration
Create a file named vault-config.hcl with the following content, adjusting the values as needed:
storage "file" {
path = "/vault/data"
}
listener "tcp" {
address = "0.0.0.0:8200"
tls_disable = 1
}
api_addr = "https://127.0.0.1:8200"
cluster_addr = "https://127.0.0.1:8201"
Then, create a Kubernetes secret using the following command:
kubectl create secret generic vault-config --from-file=vault-config.hcl
Deploy Vault to Kubernetes
Create a file named vault-deployment.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
name: vault
spec:
replicas: 1
selector:
matchLabels:
app: vault
template:
metadata:
labels:
app: vault
spec:
containers:
- name: vault
image: vault:1.6.1
command: ["vault", "server", "-config", "/vault/config/vault-config.hcl"]
volumeMounts:
- name: vault-config
mountPath: /vault/config
- name: vault-data
mountPath: /vault/data
volumes:
- name: vault-config
secret:
secretName: vault-config
- name: vault-data
emptyDir: {}
Apply the deployment using the following command:
kubectl apply -f vault-deployment.yaml
Configuring Vault for Kubernetes
Once Vault is deployed, you'll need to configure it to work with your Kubernetes cluster. This involves enabling the Kubernetes auth method and creating a policy that allows Kubernetes to access Vault secrets.
Enable the Kubernetes Auth Method
Run the following command to enable the Kubernetes auth method and create a policy that allows Kubernetes to access secrets:

kubectl exec -it vault-789c8d9947- hj887 -n default -- vault auth enable kubernetes kubectl exec -it vault- 789c8d9947- hj887 -n default -- vault policy write vault-k8s-policy -< 789c8d9947- hj887 -n default -- vault auth enable kubernetes kubectl exec -it vault- 789c8d9947- hj887 -n default -- vault write auth/kubernetes/config kubernetes_host="https://127.0.0.1:6443" kubernetes_ca_cert=@/vault/kubernetes-ca.crt kubernetes_role=default
Replace vault-789c8d9947-hj887 with the name of your Vault pod, and ensure the Kubernetes CA certificate is mounted as a volume in the Vault pod.
Configure Kubernetes to Use Vault for Secrets
To use Vault for storing and retrieving secrets in your Kubernetes cluster, you'll need to configure the Kubernetes secret provider. Create a file named vault-secret-provider.yaml with the following content:
apiVersion: v1 kind: Secret metadata: name: vault-secret-provider namespace: kube-system data: server: "aHR0cHM6Ly9leGFtcGxlLmNvbS84MjAw" path: "secret" role: "default" ca_cert: "LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQ=="
Apply the secret using the following command:
kubectl apply -f vault-secret-provider.yaml
Using Vault Secrets in Your Kubernetes Applications
Now that Vault is set up and configured to work with your Kubernetes cluster, you can use it to store and retrieve secrets for your applications. To do this, you'll need to create a Kubernetes secret that references the Vault secret provider.
Create a Kubernetes Secret Referencing Vault
Create a file named my-secret.yaml with the following content:
apiVersion: v1 kind: Secret metadata: name: my-secret type: Opaque data: username: "YWRtaW4=" password: "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAx"
Apply the secret using the following command:
kubectl apply -f my-secret.yaml
When the secret is created, Kubernetes will automatically retrieve the secrets from Vault and store them in the secret's data field.
Conclusion
Setting up Vault for your Kubernetes cluster provides a secure and dynamic way to manage secrets in your applications. By following the steps outlined in this guide, you can easily configure Vault to work with your Kubernetes cluster and use it to store and retrieve secrets for your applications.






















