In the dynamic world of container orchestration, Kubernetes has emerged as a powerful tool, offering robust security features to protect your applications. One of the key aspects of Kubernetes security is authentication, and Vault, a secrets management tool, plays a significant role in this process. This article will guide you through setting up Kubernetes authentication with Vault, ensuring a secure and efficient workflow.
Understanding Kubernetes Authentication and Vault
Before diving into the setup process, let's understand the basics of Kubernetes authentication and Vault. Kubernetes uses various authentication methods to control access to its API, including client certificates, service accounts, and token files. Vault, on the other hand, is a tool for securely managing secrets such as passwords, API keys, and certificates. By integrating Vault with Kubernetes, you can securely manage and distribute sensitive data.
Prerequisites
- Kubernetes cluster (v1.19 or later) with a running control plane and worker nodes
- Vault server (v2.6.1 or later) running in your Kubernetes cluster or elsewhere in your network
- Basic understanding of Kubernetes and Vault
Setting Up Vault Agent Injector
The Vault Agent Injector is a Kubernetes mutating webhook that injects Vault agents into your pods. This allows you to securely access Vault secrets without exposing them in your application code. Here's how to set it up:

- Deploy the Vault Agent Injector to your Kubernetes cluster using the official Helm chart:
- Configure the Vault Agent Injector to communicate with your Vault server by creating a `vault-agent-injector-config` secret:
helm install vault-agent-injector vault-agent-injector --namespace vault --create-namespace
kubectl create secret generic vault-agent-injector-config --from-literal=config.json='{"vault": {"address": "https://vault.example.com", "auth_path": "auth/kubernetes", "role": "my-role"}}' -n vault
Configuring Vault Kubernetes Authentication
Next, you need to configure Vault to recognize your Kubernetes cluster for authentication. This involves creating a Kubernetes authentication method and a role that defines which secrets can be accessed by which pods.
- Enable the Kubernetes authentication method in Vault:
- Configure the Kubernetes authentication method to communicate with your cluster's API server:
- Create a Kubernetes authentication role that maps Kubernetes service accounts to Vault policies:
vault auth enable kubernetes
vault write auth/kubernetes/config kubernetes_host="https://kubernetes.default" kubernetes_ca_cert=@/path/to/ca.crt
vault write auth/kubernetes/role/my-role bound_service_account_namespaces="default" policies="my-policy" ttl=3600
Using Vault Secrets in Your Kubernetes Pods
Now that you've set up Vault Agent Injector and configured Vault for Kubernetes authentication, you can use Vault secrets in your Kubernetes pods. Here's how:
- Create a Vault secret that your application needs:
vault kv put secret/my-app my_secret="some-value"
apiVersion: v1
kind: Pod
metadata:
name: my-pod
annotations:
vault.hashicorp.com/agent-inject: 'true'
vault.hashicorp.com/agent-inject-secret-my-secret: 'true'
vault.hashicorp.com/agent-inject-template-my-secret: |
{{- with secret "secret/my-app" -}}
my_secret="{{ .Data.data.my_secret }}"
{{- end -}}
spec:
containers:
- name: my-container
image: my-image
env:
- name: MY_SECRET
value: /vault/secrets/my-secret
Conclusion
In this article, we've explored the process of setting up Kubernetes authentication with Vault, ensuring that your sensitive data is securely managed and distributed. By following these steps, you can enhance the security of your Kubernetes cluster and applications.
























