Objective
This article provides step-by-step guidance on how to inject dynamic PostgreSQL database credentials into Kubernetes applications using the Vault Agent Injector with HCP Vault Dedicated.
Prerequisites
-
An HCP Vault Dedicated cluster up and running
-
A PostgreSQL database accessible from Vault
-
A Kubernetes cluster where your applications are deployed
-
kubectl, Helm, and Vault CLI installed and configured
Refer : Prerequisites
Overview of Steps
-
Configure the Database Secrets Engine in HCP Vault
-
Enable and Configure the Kubernetes Auth Method
-
Install Vault Agent Injector in the Kubernetes Cluster
-
Inject Secrets into Your Kubernetes Applications
Step 1: Configure the Database Secrets Engine for PostgreSQL
-
-
Enable the database secrets engine (if not already enabled):
-
Configure the PostgreSQL connection:
- Define the SQL used to create credentials.
-
tee readonly.sql <<EOF
CREATE ROLE "{{name}}" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}' INHERIT;
GRANT ro TO "{{name}}";
EOF
4. Create a role to generate dynamic credentials:
Step 2: Enable and Configure Kubernetes Auth Method
-
Refer this to enable and configure kubernetes auth method.
Step 3: Install the Vault Agent Injector in Kubernetes
-
Add the HashiCorp Helm repository:
-
Install the Vault Helm chart with Injector enabled:
Step 4: Inject Secrets into Kubernetes Applications
-
Annotate your pod/deployment with Vault injection annotations. Sample given below:-
-
The Vault Agent Injector will automatically:
-
Authenticate with Vault using the Kubernetes auth method
-
Retrieve the dynamic PostgreSQL credentials
-
Inject them into the pod and these dynamic credentials gets stored in file "credentials.txt" at location
/vault/secrets
directory in vault-agent sidecar container.
-
Verification
-
Logs of the application pod should confirm secrets were injected successfully.
-
You can also exec into the pod and check the contents of the injected file:
References
https://developer.hashicorp.com/vault/tutorials/db-credentials/database-secrets