<p>In K8S, what is the best way to execute scripts in container (POD) once at deployment, which reads from confuguration files which are part of the deployment and seed ex mongodb once?</p>

<p>my project consist of k8s manifest files + configuration files</p>

<p>I would like to be able to update the config files locally and then redeploy via kubectl or helm</p>

<p>In docker-compose i could create a volume ponting at the directory where the config files resides and then in the command part execute bash -c cmds reading from the config files in the volume. How is this best done in K8S? I don't want to include the configuration files in a image via dockerfile, forcing me to rebuild the image before redeploying again via kubectl or helm</p>

user9230212
<blockquote>
  <p>How is this best done in K8S?</p>
</blockquote>

<p>There are several ways to skin a cat, but my suggestion would be to do the following:</p>

<ul>
<li>Keep configuration in <code>configMap</code> and mount it as separate volume. Such a map is kept as k8s manifest, making all changes to it separate from docker build image - no need to rebuild or keep sensitive data within image. You can also use instead (or together with) <code>secret</code> in the same manner as <code>configMap</code>.</li>
<li>Use <code>initContainers</code> to do the initialization before main container is to be brought online, covering for your 'once on deployment' automatically. Alternatively (if init operation is not repeatable) you can use <code>Jobs</code> instead and start it when necessary.</li>
</ul>

<p>Here is excerpt of example we are using on gitlab runner:</p>

<pre><code>apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: ss-my-project
spec:
  ...
  template:
    ....
    spec:
      ...
      volumes:
      - name: volume-from-config-map-config-files
        configMap:
          name: cm-my-config-files
      - name: volume-from-config-map-script
        projected:
          sources:
          - configMap:
              name: cm-my-scripts
              items:
              - key: run.sh
                path: run.sh
                mode: 0755
      # if you need to run as non-root here is how it is done:
      securityContext:
        runAsNonRoot: true
        runAsUser: 999
        supplementalGroups: [999]
      containers:
      - image: ...
        name: ...
        command:
        - /scripts/run.sh
        ...
        volumeMounts:
        - name: volume-from-config-map-script
          mountPath: "/scripts"
          readOnly: true
        - mountPath: /usr/share/my-app-config/config.file
          name: volume-from-config-map-config-files
          subPath: config.file
      ...
</code></pre>

<p>You can, ofc, mount several volumes from config maps or combine them in one single, depending on frequency of your changes and affected parts. This is example with two separately mounted <code>configMap</code>s just to illustrate the principle (and mark script executable), but you can use only one for all required files, put several files into one or put single file into each - as per your need.</p>

<p>Example of such configMap is like so:</p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-my-scripts
data:
  run.sh: |
    #!/bin/bash
    echo "Doing some work here..."
</code></pre>

<p>And example of configMap covering config file is like so:</p>

<pre><code>kind: ConfigMap
apiVersion: v1
metadata:
  name: cm-my-config-files
data:
  config.file: |
     ---
     # Some config.file (example name) required in project
     # in whatever format config file actually is (just example)
     ... (here is actual content like server.host: "0" or EFG=True or whatever)
</code></pre>

<p>Playing with single or multiple files in <code>configMaps</code> can yield result you want, and depending on your need you can have as many or as few as you want.</p>

<blockquote>
  <p>In docker-compose i could create a volume ponting at the directory where the config files resides and then in the command part execute bash -c cmds reading from the config files in the volume.</p>
</blockquote>

<p>In k8s equivalent of this would be <code>hostPath</code> but then you would seriously hamper k8s ability to schedule pods to different nodes. This might be ok if you have single node cluster (or while developing) to ease change of config files, but for actual deployment above approach is advised.</p>
