<p>I'm deploying a Spring Boot app in minikube that connects to a database running on the host.  Following the 12 factor app recommendations I use environment variables for the necessary configuration:</p>

<pre><code>SPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver
SPRING_DATASOURCE_PASSWORD=...
SPRING_DATASOURCE_URL=jdbc:postgresql://&lt;HOST_IP_FROM_K8S&gt;:5432/myservice
SPRING_DATASOURCE_USERNAME=...
</code></pre>

<p>The kubernetes docs only show how to set environment variables in the service and deployment .yaml files which I don't want to do.  Is there a way to pass environment variables on the command line for minikube or kubectl when I create the deployment?  (In Docker I do this with -e.)</p>

<p>Note that the environment variables have to be set before starting the app or it crashes.</p>

Dean Schulze
<p>Following Ansil's comment above I used <code>configmap</code> and <code>secret</code> to pass the configuration like this:</p>

<pre><code>kubectl create secret generic springdatasourcepassword --from-literal=SPRING_DATASOURCE_PASSWORD=postgres
kubectl create secret generic springdatasourceusername --from-literal=SPRING_DATASOURCE_USERNAME=postgres
kubectl create configmap springdatasourcedriverclassname --from-literal=SPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver
kubectl create configmap springdatasourceurl --from-literal=SPRING_DATASOURCE_URL=jdbc:postgresql://172.18.0.1:5432/bookservice
</code></pre>

<p>These are referenced in the deployment.yaml file like this:</p>

<pre><code>env:
- name: GET_HOSTS_FROM
  value: dns
- name: SPRING_DATASOURCE_DRIVER_CLASS_NAME
  valueFrom:
    configMapKeyRef:
      name: springdatasourcedriverclassname
      key: SPRING_DATASOURCE_DRIVER_CLASS_NAME
- name: SPRING_DATASOURCE_URL
  valueFrom:
    configMapKeyRef:
      name: springdatasourceurl
      key: SPRING_DATASOURCE_URL
- name: SPRING_DATASOURCE_PASSWORD
  valueFrom:
    secretKeyRef:
      name: springdatasourcepassword
      key: SPRING_DATASOURCE_PASSWORD
- name: SPRING_DATASOURCE_USERNAME
  valueFrom:
    secretKeyRef:
      name: springdatasourceusername
      key: SPRING_DATASOURCE_USERNAME
</code></pre>

<p>A full explanation can be found <a href="https://medium.com/google-cloud/kubernetes-configmaps-and-secrets-68d061f7ab5b" rel="nofollow noreferrer">here</a>.</p>
