Vault-Based Secrets Management for Kubernetes
DEV Community

Vault-Based Secrets Management for Kubernetes

Project Overview

Managing application secrets securely is an important responsibility in modern cloud-native infrastructure. Applications running in Kubernetes frequently require sensitive information such as:

  • Database usernames and passwords.
  • API keys.
  • Application tokens.
  • Service credentials.
  • Third-party integration credentials.

A common but risky approach is to place these values directly inside application configuration files, container images, source code, or Kubernetes manifests.

This project demonstrates an alternative approach using HashiCorp Vault and the Vault Agent Injector to manage and deliver secrets to a Kubernetes application. The implementation uses a local Minikube cluster. Vault stores the application credentials, Kubernetes authentication establishes the application's identity, and the Vault Agent Injector makes the requested secret available inside the application pod as a file.

The project also validates secret updates, behavior during a Vault outage, recovery, and audit logging. The goal is to demonstrate how a Platform Engineer can introduce a centralized secrets-management workflow into a Kubernetes environment.

Assignment Objectives

This practical Platform Engineering assignment focuses on implementing and validating a Vault-based secrets-management solution for Kubernetes. The objectives are to:

  • Deploy HashiCorp Vault into a Kubernetes environment using Helm.
  • Configure Vault for a local development environment.
  • Enable KV v2 secret storage.
  • Configure Kubernetes authentication.
  • Create a least-privilege Vault policy.
  • Bind a Vault role to a specific Kubernetes ServiceAccount and namespace.
  • Deploy an application using the Vault Agent Injector.
  • Inject secrets into the application pod without placing credentials in its Deployment manifest.
  • Test secret rotation and file refresh.
  • Test application behavior during a Vault outage and subsequent recovery.
  • Enable and inspect Vault audit logging.
  • Evaluate Vault against other Kubernetes secrets-management approaches.

Scope: This is a local Kubernetes lab. It is not a production Vault deployment.

The Problem: Managing Application Secrets

Kubernetes provides native Secret objects, but teams still need to consider how secrets are created, stored, accessed, rotated, and audited. For example, consider an application that connects to a database. It needs a username and password to establish a connection. If these credentials are embedded in a Deployment manifest or committed to Git, anyone with access to the repository may potentially obtain them.

Even when Kubernetes Secrets are used, additional security controls are necessary. Kubernetes Secret values are base64-encoded, not automatically encrypted simply because they are stored as Secret objects.

A centralized secrets manager can provide additional capabilities, including access policies, authentication, secret versioning, audit records, and rotation workflows. This project uses HashiCorp Vault to demonstrate that approach.

Technology Stack

  • Technology Purpose Ubuntu 24.04 / WSL Local development environment
  • Docker Container runtime
  • Minikube Local Kubernetes cluster
  • Kubernetes Container orchestration
  • Helm Vault installation and configuration
  • HashiCorp Vault Centralized secrets management
  • Vault KV v2 Versioned key-value secret storage
  • Vault Kubernetes Auth Kubernetes workload authentication
  • Vault Agent Injector Automatic secret injection into pods
  • BusyBox Lightweight demonstration application
  • Bash Configuration automation
  • kubectl Kubernetes administration
  • jq JSON processing

Architecture

The architecture uses Kubernetes ServiceAccount authentication to establish workload identity. Vault then authorizes access through a role and policy.

flowchart TD A[Platform Engineer] --> B[Minikube Cluster] B --> C[Vault Server] B --> D[Vault Agent Injector] B --> E[Application Pod] C --> F[KV v2 Secret Storage] C --> G[Kubernetes Auth Method] C --> H[Vault Policies and Roles] C --> I[Audit Device] E --> J[app-sa ServiceAccount] J --> K[JWT ServiceAccount Token] K --> G G --> H H --> F D --> E E --> L[Vault Agent Sidecar] L --> M[/vault/secrets/database.txt] M --> N[Application Container]

Architecture components Vault Server Stores application secrets, manages authentication, evaluates policies, and records audit events.

Vault Agent Injector A Kubernetes admission webhook that modifies eligible pods to include Vault Agent containers and the required secret volume configuration.

Kubernetes ServiceAccount Provides the workload identity used by the application pod.

