HashiCorp Vault as a Secrets Backend for Kubernetes
Vault + Kubernetes auth + Vault Agent Injector. The setup, the failure modes during pod startup, and the patterns that beat raw Kubernetes Secrets.
Key takeaways
- Run Vault as the Kubernetes secrets backend by enabling the Kubernetes auth method, binding roles to service accounts and namespaces, and letting the Vault Agent Injector write secrets to files in the pod.
- Prefer dynamic database credentials, keep token TTLs short, and run Vault in HA, because pods cannot start while Vault is down.
On this page
To use Vault as a Kubernetes secrets backend, enable Vault's Kubernetes auth method, bind a Vault role to a service account and namespace, and annotate pods so the Vault Agent Injector fetches secrets into /vault/secrets. Pods authenticate with their service account JWT and get short-lived tokens. The cost: Vault becomes critical-path infrastructure for every pod start.
Verified against
Vault 2.2.1 (latest release on releases.hashicorp.com), vault-k8s injector 1.7.7, against the developer.hashicorp.com Kubernetes auth, Agent Injector annotations and database secrets engine docs. Checked October 2026.
Kubernetes Secrets are base64-encoded data sitting in etcd. That's encoding, not encryption, unless you have configured encryption at rest for the API server. For anything sensitive, you want a real secrets backend: encrypted at rest, encrypted in transit, audit-logged, with rotation. Vault is the most common answer for self-managed setups. This post covers what works, what bites, and the patterns that earn their place.
The mental model
Kubernetes Secrets are convenient but weak. Vault is strong but operational. The integration model:
- Vault stores the actual secret material, encrypted with its own keys, audit-logged.
- Kubernetes service accounts authenticate to Vault using a JWT (the pod's service account token).
- Vault returns a short-lived token, and through it short-lived or leased secrets.
- The pod uses the secret; when the lease nears expiry, the agent re-fetches.
The pod never holds long-lived credentials. Kubernetes Secrets aren't eliminated (they still hold values not sensitive enough to need Vault), but the high-value secrets move.
Auth method: Kubernetes JWT
Vault's Kubernetes auth method validates pod-issued JWTs against the cluster's TokenReview API. The flow:
- Pod starts; its service account token is mounted as a JWT.
- Pod calls Vault, presenting the JWT.
- Vault calls the Kubernetes
TokenReviewAPI to confirm the JWT is valid. - Vault checks the bound service account and namespace against the role.
- If allowed, Vault returns a Vault token good for the role's TTL.
The policy grants paths:
path "secret/data/payments/*" {
capabilities = ["read"]
}
The role ties the policy to a service account:
vault policy write payments-policy policies/payments-policy.hcl
vault write auth/kubernetes/role/payments \
bound_service_account_names=payments-sa \
bound_service_account_namespaces=payments \
policies=payments-policy \
ttl=15m
Now any pod running as payments-sa in the payments namespace can read secret/data/payments/* with a 15-minute token. The TTL is the key knob: shorter means a smaller blast radius if a token leaks, longer means fewer renewals and less Vault load.
One gotcha from the Vault docs: since Kubernetes 1.21, pod tokens are short-lived and bound to the pod. If you configured auth with a pod's token as token_reviewer_jwt, TokenReview calls start failing when it expires. When Vault runs in the cluster, omit token_reviewer_jwt and kubernetes_ca_cert so Vault uses its own local service account token (Vault 1.9.3 and later).
Vault Agent Injector
This is the piece that makes Vault usable from applications. Without it, every app needs Vault client code and token renewal. With it, the injector watches for annotated pods and adds a Vault Agent that handles all the Vault interaction.
# deploy/payments.yaml (pod template metadata)
metadata:
annotations:
vault.hashicorp.com/agent-inject: "true"
vault.hashicorp.com/role: "payments"
vault.hashicorp.com/agent-inject-secret-db-creds: "secret/data/payments/db"
What happens at pod startup:
- The injector mutates the pod, adding a Vault Agent init container and sidecar.
- The init container authenticates, fetches the secret, and writes it to a shared in-memory volume.
- The main container starts and reads
/vault/secrets/db-credsfrom disk. - The sidecar keeps running and renews the secret as it nears expiry.
Nothing Vault-specific lives in app code. Use agent-inject-template-db-creds to render the file as a connection string or env file instead of raw JSON.
Static secrets still gain something
Some secrets really are static: API keys for third-party services that don't support rotation. Vault's KV v2 engine still adds value. Secrets are encrypted at rest in Vault, every read is audited, access is policy-controlled, and previous versions stay retrievable.
The Vault token stays short even for static secrets. The material doesn't rotate, but access to it does. A compromised pod has at most one TTL before it must re-authenticate.
Dynamic secrets: the real win
The Vault feature most teams underuse is dynamic database credentials. The database secrets engine connects to Postgres, MySQL and others, creates a user with limited permissions per request, and returns the credentials under a lease. When the lease expires, Vault's revocation system drops the user.
A pod asks for read-only access to the analytics DB, gets vault_abc123 and a password, uses them for an hour, and they're revoked.
- No shared passwords. Each pod has unique credentials; compromise of one is compromise of one.
- Auto-revocation. A killed pod's credentials expire on their own.
- Audit. Every issuance is logged with the role that requested it.
The startup ordering problem
The biggest gotcha is pod startup ordering. The init container fetches secrets before the main container starts. If Vault is unavailable, the init container retries, then fails, and the pod never starts. A Vault outage during a deploy means you cannot roll out new versions, and a node failure means evicted pods cannot come back.
Mitigations:
- Vault HA. Run multiple replicas across zones with integrated storage. This doesn't help if the storage backend itself is down.
agent-pre-populate-only. Injects only the init container, no sidecar. The docs recommend it for Jobs and CronJobs; for long-running pods it means secrets never refresh.agent-cache-enable. Enables the agent cache, which since Vault 1.7 is persisted between the init and sidecar containers, so the sidecar doesn't need a second round-trip at start.
Treat Vault's availability with the same seriousness as the cluster control plane.
Rotation discipline
What rotates on its own and what doesn't:
- Dynamic secrets expire with their lease, automatically.
- Database static roles map one Vault role to one existing DB user, and Vault rotates its password on
rotation_period(24 hours by default) or a cron-stylerotation_schedule. - KV secrets never rotate on their own. Schedule rotation in CI, or move the secret behind an engine that can.
Rotate the root credential the database engine uses with the rotate-root endpoint, after which only Vault knows the password. Keep that user out of static roles: the docs warn that rotating it there breaks every user managed by that connection.
The failure modes we see most
Treating Vault as an optional component. It is on the critical path for every pod start. Running a single replica means one restart blocks deploys until it is back.
Long TTLs to avoid renewals. A 24-hour TTL with no renewing sidecar works until the token expires and the app breaks mid-day. Keep TTLs short and let the sidecar renew.
One role per microservice from day one. Hundreds of fine-grained roles become unmanageable. Fewer roles with carefully scoped policies are easier to audit.
No backup story. Vault's state lives in integrated storage or Consul. Take scheduled snapshots and test a restore before you need one.
When Vault isn't worth it
- Small clusters with a handful of services. A cloud secret manager plus External Secrets Operator is less to run. We compare the managed options in Vault vs Secrets Manager vs Doppler.
- Teams without operational depth. Vault is real infrastructure, not an occasional-sysadmin tool.
- Secrets that aren't actually sensitive. Internal-only keys and dev configs are fine as Kubernetes Secrets with tight RBAC and encryption at rest.
Watch Vault's request latency (slow Vault means slow pod startup), token issuance rate, failed authentications, and storage backend health.
Questions people ask
Are Kubernetes Secrets encrypted?
Not by default. Kubernetes Secrets are base64-encoded and stored in etcd, and base64 is encoding, not encryption. You can enable encryption at rest on the API server, ideally with a KMS provider, which protects etcd data and backups. You still lack per-read audit logs, leases, and dynamic credentials, which is the gap Vault fills.
What does the Vault Agent Injector do?
It is a mutating admission webhook. When a pod carries vault.hashicorp.com/agent-inject: "true", it adds a Vault Agent init container and, unless agent-pre-populate-only is set, a sidecar. The agent logs in with the pod's service account through the Kubernetes auth method, renders the requested secrets into files under /vault/secrets, and keeps them renewed.
What happens to pods if Vault goes down?
Running pods keep the secrets already written to their volume until leases expire. New pods block, because the injected init container cannot fetch secrets and the main container never starts. That stops deploys, autoscaling and rescheduling after node failures. Run Vault in HA across zones and monitor it like the control plane.
The call we'd make
Use Vault when you need dynamic credentials, per-read audit or many teams sharing one secrets platform, and run it in HA from the first day it holds a production secret. Default to the Agent Injector with short TTLs and dynamic database credentials. The caveat: if your secrets are a handful of static API keys, a managed secret manager with External Secrets Operator gets you most of the benefit for a fraction of the operational load. For the cloud-native version of the identity problem, see cross-cloud identity federation patterns.
Sources
Cite this article
Kiril Urbonas (2026). HashiCorp Vault as a Secrets Backend for Kubernetes. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/vault-as-secrets-backend-for-kubernetes
[HashiCorp Vault as a Secrets Backend for Kubernetes](https://www.devopsness.com/blog/vault-as-secrets-backend-for-kubernetes) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)
More from DevOps
Explore more articles in this category
How to Secure Your CI/CD Pipeline (DevSecOps)
Your pipeline holds the keys to production and signs off on everything you ship, so harden both the pipeline itself and the artifacts it builds.
Kubernetes Node NotReady: Diagnosis and Fix
A node flips to NotReady and pods start disappearing. Here's the order we check things in, the usual culprits, and how to recover without making it worse.
Practical Guide: Incident Response for Platform Teams
Platform teams own the systems that EVERY service depends on. An incident response playbook for when the foundation cracks.
You might have missed
Evergreen posts worth revisiting.