Injecting vault secrets into Kubernetes Pod Environment variable

4/15/2020

I'm trying to install Sonarqube in Kubernetes environment which needs PostgresSQL. I'm using an external Postgres instance and I have the crednetials kv secret set in Vault. SonarQube helm chart creates an Environment variable in the container which takes the username and password for Postgres.

How can I inject the secret from my Vault to environment variable of sonarqube pod running on Kubernetes?

Creating a Kubernetes secret and suing the secret in the helm chart works, but we are managing all secrets on Vault and need Vault secrets to be injected into pods.

Thanks

-- Krishna Arani
hashicorp-vault
kubernetes
kubernetes-secrets
postgresql
sonarqube

4 Answers

4/16/2020

The kubernetes documentation (https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables) has an example YAML which populates an environment variable with a secret.

apiVersion: v1
kind: Pod
metadata:
  name: secret-env-pod
spec:
  containers:
  - name: mycontainer
    image: redis
    env:
      - name: SECRET_USERNAME
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: username
      - name: SECRET_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: password
  restartPolicy: Never
-- David Parks
Source: StackOverflow

4/15/2020
-- Flash1212
Source: StackOverflow

4/16/2020

If you are facing issue in injecting secret using consul sidecar container and finding it very difficult to setup you can use this : https://github.com/DaspawnW/vault-crd

This is vault-custom resource definition which directly sync vault environment variables to kuberntes secret so now you can directly add secret to POD. with secretref.

vault crd create one pod in which you have to pass vault service name or URL using which application can connect to vault and on changes in vault value it will automatically sync value to kubernetes secret.

https://vault.koudingspawn.de/

-- Harsh Manvar
Source: StackOverflow

4/15/2020

You need to use a parent process that will talk to vault and retrieve the value, and then run your real process. https://github.com/hashicorp/envconsul is the marginally official tool for this from the Vault team, but there are many other options if you go looking.

-- coderanger
Source: StackOverflow