Kubernetes Authentication Allows Vault to verify the Kubernetes ServiceAccount identity and issue a Vault token with the permissions associated with the configured role.

Vault Policy Restricts the application's access to the intended secret path.

Vault Agent Sidecar Authenticates to Vault, retrieves the secret, and renders it into a file accessible to the application container.

How the Solution Works

The workflow is:

  1. Vault is deployed inside the Kubernetes cluster.
  2. KV v2 is used to store the application's database credentials.
  3. Kubernetes authentication is enabled in Vault.
  4. A Vault policy grants read access to the application's secret path.
  5. A Vault role binds that policy to app-sa in the default namespace.
  6. The application pod is created with the app-sa ServiceAccount.
  7. The Vault Agent Injector detects the pod annotations and injects the Vault Agent containers.
  8. The Vault Agent uses the pod's Kubernetes identity to authenticate with Vault.
  9. Vault evaluates the role and policy.
  10. The agent retrieves the authorized secret and renders it into /vault/secrets/database.txt.
  11. The application container can read the rendered file.

The application does not need the Vault root token, and its Deployment manifest does not contain the database password.

Project Structure

vault-kubernetes-secrets-management/ │ ├── README.md ├── LICENSE ├── .gitignore │ ├── app-deployment.yaml │ └── vault-setup/ ├── vault-values.yaml └── vault-config.sh

The project documentation is consolidated in this README so that the implementation, security strategy, testing evidence, and learning outcomes are maintained in one place.

Environment and Prerequisites

The implementation was performed using:

  • Component Environment Operating system Ubuntu 24.04.5 LTS on WSL Kubernetes Minikube Kubernetes version v1.35.1 Minikube v1.38.1 Docker CLI v29.1.3 Helm v4.3.2 kubectl v1.36.3 Vault Helm chart v0.35.0 Vault application v2.2.2

Required command-line tools:

  • Docker
  • Minikube
  • kubectl
  • Helm
  • Vault CLI
  • curl
  • jq

Check that the cluster is available:

minikube status
kubectl get nodes
kubectl get pods -A

Important: This Minikube cluster already contained other workloads. The project was deployed into a dedicated vault namespace to avoid unnecessarily changing unrelated workloads.

Implementation

Create the Vault namespace

kubectl create namespace vault

Configure Vault with Helm

The configuration is maintained in: vault-setup/vault-values.yaml

The configuration enables:

  • Vault server.
  • Vault Agent Injector.
  • Vault UI.
  • Development mode.
  • Resource requests and limits for the Vault server and injector.

Install Vault using the HashiCorp Helm repository:

helm repo add hashicorp https://helm.releases.hashicorp.com
helm repo update

Validate the chart configuration:

helm lint hashicorp/vault -f vault-setup/vault-values.yaml

Install Vault:

helm install vault hashicorp/vault \
  --namespace vault \
  --version 0.35.0 \
  -f vault-setup/vault-values.yaml

Check the deployment:

kubectl get pods -n vault
kubectl get services -n vault

The Vault server and injector should become ready.

Create the application ServiceAccount

kubectl create serviceaccount app-sa \
  --namespace default \
  --dry-run=client -o yaml | kubectl apply -f -

This ServiceAccount is the identity assigned to the demonstration application.

Configure Vault

The automation script is: vault-setup/vault-config.sh

It performs the following operations:

  • Waits for the Vault pod to become ready.
  • Opens a local port-forward to the Vault API.
  • Verifies that KV v2 is mounted at secret/.
  • Stores demonstration database credentials.
  • Creates the application read policy.
  • Enables Kubernetes authentication if it is not already enabled.
  • Configures Kubernetes authentication using the Vault pod's ServiceAccount token and Kubernetes CA certificate.
  • Creates the app role.
  • Binds the role to app-sa in the default namespace.

Run the script:

chmod +x vault-setup/vault-config.sh
./vault-setup/vault-config.sh

The demonstration credentials in this lab are dummy values. They must not be reused in a real environment.

Verify Vault configuration

Check the secret engine:

kubectl exec -n vault vault-0 -- \
  sh -c 'VAULT_ADDR=http://127.0.0.1:8200 VAULT_TOKEN=root vault secrets list'

Check the policy:

