<p>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.</p>
<p>How can I inject the secret from my Vault to environment variable of sonarqube pod running on Kubernetes?</p>
<p>Creating a Kubernetes secret and using the secret in the helm chart works, but we are managing all secrets on Vault and need Vault secrets to be injected into pods.</p>
<p>Thanks</p>

<p>There are 2 ways to inject vault secrets into the k8s pod as ENV vars.</p>
<h1>1) Use the vault Agent Injector</h1>
<p>A template should be created that exports a Vault secret as an environment variable.</p>
<pre><code>spec:
  template:
    metadata:
      annotations:
        # Environment variable export template
        vault.hashicorp.com/agent-inject-template-config: |
          {{ with secret &quot;secret/data/web&quot; -}}
            export api_key=&quot;{{ .Data.data.payments_api_key }}&quot;
          {{- end }}
</code></pre>
<p>And the application container should source those files during startup.</p>
<pre><code>args:
  ['sh', '-c', 'source /vault/secrets/config &amp;&amp; &lt;entrypoint script&gt;']
</code></pre>
<p>Reference: <a href="https://www.vaultproject.io/docs/platform/k8s/injector/examples#environment-variable-example" rel="noreferrer">https://www.vaultproject.io/docs/platform/k8s/injector/examples#environment-variable-example</a></p>
<h1>2) Use banzaicloud bank-vault</h1>
<p>Reference: <a href="https://banzaicloud.com/blog/inject-secrets-into-pods-vault-revisited/" rel="noreferrer">https://banzaicloud.com/blog/inject-secrets-into-pods-vault-revisited/</a>.</p>
<h1>Comments:</h1>
<p>Both methods are bypassing k8s security because secrets are not stored in etcd.
In addition, pods are unaware of vault in both methods.
So any one of these can be adopted without a deep comparison.</p>
<p><strong>For vault-k8s and vault-helm users, I recommend the first method.</strong></p>
