<p>I'm learning k8s and am struggling with writing Helm charts to generate config files for an application I'm using to ramp up on the ecosystem. I've hit an interesting issue where I need to generate configs that are common to all nodes, and some that are unique to each node. Any idea how I would do this?</p>

<p>From my values.xml file:</p>

<pre><code># number of nodes / replicas
nodeCount: 5
replicaCount: 3
</code></pre>

<p>The common config across all nodes called node_map.xml:</p>

<pre><code>              &lt;default&gt;
                    &lt;node&gt;
                        &lt;replica&gt;
                            &lt;host&gt;wild-wallaby-0&lt;/host&gt;
                            &lt;port&gt;8000&lt;/port&gt;
                        &lt;/replica&gt;
                        &lt;replica&gt;
                            &lt;host&gt;scary-rapids-1&lt;/host&gt;
                            &lt;port&gt;8000&lt;/port&gt;
                        &lt;/replica&gt;
                    &lt;/node&gt;
                    &lt;node&gt;
                        &lt;replica&gt;
                            &lt;host&gt;wild-wallaby-1&lt;/host&gt;
                            &lt;port&gt;8000&lt;/port&gt;
                        &lt;/replica&gt;
                        &lt;replica&gt;
                            &lt;host&gt;scary-rapids-2&lt;/host&gt;
                            &lt;port&gt;8000&lt;/port&gt;
                        &lt;/replica&gt;
                    &lt;/node&gt;
                    &lt;node&gt;
                        &lt;replica&gt;
                            &lt;host&gt;wild-wallaby-2&lt;/host&gt;
                            &lt;port&gt;8000&lt;/port&gt;
                        &lt;/replica&gt;
                        &lt;replica&gt;
                            &lt;host&gt;scary-rapids-0&lt;/host&gt;
                            &lt;port&gt;8000&lt;/port&gt;
                        &lt;/replica&gt;
                    &lt;/node&gt;
              &lt;/default&gt;
</code></pre>

<p>The above is easy enough to generate, and this config is ready be every pod that is a single container, but now each pod also needs to have an additional config file written letting that pod know what node and replica that instance is, called instance.xml. Note that the file doesn't need to be called instance.xml... I have the flexibility to point at and load any named file as long as I know what name to include in the start command.</p>

<p>For example...</p>

<p>Two instances would run on node wild-wallaby-0, node 0 replica 1, and node 0 replica 2. Each instance would need config files generated as such:</p>

<p>The first instance...</p>

<pre><code>    &lt;!-- node 0 replica 1 instance.xml --&gt;
    &lt;id&gt;
        &lt;node&gt;0&lt;/node&gt;
        &lt;replica&gt;1&lt;/replica&gt;
    &lt;/id&gt;
</code></pre>

<p>And the second instance ...</p>

<pre><code>&lt;!-- node 0 replica 2 instance.xml --&gt;
    &lt;id&gt;
        &lt;node&gt;0&lt;/node&gt;
        &lt;replica&gt;2&lt;/replica&gt;
    &lt;/id&gt;
</code></pre>

<p>This of course can follow some convention based on the number of nodes and replicas defined in my values file. While it's easy to generate the file that's common across all nodes, it's not clear to me how I can generate a custom config file for each node from a helm chart for the instance.xml files. </p>

<p>Any ideas or pointers? </p>

user797963
<p>You can deploy this as a StatefulSet, and use an <code>initContainers:</code> to create the config file before the main task of the pod really starts up.</p>

<p>The Kubernetes documentation has <a href="https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/" rel="nofollow noreferrer">a fairly detailed of example of this</a> oriented around a replicated MySQL cluster, but with the same essential setup: there is a master node and some number of replicas, each needs to know its own ID, and the config files are different on the master and replicas.</p>

<p>It looks like the important detail you can work from is that the pod's <code>hostname</code> (as in the shell command) is <code>statefulsetname-123</code>, where the numbers are sequential and the individual pods are guaranteed to be started in order.  The same detail is in <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-name-label" rel="nofollow noreferrer">a `statefulset.kubernetes.io/pod-name' label</a>, which you can retrieve via the <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">downward API</a>.</p>

<p>I might create a ConfigMap like:</p>

<pre><code>version: v1
kind: ConfigMap
metadata:
  name: config-templates
data:
  config.xml.tmpl: &gt;-
    &lt;id&gt;
      &lt;node&gt;NODE&lt;/node&gt;
      &lt;replica&gt;REPLICA&lt;/replica&gt;
    &lt;/id&gt;
</code></pre>

<p>And then my StatefulSet spec could look like, in part:</p>

<pre><code>version: apps/v1
kind: StatefulSet
...
spec:
  ...
  template:
    spec:
      volumes:
        - name: config
          emptyDir: {}
        - name: templates
          configMap:
            name: config-templates
      initContainers:
        - name: configfiles
          image: ubuntu:16.04
          command:
            - sh
            - -c
            - |
              POD_NUMBER=$(hostname | sed 's/.*-//')
              NODE=$(( $POD_NUMBER / 5 ))
              REPLICA=$(( $POD_NUMBER % 5 ))
              sed -e "s/NODE/$NODE/g" -e "s/REPLICA/$REPLICA/g" \
                /templates/config.xml.tmpl &gt; /config/config.xml
          volumeMounts:
            - name: templates
              mountPath: /templates
            - name: config
              mountPath: /config
      containers:
        - name: ...
          ...
          volumeMounts:
            - name: config
              mountPath: /opt/myapp/etc/config
</code></pre>

<p>In that setup you ask Kubernetes to create an empty temporary volume (<code>config</code>) that's shared between the containers, and make the config map available as a volume too.  The init container extracts the sequential pod ID, splits it into the two numbers, and writes the actual config file into the temporary volume.  Then the main container mounts the shared config directory into wherever it expects its config files to be.</p>
