Skip to main content
How does your Kubernetes cluster work with HashiCorp Vault? Imagine Alan runs a payments service in Kubernetes. Every time a customer pays, the service writes that payment to a PostgreSQL database. Postgres requires a username and password to accept a connection. Alan could store that password in a Kubernetes Secret, but Kubernetes Secrets are only Base64 encoded — encoding is not encryption. Anyone with permission to read the Secret can decode the password. Instead, Alan centralizes credential management in HashiCorp Vault. This raises a question: if the payments service needs a password to access Postgres, does it now need another password to access Vault? Not necessarily. Vault can use the pod’s Kubernetes identity to authenticate. Follow one payments pod to see how this works.
  1. Kubernetes assigns the pod a Kubernetes ServiceAccount named payments-service.
  2. When the pod starts, Kubernetes mounts a service account token (a JWT) inside the pod. This token proves the pod’s Kubernetes identity.
  3. Alan could modify his application code to authenticate with Vault and fetch the password directly, for example:
However, Alan prefers not to add Vault authentication logic into the payments app. Instead he runs Vault Agent as a sidecar container inside the same pod. The Vault Agent authenticates with Vault and fetches secrets on behalf of the application. How the Vault Agent flow works (high-level):
  • When Vault Agent starts, it reads the Kubernetes service account token from the pod filesystem and sends that token to Vault together with a configured Vault role name.
  • Vault validates the Kubernetes token using Kubernetes’ TokenReview API instead of blindly trusting it. Vault calls the API server to confirm the token and retrieve the service account and namespace associated with it.
  • After validation, Vault checks the configured Vault role that maps Kubernetes service accounts and namespaces to Vault policies. The role determines which policies will be applied to the resulting Vault token.
  • If the service account and role match, Vault issues a short-lived Vault token to the agent. This Vault token is separate from the Kubernetes JWT: the Kubernetes token proves the pod’s identity, while the Vault token defines which secrets and operations the agent can perform.
  • The agent uses the Vault token to request the Postgres credential. Vault validates the request against the policy and returns the credential. The agent writes the secret into a file on a shared volume mounted into the pod (for example, an emptyDir volume). The payments application reads that file and uses the username and password to connect to Postgres.
Here is a simple Vault policy example that grants read access to the Postgres secret:
A stylized architecture diagram showing a Kubernetes payments pod integrated with HashiCorp Vault and Postgres, featuring a Vault Agent, shared volume, app, and a payments-service. It also shows an API server, user (Alan), and the secret/connection flows between Vault, Postgres, and the pod.
Key benefits of this approach
  • The application itself does not need to implement Vault authentication; Vault Agent or other integrations handle it.
  • Vault can generate dynamic, short-lived PostgreSQL credentials instead of returning long-lived static passwords, improving security and rotation.
  • Centralized secrets management reduces credential sprawl and improves auditability.
Scaling this pattern If Alan has 100 applications, he can standardize the pattern:
  • Give each application its own Kubernetes ServiceAccount.
  • Create Vault roles that bind specific ServiceAccount names and namespaces to Vault policies.
  • Run Vault Agent as a sidecar (or use projected service account tokens and external injectors) to deliver approved secrets to each application.
This model maps responsibilities clearly: References
Vault validates Kubernetes tokens using the TokenReview API, then issues short-lived Vault tokens mapped to policies. This separation between Kubernetes identity (who the pod is) and Vault token (what the pod is allowed to do) is essential for least-privilege secret access and secure credential lifecycle.

Watch Video