kubectl exec -n vault vault-0 -- \
  sh -c 'VAULT_ADDR=http://127.0.0.1:8200 VAULT_TOKEN=root vault policy read app-read-policy'

The application policy is:

path "secret/data/app/*" { capabilities = [ "read" ] }

This grants read access to the specified KV v2 data path. It does not grant the application permission to write or delete secrets.

Kubernetes Authentication and JWT

Kubernetes authentication is an important part of this implementation. The application does not authenticate using a hardcoded Vault token. Instead, the application pod runs with the app-sa ServiceAccount. Kubernetes provides a ServiceAccount JWT to the workload, and the Vault Agent uses that identity to authenticate against Vault.

The authentication flow is:

  1. Kubernetes assigns the application pod its ServiceAccount identity.
  2. The pod receives a ServiceAccount JWT.
  3. Vault Agent submits the JWT to Vault's Kubernetes authentication endpoint.
  4. Vault validates the identity through the Kubernetes authentication configuration.
  5. Vault checks whether the ServiceAccount and namespace match the configured role.
  6. Vault issues a token associated with the role's policy.
  7. The agent uses that token to read the permitted secret.

The configured role is: Role: app ServiceAccount: app-sa Namespace: default Policy: app-read-policy Token TTL: 1 hour

Why JWT authentication? JWT-based workload identity avoids distributing a permanent Vault token inside the application manifest. It also allows Vault to authorize workloads based on their Kubernetes identity.

Security note: The lab role does not explicitly configure a JWT audience. Audience restriction is a production-hardening improvement that should be implemented using the actual audience configured on the workload's projected ServiceAccount token.

Secret Injection into the Application

The application Deployment is maintained in: app-deployment.yaml

The Vault Agent Injector is enabled using pod annotations. The important annotations are:

  • vault.hashicorp.com/agent-inject : "true"
  • vault.hashicorp.com/role : "app"
  • vault.hashicorp.com/agent-inject-secret-database.txt : "secret/data/app/database"

The template annotation specifies how the retrieved values are written: vault.hashicorp.com/agent-inject-template-database.txt :

{{ - with secret "secret/data/app/database" - }}
USERNAME={{ .Data.data.username }}
PASSWORD={{ .Data.data.password }}
{{ - end }}

The application uses: serviceAccountName : app-sa

The injected file is: /vault/secrets/database.txt

Apply the Deployment:

kubectl apply --dry-run=server -f app-deployment.yaml
kubectl apply -f app-deployment.yaml

Wait for the rollout:

kubectl rollout status deployment/vault-demo-app

Check the pod:

kubectl get pods -l app=vault-demo-app

The pod should contain:

  • The application container.
  • The Vault Agent sidecar.
  • The Vault Agent init container, which completes initialization.

Check the injected file without printing its contents:

kubectl exec deployment/vault-demo-app -c app -- \
  sh -c 'test -s /vault/secrets/database.txt && echo "Secret file exists"'

Check that both expected keys are present without revealing their values:

kubectl exec deployment/vault-demo-app -c app -- sh -c ' grep -q "^USERNAME=" /vault/secrets/database.txt && grep -q "^PASSWORD=" /vault/secrets/database.txt && echo "Both expected keys are present" '

This validates that the application container can access the rendered secret file.

Secret Rotation Validation

Secret rotation is important because credentials should not remain unchanged indefinitely. For this test, the password stored in Vault was updated while the application pod remained running.

The test used a new dummy password:

kubectl exec -n vault vault-0 -- sh -c ' export VAULT_ADDR=http://127.0.0.1:8200 export VAULT_TOKEN=root vault kv put secret/app/database \
  username="appuser" \
  password="RotatedDatabasePassword456!" '

The test then allowed time for the Vault Agent to refresh the rendered file. The result was verified by checking the file's password value without displaying it:

kubectl exec deployment/vault-demo-app -c app -- sh -c ' grep -q "^PASSWORD=RotatedDatabasePassword456!" \
  /vault/secrets/database.txt && echo "PASS: rotated password is present" '

The pod UID was checked before and after the rotation. Observed result: The password in the rendered file changed. The application pod UID remained unchanged. The application pod remained running. No application-container restart was required for the file update.

Important distinction: file refresh versus application reload This test demonstrated

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.