<p>I am trying to run a application that has around 40 microservices. How to pass 40 different docker images from values.yml file to template.yml file.</p>

<h2>template file</h2>

<pre><code>      name:{{ .values.name }}
spec:
   containers:
      - image: {{ .values.container.image }}
</code></pre>

<p><strong>values file</strong></p>

<pre><code>name:A
 container:
     image:A
name :B
  container :
       image:B
</code></pre>

<p>i have 40 more docker images like that, how to pass all those images to template.
And will passing like that creates <strong>40 different</strong> pods?, because we would need 40 different pods.
Any guidance is highly appreciated.</p>

<p><strong>Focusing only on images and templates</strong> you can create a helm template that will spawn an X amount of pods by:</p>

<ul>
<li>Creating a <code>Chart.yaml</code> file </li>
<li>Creating a <code>values.yaml</code> file with the variable that store all image names</li>
<li>Creating a template with a <code>{{ range }}</code> directive </li>
<li>Testing</li>
</ul>

<p>Below is the structure of files and directories: </p>

<pre class="lang-sh prettyprint-override"><code>❯ tree helm-dir
helm-dir
├── Chart.yaml
├── templates
│   └── pod.yaml
└── values.yaml

1 directory, 3 files
</code></pre>

<hr>

<h2>Create <code>Chart.yaml</code> file</h2>

<p>Below is the <code>Chart.yaml</code> file:</p>

<pre class="lang-yaml prettyprint-override"><code>apiVersion: v2
name: helm-templates
description: A Helm chart for spawning pods from images
version: 0.1.0
</code></pre>

<hr>

<h2>Create a <code>values.yaml</code> file with the variable that store all image names</h2>

<p>Below is the simple <code>values.yaml</code> file with different images name that will be used with a template: </p>

<pre class="lang-yaml prettyprint-override"><code>different_images: 
  - ubuntu 
  - nginx
</code></pre>

<hr>

<h2>Create a template that with a <code>{{ range }}</code> directive</h2>

<p>This template is stored in <code>templates</code> directory with a name <code>pod.yaml</code></p>

<p>Below <code>YAML</code> definition will be a template for all the pods: </p>

<pre class="lang-yaml prettyprint-override"><code>{{- range .Values.different_images }}
apiVersion: v1
kind: Pod
metadata:
  name: {{ . }} 
  labels:
    app: {{ . }} 
spec:
  restartPolicy: Never
  containers:
  - name: {{ . }} 
    image: {{ . }} 
    imagePullPolicy: Always
    command: 
    - sleep 
    - infinity
---
{{- end }} 
</code></pre>

<p><code>{{- range .Values.different_images }}</code> will iterate over <code>different_images</code> variable and  replace the <code>{{ . }}</code> with an image name. </p>

<hr>

<h2>Test</h2>

<p>Run below command from <code>helm-dir</code> directory to check if helm <code>YAML</code> definitions of pods are correctly created: </p>

<p><code>$ helm install NAME . --dry-run --debug</code></p>

<p>You should get an output with multiple pods definition that look similar to one below:</p>

<pre class="lang-yaml prettyprint-override"><code># Source: helm-templates/templates/pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: ubuntu
  labels:
    app: ubuntu
spec:
  restartPolicy: Never
  containers:
  - name: ubuntu
    ports:
      - containerPort: 3000
    image: ubuntu
    imagePullPolicy: Always
    command: 
    - sleep 
    - infinity
    resources:
      requests:
        memory: 500Mi
        cpu: 500m 
</code></pre>

<p>You can now run: 
<code>$ helm install NAME .</code> </p>

<p>and check if pods spawned correctly with <code>$ kubectl get pods</code>:</p>

<pre class="lang-sh prettyprint-override"><code>NAME     READY   STATUS    RESTARTS   AGE
nginx    1/1     Running   0          8s
ubuntu   1/1     Running   0          8s
</code></pre>

<p>Please take a look on additional resources: </p>

<ul>
<li><a href="https://helm.sh/docs/chart_best_practices/" rel="noreferrer">Helm.sh: Chart best practices</a></li>
<li><a href="https://helm.sh/docs/chart_template_guide/control_structures/" rel="noreferrer">Helm.sh: Chart template guide: Control structures</a></li>
<li><a href="https://stackoverflow.com/questions/51024074/how-can-i-iteratively-create-pods-from-list-using-helm">Stackoverflow.com: How can I iteratively create pods from list using helm</a></li>
</ul>
