Search code examples
kuberneteskubernetes-helm

How to pass multiple docker images through values.yml to template.yml in Helm


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.

template file

      name:{{ .values.name }}
spec:
   containers:
      - image: {{ .values.container.image }}

values file

name:A
 container:
     image:A
name :B
  container :
       image:B

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


Solution

  • Focusing only on images and templates you can create a helm template that will spawn an X amount of pods by:

    • Creating a Chart.yaml file
    • Creating a values.yaml file with the variable that store all image names
    • Creating a template with a {{ range }} directive
    • Testing

    Below is the structure of files and directories:

    ❯ tree helm-dir
    helm-dir
    ├── Chart.yaml
    ├── templates
    │   └── pod.yaml
    └── values.yaml
    
    1 directory, 3 files
    

    Create Chart.yaml file

    Below is the Chart.yaml file:

    apiVersion: v2
    name: helm-templates
    description: A Helm chart for spawning pods from images
    version: 0.1.0
    

    Create a values.yaml file with the variable that store all image names

    Below is the simple values.yaml file with different images name that will be used with a template:

    different_images: 
      - ubuntu 
      - nginx
    

    Create a template that with a {{ range }} directive

    This template is stored in templates directory with a name pod.yaml

    Below YAML definition will be a template for all the pods:

    {{- range .Values.different_images }}
    apiVersion: v1
    kind: Pod
    metadata:
      name: {{ . }} 
      labels:
        app: {{ . }} 
    spec:
      restartPolicy: Never
      containers:
      - name: {{ . }} 
        image: {{ . }} 
        imagePullPolicy: Always
        command: 
        - sleep 
        - infinity
    ---
    {{- end }} 
    

    {{- range .Values.different_images }} will iterate over different_images variable and replace the {{ . }} with an image name.


    Test

    Run below command from helm-dir directory to check if helm YAML definitions of pods are correctly created:

    $ helm install NAME . --dry-run --debug

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

    # 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 
    

    You can now run: $ helm install NAME .

    and check if pods spawned correctly with $ kubectl get pods:

    NAME     READY   STATUS    RESTARTS   AGE
    nginx    1/1     Running   0          8s
    ubuntu   1/1     Running   0          8s
    

    Please take a look on additional resources: