In this tutorial, we’ll explore how to use HashiCorp Vault annotations and templates to inject secrets into Kubernetes Pods via the Vault Agent Injector. Annotations control both the injection process and how the Vault Agent interacts with Vault.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Table of Contents
- Prerequisites
- Vault Annotation Overview
- 1. Injecting the Full Secret Map
- 2. Rendering a Single Field with Templates
- 3. Injecting Multiple Secrets with Templates
- Pod Initialization and Containers
- Conclusion
- References
Prerequisites
- A running Kubernetes cluster (v1.16+).
- A Vault server with KV v2 secrets stored at
crds/data/mysql. - An existing
phpDeployment applied in your cluster.
When using KV v2, remember that paths include
/data/ (e.g., crds/data/mysql).Vault Annotation Overview
Vault annotations fall into two main categories:| Annotation Group | Controls |
|---|---|
| Agent annotations | Secret retrieval, templating, injection toggles |
| Vault annotations | Connection settings (address, TLS, auth role) |
| Annotation | Purpose | Default / Values |
|---|---|---|
vault.hashicorp.com/agent-inject | Enable or disable injection | "true" / "false" (default) |
vault.hashicorp.com/agent-inject-status | Update existing secrets instead of fresh injection | "update" |
vault.hashicorp.com/agent-inject-secret-<name> | Define a secret path under a unique <name> (e.g., username) | — |
vault.hashicorp.com/agent-inject-template-<name> | Provide a template for rendering the <name> secret; must match the <name> | — |
vault.hashicorp.com/role | Vault role used for agent authentication | — |
1. Injecting the Full Secret Map
By default, the Vault Agent Injector writes both the data and metadata of a KV secret into a single file.-
Create
patch-annotations.yaml: -
Apply the patch:
-
Verify the injected content:
Output:
2. Rendering a Single Field with Templates
To extract only a specific field (e.g.,username), use a templating annotation.
-
Create
patch-annotations-template.yaml: -
Apply the patch and wait for the new Pod:
-
Confirm the output:
Expected:
3. Injecting Multiple Secrets with Templates
You can inject several secrets into separate files by defining multiple<name> annotations.
-
Create
patch-annotations-multi.yaml: -
Apply the patch:
-
List the injected files:
Expected:
-
Verify each secret:
Pod Initialization and Containers
After applying annotations, inspect the Pod:- vault-agent-init (initContainer)
- vault-agent (sidecar)
- php (your application)
/vault/secrets.
Conclusion
In this demo, you learned how to:- Enable full secret map injection
- Render specific secret fields with templates
- Inject multiple secrets into separate files