<p>I am new to Kubernetes and Helm. I am coming from a plain Docker/docker-compose world.</p>

<p>I have some complex services running multiple Docker containers that require a lot of configuration parameters and logic. The docker-ized services require a lot of different configuration files, keys and command line arguments on start up. I also require some configuration logic at runtime (some configuration elements have to be generated) that can only execute inside of the container.</p>

<p>What I ended up doing is to write a shell script (to use as <code>CMD</code>) that expects environment variables, defines default values, translates those environment variables to command arguments and configuration files.</p>

<hr>

<p>This is a non-working example of how I build it, without having Kubernetes and Helm in mind.</p>

<p><strong>Dockerfile</strong></p>

<pre><code>...
CMD [ "./bootstrap.sh" ]
</code></pre>

<p><strong>bootstrap.sh (packaged in image)</strong></p>

<pre><code># Define default values, if no environment variables provided on
# on "docker run"
export CONFIG_VALUE_A=${CONFIG_VALUE_A:="a"} 
export CONFIG_VALUE_B=${CONFIG_VALUE_B:="b"}
export CONFIG_VALUE_C=${CONFIG_VALUE_C:="c"}

# write CONFIG_VALUE_A to file
echo ${CONFIG_VALUE_A} &gt; ./some-config-file-a.cfg

ARGS="--config-file-a ./some-config-file-a.cfg --config-value-b ${CONFIG_VALUE_B} --config-value-c ${CONFIG_VALUE_C}"
exec ./my-app ${ARGS}
</code></pre>

<p>This has the advantage that using the environment variables, I have a standard configuration interface and don't need to deal with volumes for configuration files. </p>

<hr>

<p>Now, I am stepping into Kubernetes in Helm. Helm has its own parameter concept using the <code>values.yaml</code>. To combine it with what I already have above, I would just map values from the <code>values.yaml</code> with those environment variables.</p>

<p><strong>deployment.yaml</strong></p>

<pre><code>...
 spec:
  ...
  template:
    ...
    spec:
      containers:
      - name: my-app
        ...
        env:
        - name: "CONFIG_VALUE_A"
          value: {{ .Values.config.value_a }}
        - name: "CONFIG_VALUE_B"
          value: {{ .Values.config.value_b }}
        - name: "CONFIG_VALUE_C"
          value: {{ .Values.config.value_c }}
</code></pre>

<p><strong>values.yaml</strong></p>

<pre><code>config:
  value_a: a
  value_b: b
  value_c: c
</code></pre>

<p>However, having three configuration layers where I map values back and forth (helm templates => Container environment variables => Config files/CLI arguments) violates the DRY principle and adds a lot of potential for typos/errors that will be hard to find later.</p>

<hr>

<p>Ideally,</p>

<ul>
<li>I would just define my configuration structure in the <code>deployment.yaml</code> and my defaults once in Helm's <code>default.yaml</code></li>
<li>I would pass those values straight through to the container and let some sort of configuration script build the command line arguments and configuration file, without the use of environment variables as a middle layer</li>
<li>Use some sort of type-safe configuration format</li>
<li>Keep the total # of lines as low as possible</li>
<li>Keep configuration files readable and don't mix up different languages (i.e. JSON defined in a YAML file)</li>
</ul>

<p>How do you solve complex configuration management with Kubernetes, Helm and Docker? </p>

<p>In the Kubernetes world, configs are usually managed by ConfigMap, which is the main storage of configuration.</p>

<p>In your situation, I think you can do it like that (at least if I will do it, I will do it that way):</p>

<ol>
<li>Create another one template in Helm with kind ConfigMap and create there a structure of a <code>.cfg</code> file for an application. Helm is using GoTemplate format, so it is easy to create any structures there, with iterations etc.</li>
<li>Add all default values to the <code>values.yaml</code> file. </li>
<li>Edit <code>deployment.yaml</code>. Add a mounting of <code>.cfg</code> file to a path in the container and point the application to it.</li>
<li>Use an additional file with values (or several once) and write there overrides of default values into it.</li>
</ol>

<p>So, that's it. We have:</p>

<ul>
<li>The ConfigMap with the static configuration in application format, and we can check it at any time. </li>
<li>Only one place where we can edit it - in our default and override yamls.</li>
<li>Readable <code>key: value</code> YAML format. </li>
<li>All logic of the config file generation and sorting out of a container, so we don't need to build a new version just to change an order of options.</li>
</ul>
