Connect to k8s container
make shellAdd hashicorp repository
helm repo add hashicorp https://helm.releases.hashicorp.com
helm repo updateInstall Vault application
helm upgrade --install vault hashicorp/vault -n vault --create-namespace -f helm/vault/vault-values.yamlkubectl exec vault-0 -n vault -- vault operator init -key-shares=1 -key-threshold=1 -format=json > helm/vault/cluster-keys.jsonVault keys will be stored in the file
helm/vault/cluster-keys.json
Retrieve unseal key from helm/vault/cluster-keys.json and store it in VAULT_UNSEAL_KEY environment variable
export VAULT_UNSEAL_KEY=$(jq -r ".unseal_keys_b64[]" helm/vault/cluster-keys.json)kubectl exec vault-0 -n vault -- vault operator unseal $VAULT_UNSEAL_KEYThis step is for a replicated vault only, otherwise skip it
kubectl exec vault-0 -n vault -- vault operator unseal $VAULT_UNSEAL_KEY
kubectl exec vault-1 -n vault -- vault operator raft join http://vault-0.vault-internal:8200
kubectl exec vault-1 -n vault -- vault operator unseal $VAULT_UNSEAL_KEY
kubectl exec vault-2 -n vault -- vault operator raft join http://vault-0.vault-internal:8200
kubectl exec vault-2 -n vault -- vault operator unseal $VAULT_UNSEAL_KEY
kubectl exec vault-0 -- vault operator unseal "$VAULT_UNSEAL_KEY" -n vault
kubectl exec vault-1 -- vault operator unseal "$VAULT_UNSEAL_KEY" -n vault
kubectl exec vault-2 -- vault operator unseal "$VAULT_UNSEAL_KEY" -n vaultAfter vault unseal, you must open the connexion with vault to be able to configure it
Retrieve root token from helm/vault/cluster-keys.json and store it in VAULT_ROOT_TOKEN environment variable
export VAULT_ROOT_TOKEN=$(jq -r ".root_token" helm/vault/cluster-keys.json)Vault login
kubectl exec vault-0 -n vault -- vault login -no-print $VAULT_ROOT_TOKEN# replace PUT_YOUR_SECRET_PATH by your secret path name to create (exp: -path=mysecrets)
kubectl exec vault-0 -n vault -- vault secrets enable -path=PUT_YOUR_SECRET_PATH kv-v2kubectl exec vault-0 -n vault -- vault policy write secrets-policy - <<EOF
path "secrets/data/preprod" {
capabilities = ["list", "read"]
}
path "secrets/metadata/preprod" {
capabilities = ["list", "read"]
}
EOFIn this example
- secrets is the namespace
- preprod is the env name
- username and password are the environment variables
kubectl exec vault-0 -n vault -- \
vault kv put secrets/preprod username="static-user" password="static-password"
# Display secrets (optional)
kubectl exec vault-0 -n vault -- \
vault kv get secrets/preprodNow you can install either Vault operator or External secrets operator to sync secrets in your kubernetes cluster.
https://developer.hashicorp.com/vault/tutorials/kubernetes/vault-secrets-operator
https://developer.hashicorp.com/vault/tutorials/kubernetes/kubernetes-minikube-consul
https://www.sfeir.dev/cloud/simplifier-la-gestion-des-secrets-avec-vault-secret-operator/