Using HashiCorp Vault to store secrets
- Required paid resources
- Get your cloud ready
- Install HashiCorp Vault
- Log in to HashiCorp Vault
- Create a secret
- Configure the Kubernetes authentication method
- Install the SCI driver for the secret vault
- Create a SecretProviderClass resource
- Create a pod with the secret mounted
- Delete the resources you created
- See also
HashiCorp Vault
Configure secret storage and access within your Yandex Managed Service for Kubernetes cluster using HashiCorp Vault with Key Management Service support from Yandex Cloud Marketplace.
This tutorial shows how to mount a secret from HashiCorp Vault using a Container Storage Interface
To set up access to a secret in a Managed Service for Kubernetes cluster using HashiCorp Vault:
- Get your cloud ready.
- Install HashiCorp Vault.
- Log in to HashiCorp Vault.
- Create a secret.
- Configure the Kubernetes authentication method.
- Install the SCI driver for the secret vault.
- Create a SecretProviderClass resource.
- Create a pod with the secret mounted.
If you no longer need the resources you created, delete them.
Required paid resources
The support cost for this solution includes:
- Fee for using the master and outgoing traffic in a Managed Service for Kubernetes cluster (see Managed Service for Kubernetes pricing).
- Fee for using computing resources, OS, and storage in cluster nodes (VMs) (see Compute Cloud pricing).
- Fee for a public IP address assigned to cluster nodes (see Virtual Private Cloud pricing).
- Key Management Service fee for the number of active key versions (with
ActiveorScheduled For Destructionfor status) and completed cryptographic operations (see Key Management Service pricing).
Get your cloud ready
-
Create a Kubernetes cluster and node group.
ManuallyUsing Terraform-
If you do not have a network yet, create one.
-
If you do not have any subnets yet, create them in the availability zones where the new Kubernetes cluster and node group will reside.
-
Create these service accounts:
- Service account with the
k8s.clusters.agentandvpc.publicAdminroles for the folder where you want to create a Kubernetes cluster. This service account will be used to create resources for your Kubernetes cluster. - Service account with the container-registry.images.puller role. The nodes will use this account to pull the required Docker images from the registry.
Tip
You can use the same service account to manage your Kubernetes cluster and its node groups.
- Service account with the
-
Create security groups for the Managed Service for Kubernetes cluster and its node groups.
Warning
The configuration of security groups determines the performance and availability of the cluster and the services and applications running in it.
-
Create a Kubernetes cluster and node group with any suitable configuration. When creating, specify the preconfigured security groups.
-
If you do not have Terraform yet, install it.
-
Get the authentication credentials. You can add them to environment variables or specify them later in the provider configuration file.
-
Configure and initialize a provider. There is no need to create a provider configuration file manually, you can download it
. -
Place the configuration file in a separate working directory and specify the parameter values. If you did not add the authentication credentials to environment variables, specify them in the configuration file.
-
Download the k8s-cluster.tf
cluster configuration file to the same working directory. This file describes:-
Kubernetes cluster.
-
Service account for the Managed Service for Kubernetes cluster and node group.
-
Security groups which contain rules required for the Managed Service for Kubernetes cluster and its node groups.
Warning
The configuration of security groups determines the performance and availability of the cluster and the services and applications running in it.
-
Specify the following in
k8s-cluster.tf:- Folder ID.
- Kubernetes version for the Kubernetes cluster and node groups.
- Kubernetes cluster CIDR.
- Name of the Managed Service for Kubernetes cluster service account.
-
Make sure the Terraform configuration files are correct using this command:
terraform validateTerraform will show any errors found in your configuration files.
-
Create the required infrastructure:
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
All the required resources will be created in the specified folder. You can check resource availability and their settings in the management console
. -
-
-
Install kubect
and configure it to work with the new cluster.
Install HashiCorp Vault
Install HashiCorp Vault using Helm and initialize the vault by following this guide. In the installation command, specify the hcv namespace and add extra parameters to set up the Vault CSI provider
--namespace hcv \
--set "injector.enabled=false" \
--set "csi.enabled=true"
Log in to HashiCorp Vault
-
Start an interactive HashiCorp Vault shell session for the
hashicorp-vault-0pod:kubectl exec -it hashicorp-vault-0 \ --namespace hcv \ -- /bin/sh -
Unseal
the vault:vault operator unsealEnter one of the recovery keys you got during vault initialization.
-
Log in to HashiCorp Vault with the root token:
vault loginEnter the root token (
Initial Root Token) you got during vault initialization.
Create a secret
-
Enable the
kvsecrets engine at thesecretpath:vault secrets enable -path=secret kv -
Create a secret at
secret/db-passwith the password:vault kv put secret/db-pass password="12345678" -
Make sure the secret is available for reading at
secret/db-pass:vault kv get secret/db-passResult:
====== Data ====== Key Value --- ----- password 12345678
Configure the Kubernetes authentication method
This method allows authentication with a Kubernetes service account token.
-
Enable the Kubernetes authentication method:
vault auth enable kubernetes -
Configure authentication using the Kubernetes API address:
vault write auth/kubernetes/config \ kubernetes_host="https://$KUBERNETES_PORT_443_TCP_ADDR:443"The
KUBERNETES_PORT_443_TCP_ADDRenvironment variable refers to the internal network address of the Kubernetes node. -
Create a policy named
internal-appthat will allow the Kubernetes service account to read the secret you created:vault policy write internal-app - <<EOF path "secret/db-pass" { capabilities = ["read"] } EOF -
Create the
databaserole that will associate theinternal-apppolicy with the Kuberneteswebapp-saservice account (you will create it later):vault write auth/kubernetes/role/database \ bound_service_account_names=webapp-sa \ bound_service_account_namespaces=hcv \ policies=internal-app \ ttl=20mTokens returned upon authentication are valid for 20 minutes.
-
Exit HashiCorp Vault:
exit
Install the SCI driver for the secret vault
-
Add the
secrets-store-csi-driverHelm repository:helm repo add secrets-store-csi-driver https://kubernetes-sigs.github.io/secrets-store-csi-driver/charts -
Install the SCI driver:
helm install csi secrets-store-csi-driver/secrets-store-csi-driver \ --namespace=hcv \ --set syncSecret.enabled=true -
Make sure the driver is running and ready:
kubectl get pods -n hcv -l "app=secrets-store-csi-driver"Result:
NAME READY STATUS RESTARTS AGE csi-secrets-store-csi-driver-nbxcd 3/3 Running 0 4m28s
Create a SecretProviderClass resource
-
Create a file named
spc-vault-database.yamlwith settings for the CSI provider:spc-vault-database.yaml
apiVersion: secrets-store.csi.x-k8s.io/v1 kind: SecretProviderClass metadata: name: vault-database spec: provider: vault parameters: vaultAddress: "http://hashicorp-vault.hcv:8200" roleName: "database" objects: | - objectName: "db-password" secretPath: "secret/db-pass" secretKey: "password" -
Create the
SecretProviderClassresource:kubectl apply -f spc-vault-database.yaml -n hcv
Create a pod with the secret mounted
-
Create a service account named
webapp-safor the Kubernetes cluster:kubectl create serviceaccount webapp-sa \ --namespace hcv -
Create a file named
webapp-pod.yamlcontaining thewebapppod configuration:spc-vault-database.yaml
kind: Pod apiVersion: v1 metadata: name: webapp spec: serviceAccountName: webapp-sa containers: - image: jweissig/app:0.0.1 name: webapp volumeMounts: - name: secrets-store-inline mountPath: "/mnt/secrets-store" readOnly: true volumes: - name: secrets-store-inline csi: driver: secrets-store.csi.k8s.io readOnly: true volumeAttributes: secretProviderClass: "vault-database" -
Create a pod named
webapp:kubectl apply -f webapp-pod.yaml -n hcv -
Make sure the
webapppod is running and ready:kubectl get pod webapp -n hcvResult:
NAME READY STATUS RESTARTS AGE webapp 1/1 Running 0 5m25s -
Display the secret password stored at
/mnt/secrets-store/db-passwordin the file system:kubectl exec webapp -n hcv -- cat /mnt/secrets-store/db-passwordResult:
12345678
Delete the resources you created
Some resources are not free of charge. Delete the resources you no longer need to avoid paying for them.
-
In the terminal window, go to the directory containing the infrastructure plan.
Warning
Make sure the directory has no Terraform manifests with the resources you want to keep. Terraform deletes all resources that were created using the manifests in the current directory.
-
Delete resources:
-
Run this command:
terraform destroy -
Confirm deleting the resources and wait for the operation to complete.
All the resources described in the Terraform manifests will be deleted.
-