Skip to main content
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.

Table of Contents

  1. Prerequisites
  2. Vault Annotation Overview
  3. 1. Injecting the Full Secret Map
  4. 2. Rendering a Single Field with Templates
  5. 3. Injecting Multiple Secrets with Templates
  6. Pod Initialization and Containers
  7. Conclusion
  8. References

Prerequisites

  • A running Kubernetes cluster (v1.16+).
  • A Vault server with KV v2 secrets stored at crds/data/mysql.
  • An existing php Deployment 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: Below is a quick reference for the five annotations used in this demo:

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.
  1. Create patch-annotations.yaml:
  2. Apply the patch:
  3. 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.
  1. Create patch-annotations-template.yaml:
  2. Apply the patch and wait for the new Pod:
  3. Confirm the output:
    Expected:

3. Injecting Multiple Secrets with Templates

You can inject several secrets into separate files by defining multiple <name> annotations.
  1. Create patch-annotations-multi.yaml:
  2. Apply the patch:
  3. List the injected files:
    Expected:
  4. Verify each secret:

Pod Initialization and Containers

After applying annotations, inspect the Pod:
You’ll see three containers:
  1. vault-agent-init (initContainer)
  2. vault-agent (sidecar)
  3. php (your application)
These handle authentication, periodic secret renewal, and your app’s access to /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
Using Vault annotations and templates helps keep your Kubernetes workloads secure and your secrets management automated.

References

Watch Video