How to manage Kubernetes secrets with Vault securely
Learn how to inject and dynamically rotate credentials and sensitive environment variables in Kubernetes clusters using HashiCorp Vault.
Summary
- Storing secrets in plain text inside Kubernetes exposes credentials to any process with cluster read access.
- HashiCorp Vault resolves this flaw by centralizing storage and encrypting confidential data at rest.
- Sidecar secret injection delivers credentials straight to container memory without writing them to disk.
- Dynamic rotation eliminates long-lived keys by generating short-lived temporary credentials on demand.
- Strict access control policies ensure that each application views only the secrets it strictly needs.
Why Kubernetes fails at native secret security
Managing confidential data, such as database passwords and API keys, is one of the toughest challenges when running distributed systems. Kubernetes has a native feature called Secrets that stores sensitive info for applications. In practice, this means anyone or any process with read access to the cluster can easily decode that data, since the default Base64 encoding only obscures text rather than protecting it from curious eyes.
When scaling applications in modern infrastructure, leaving static passwords scattered across source code or standard configuration files leaves systems vulnerable to leaks. This is precisely where security-focused tools step in. To understand the severity of communication failures in isolated environments, take a look at our guide on why container network traffic fails and how to fix it with iptables, which covers the risks of misconfigured networking.
The role of HashiCorp Vault in data protection
HashiCorp Vault is a centralized system built to manage, store, and control access to tokens, passwords, certificates, and encryption keys. Instead of saving sensitive data directly in source code or local files, applications request these credentials from Vault the exact moment they need them. In practice, Vault acts like a heavily fortified digital safe that requires strict authentication before releasing any information to the outside world.
Beyond safeguarding secrets with advanced encryption, Vault solves the credential rotation problem. Passwords that never change are an easy target for attackers who manage to capture them. With Vault, you can configure policies so database or external service credentials automatically expire and renew without human intervention, ensuring the system remains resilient even if an old key is compromised.
How secure sidecar secret injection works
One of the cleanest approaches to integrating Vault with Kubernetes is using a helper container, technically known as a sidecar. When an application boots up in the cluster, the Vault Agent is automatically injected right beside it in the same pod. In practice, this helper container authenticates with Vault, fetches the required credentials, and delivers them directly into shared memory or a restricted temporary file.
This strategy prevents sensitive data from being permanently written to node hard disks or exposed in environment variables visible on control panels. To ensure cascading failures do not bring down your entire infrastructure when an authentication service wobbles, applying resilience patterns is crucial—concepts similar to those we explore when analyzing how to protect microservices from cascading failures with circuit breakers in complex architectures.
Configuring dynamic credential rotation step by step
Dynamic rotation removes the need to manage static passwords for services like PostgreSQL or MySQL. Vault creates a temporary database user with specific permissions and sets a short lifespan for that credential. When the time expires, Vault automatically revokes access and generates a new key pair.
To put this strategy into practice in daily reliability engineering, follow the fundamental steps below to enable the database secret engine:
- Enable the database secrets engine on your Vault server by executing the activation command:
vault secrets enable -path=database database - Configure Vault's connection to your database instance providing administrative credentials and the connection string:
vault write database/config/my-postgresql-database plugin_name=postgresql-database-plugin connection_url="postgresql://{{username}}:{{password}}@postgres:5432/mydb?sslmode=disable" allowed_roles="my-role" - Create a mapping rule that defines which SQL commands will run to generate temporary users with restricted validity:
vault write database/roles/my-role db_name=my-postgresql-database creation_statements="CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL 'timestamp '+ interval '1 hour''; GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"{{name}}\";" default_ttl="1h" max_ttl="24h"
Ensuring resilience and continuous validation in the cluster
Implementing Vault in Kubernetes demands constant monitoring to prevent network glitches between the cluster and the vault from blocking application startup. It is recommended to configure local caching policies so that if Vault becomes temporarily unavailable, existing services can read secrets stored in memory until connectivity is restored. Additionally, infrastructure automations help ensure operational failures can be rolled back quickly, a topic we detail in our article on how to automate safe rollbacks in Kubernetes deploy failures.
In short, combining Kubernetes with HashiCorp Vault transforms an organization's security posture, replacing spreadsheets and local files with an automated credential delivery pipeline. By adopting memory-based injection and dynamic rotation, your team eliminates human error in password rotation and protects corporate assets against unauthorized access in a robust and scalable way.