Skip to main content
This guide shows a simple PHP application that reads secrets injected into the pod filesystem by HashiCorp Vault (via Vault Agent Injector) and displays them in a minimal UI. It walks through cloning the repository, building the Docker image, deploying to Kubernetes, patching the Deployment with Vault annotations, and verifying that the app can read the injected secrets. Repository: https://github.com/sidd-harth/php-vault-example Quick start — clone, build, and prepare
Why this example
  • Demonstrates secret injection into pod filesystem using Vault Agent Injector.
  • Shows how a simple PHP app reads files mounted at predetermined paths.
  • Useful for learning pod-level secret access patterns and Vault integration with Kubernetes.
Application overview
  • The PHP app expects these files (mounted by the Vault injector) at runtime:
    • /vault/secrets/username
    • /vault/secrets/password
    • /vault/secrets/apikey
Index page (index.php)
  • The example index.php outputs a small HTML UI and reads the three files under /vault/secrets/. It suppresses warnings from file_get_contents() using @ for this demo; in production handle errors explicitly.
HTML/CSS for the page
  • The app includes a small style block for the page layout and table formatting:
Dockerfile
  • Builds the PHP/Apache container and copies the app into the web root.
Kubernetes manifest
  • The provided manifest creates a Deployment (replicas: 1), a NodePort Service, and a ServiceAccount named app. The container image referenced is php:vault (the image you built locally above).
Patch step (apply Vault Agent Injector annotations)
  • The repository includes patch-annotations-template.yaml to add Vault Agent Injector annotations to the Deployment. Apply the base manifest first, then patch the deployment with the annotations so Vault can inject the secrets.
Example commands:
Deploy and verify
  1. Apply the manifest and patch the Deployment:
  1. Confirm the pods, services, and service accounts exist:
Accessing the UI
  • The php Service is exposed as a NodePort. Use the node (VM) public IP and NodePort to open the app in your browser:
  • Behavior:
    • If Vault has not injected the three files at /vault/secrets/, the UI shows a red background with “File(s) Not Found”.
    • Once Vault Agent Injector mounts/writes the secrets into /vault/secrets/ inside the pod, the UI will switch to green and display the username, password, and API key.
Pod-level verification
  • Inspect the pod filesystem to validate whether the secret files exist:
  • If files are missing, the UI remains in the red “File(s) Not Found” state. After successful injection, the UI displays secrets (as shown in index.php).
Security notes
  • The demo uses the PHP @ operator to suppress file warnings; prefer explicit error handling in production.
  • Use htmlspecialchars() (as done here) or other sanitization to avoid HTML injection when rendering secrets in a browser. For production, avoid rendering raw secrets in UI and use secure secrets handling patterns.
Make sure the mount path used by your Vault injection configuration matches the file paths the application expects (here: /vault/secrets/username, /vault/secrets/password, /vault/secrets/apikey). Also confirm the ServiceAccount used by the Deployment has the necessary annotations and role bindings for Vault Agent Injector to work.
Next steps and references

Watch Video