<p>I have my microservice and ConfigMap deployed in cluster.</p>
<p>I have a need to update the value of variable defined in <code>ConfigMap</code> object on the fly. I can re-start the microservice programmatically (like re-create the service pod). The main thing is to keep the <code>ConfigMap</code> object in the cluster and only update the value in it.</p>
<p>My current idea is to define env variables in an external file:</p>
<pre><code>key1: foo
key2: bar
</code></pre>
<p>and in ConfigMap manifest mount the file:</p>
<pre><code>spec:
  containers:
    image: ...
    volumeMounts:
    - mountPath: /clusters-config
      name: config-volume
  volumes:
    - configMap:
        name: my-env-file
      name: config-volume
</code></pre>
<p>I wonder if using this approach, what is the main pitfalls/cons I should consider?</p>
<p>Is there an better option/solution if not using the volume mounted <code>ConfigMap</code> but keep variables inside ConfigMap manifest?</p>

user842225
<blockquote>
<p>The main thing is to keep the ConfigMap object in the cluster and only
update the value in it.</p>
</blockquote>
<p>As said in the comment proper requirement is not clear to me still trying my best to answer.</p>
<p><strong>Yes</strong>, you can keep the <strong>configmap</strong> object in the cluster and update the value in it.</p>
<p>The <strong>mount</strong> option would be the best one to use instead of using <strong>Env</strong> as per your requirement.</p>
<p>If you mount the <strong>configmap</strong> to <strong>Deployment</strong> of POD after some <strong>TTL</strong> <strong>second</strong> it got <strong>auto-updated</strong> to the deployment or inside POD.</p>
<p>So if you are mounting the configmap there might be delay little bit but it will get <strong>auto updated</strong> in pod.</p>
<p>In this scenario, your application should be smart which takes new values out of the <strong>mounted file</strong> once updated.</p>
<p>You can refer to my article on auto update the configmap for more details : <a href="https://faun.pub/update-configmap-without-restarting-pod-56801dce3388" rel="nofollow noreferrer">https://faun.pub/update-configmap-without-restarting-pod-56801dce3388</a></p>
<p><strong>Example</strong></p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: test-config
data:
  hello: world
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
spec:
  selector:
    matchLabels:
      app: test
  replicas: 1
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - name: configmaptestapp
        image: &lt;Image&gt;
        volumeMounts:
        - mountPath: /config
          name: data-volume
        ports:
        - containerPort: 80
      volumes:
        - name: data-volume
          configMap:
            name: test-config
</code></pre>
<p>now anytime you want to update the configmap just apply YAML with the updated value, the POD value will get updated <strong>automatically</strong> <strong>without restart</strong>.</p>
<p>If you are using as <strong>Env</strong> you have to restart the POD each time once configmap is updated.</p>
<p>To automate this you can also leverage the <strong>Reloader</strong> which will restart your deployment if configmap got updated. <a href="https://github.com/stakater/Reloader" rel="nofollow noreferrer">https://github.com/stakater/Reloader</a></p>
