<p>I'm new at kubernetes, and Im wondering the best way to inject values to <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer">ConfigMap</a>.</p>

<p>for now, I defined Deployment object which takes the relevant values from ConfigMap file. I wish to use the same <code>.yml</code> file for my production and staging environments. so only the values in the configMap will be changed, while the file itself will be the same.</p>

<p>Is there any way to do it built-in in kubernetes, without using configuration management tools (like Ansible, puppet, etc.)?</p>

<p>You can find the links to the quoted text in the end of the answer.</p>

<blockquote>
  <p>A good practice when writing applications is to separate application code from configuration. We want to enable application authors to easily employ this pattern within Kubernetes. While the Secrets API allows separating information like credentials and keys from an application, no object existed in the past for ordinary, non-secret configuration. In Kubernetes 1.2, we’ve added a new API resource called ConfigMap to handle this type of configuration data.</p>
  
  <p>Besides, Secrets data will be stored in a base64 encoded form, which is also suitable for binary data such as keys, whereas ConfigMaps data will be stored in plain text format, which is fine for text files.</p>
</blockquote>

<p>The ConfigMap API is simple conceptually. From a data perspective, the ConfigMap type is just a set of key-value pairs.</p>

<p>There are several ways you can create config maps:</p>

<ul>
<li><p>Using list of values in the command line</p>

<pre><code>$ kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
</code></pre></li>
<li><p>Using a file on the disk as a source of data</p>

<pre><code>$ kubectl create configmap game-config-2 --from-file=docs/user-guide/configmap/kubectl/game.properties --from-file=docs/user-guide/configmap/kubectl/ui.properties
$ kubectl create configmap game-config-3 --from-file=game-special-key=docs/user-guide/configmap/kubectl/game.properties
</code></pre></li>
<li><p>Using directory with files as a source of data</p>

<pre><code>$ kubectl create configmap game-config --from-file=configure-pod-container/configmap/kubectl/
</code></pre></li>
<li><p>Combining all three previously mentioned methods</p></li>
</ul>

<p>There are several ways to consume a ConfigMap data in Pods</p>

<ul>
<li><p>Use values in ConfigMap as environment variables</p>

<pre><code>spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY)" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: SPECIAL_LEVEL
</code></pre></li>
<li><p>Use data in ConfigMap as files on the volume</p>

<pre><code>spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # ConfigMap containing the files
        name: special-config
</code></pre></li>
</ul>

<blockquote>
  <p>Only changes in ConfigMaps that are consumed in a volume will be visible inside the running pod. Kubelet is checking whether the mounted ConfigMap is fresh on every periodic sync. However, it is using its local ttl-based cache for getting the current value of the ConfigMap. As a result, the total delay from the moment when the ConfigMap is updated to the moment when new keys are projected to the pod can be as long as kubelet sync period + ttl of ConfigMaps cache in kubelet.</p>
</blockquote>

<p>Pod that contains in specification any references to non-existent ConfigMap or Secrets won't start.</p>

<p>Consider to read official documentation and other good articles for even more details:</p>

<ul>
<li><a href="https://kubernetes.io/blog/2016/04/configuration-management-with-containers/" rel="nofollow noreferrer">Configuration management with Containers</a></li>
<li><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer">Configure a Pod to Use a ConfigMap</a></li>
<li><a href="https://kubernetes-v1-4.github.io/docs/user-guide/configmap/" rel="nofollow noreferrer">Using ConfigMap</a></li>
<li><a href="https://medium.com/google-cloud/kubernetes-configmaps-and-secrets-68d061f7ab5b" rel="nofollow noreferrer">Kubernetes ConfigMaps and Secrets</a></li>
<li><a href="https://medium.com/@xcoulon/managing-pod-configuration-using-configmaps-and-secrets-in-kubernetes-93a2de9449be" rel="nofollow noreferrer">Managing Pod configuration using ConfigMaps and Secrets in Kubernetes</a></li>
</ul>
