Mastering HashiCorp Vault: Practical Examples
HashiCorp Vault is a popular open-source tool for securely managing secrets and enabling dynamic, highly-available applications. It provides a unified interface for accessing and managing secrets, such as passwords, API keys, and certificates. In this article, we'll explore HashiCorp Vault examples to help you understand and implement its core features.
Getting Started with HashiCorp Vault
Before diving into examples, ensure you have HashiCorp Vault installed and running. You can follow the official guide to set up a development environment: https://learn.hashicorp.com/vault/getting-started/install
Initializing and Unsealing Vault
After installation, initialize Vault and generate a root token for initial access:

vault init -key-shares=1 -key-threshold=1
Unseal Vault using the generated keys:
vault unseal
Storing and Retrieving Secrets
Creating a Secret Engine
First, enable the Kubernetes secret engine, which allows storing and retrieving secrets using Kubernetes namespaces:
vault secrets enable -path=secret kv-v2
Storing a Secret
Store a secret using the `vault kv put` command:

vault kv put secret/myapp database-password=mysecretpassword
Retrieving a Secret
Retrieve the stored secret:
vault kv get secret/myapp
Dynamic Secret Generation
Vault can generate dynamic secrets on demand. Let's configure the MySQL secret engine to generate dynamic database credentials:
Enabling the MySQL Secret Engine
Enable the MySQL secret engine and configure it with your database details:

vault secrets enable database
vault write database/config/myapp plugin_name=mysql connection_url="mysql://username:password@localhost/dbname"
Generating a Dynamic Secret
Generate a dynamic secret (database credentials) using the `vault write` command:
vault write database/creds/myapp
Access Control and Policies
Vault uses policies to control access to secrets. Let's create a policy allowing access to the `myapp` secret:
Creating a Policy
Create a policy file (`myapp.hcl`) with the following content:
path "secret/myapp" {
capabilities = ["read", "list"]
}
Apply the policy using the `vault policy write` command:
vault policy write myapp myapp.hcl
Assigning the Policy to a Token
Assign the policy to a token using the `vault token create` command:
vault token create -policy=myapp
Conclusion
In this article, we've explored various HashiCorp Vault examples, demonstrating how to store and retrieve secrets, generate dynamic secrets, and manage access control using policies. By mastering these core features, you'll be well-equipped to secure your applications using HashiCorp Vault.
| Resource | Description |
|---|---|
| Vault Documentation | Official HashiCorp Vault documentation |
| Vault Learning Path | Interactive learning path for HashiCorp Vault |



![How To Integrate Multiple Kubernetes Clusters to [Guide]](https://i.pinimg.com/originals/1c/e7/01/1ce701b8d41806f706a0c3a8b5a07162.png)


















