Kubernetes-helm: using one template for multiple applications

10/10/2019

This is what I currently have:

With the basic helm create comes a values.yaml file and a template folder with a deployment.yaml. The Deployment file has been adapted to manage a spring-boot application in a docker image.

Now the values.yaml contains something like

myApp:
  repository: myApp.mycompany.com/demo/my-app-customers
  tag: stable

And the deployment.yaml contains

spec:
  template:
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.myApp.repository }}:{{ .Values.myApp.tag }}"

Let's say I have to manage another spring-boot app, really close to what "my-app-customers" is. I thought that I would just have to add another line to values.yaml, like that:

mySecondApp:
  repository: myApp.mycompany.com/demo/my-app-others
  tag: stable

But this wouldn't work with deployment.yaml because it uses .Values.myApp.repository. Which means that I need to create another file deployment-others.yaml which uses .Values.mySecondApp.repository to deploy this second app. So it is not possible to use the same template for differents applications.

  • Am I correct or did I miss something ?
  • Would there be another way to do this or do I need to copy past similar deployment (and others) file and have one template file per application ?
-- Asoub
kubernetes
kubernetes-helm
templates

2 Answers

10/10/2019

If the Deployment resource is pretty much the same, you could just consider your Helm chart as an abstraction and specify values using the --set flag.

helm install --set repository=myApp.mycompany.com/demo/my-first-app --name my-first-app /path/to/helm/chart my-first-app
helm install --set repository=myApp.mycompany.com/demo/my-second-app --name my-second-app /path/to/helm/chart my-second-app
-- prometherion
Source: StackOverflow

10/11/2019

The standard way to do this is to have a second-deployment.yaml file which is essentially a copy of the first one, but with different labels and value references. It's customary to put some templates in _helpers.tpl to generate things like the labels blocks and you can extend these for common environment variables, or you can offload some of the configuration into a ConfigMap to reduce the duplication.

The templating layer of Helm isn't really aware of YAML syntax at all, so just as long as each file produces valid YAML, you can do anything you want. That can include multiple documents in a single file, and they can be generated by templating. This can get tricky, though. The basic outline might look like

{{- range list .Values.myApp .Values.mySecondApp -}}
---
...
spec:
  template:
    spec:
      containers:
        - name: {{ $.Chart.Name }}
          image: "{{ .repository }}:{{ .tag }}"
...
{{ end -}}

If you try this, you need to be aware that . is a fragment of the values object and not the root object like it usually is; note for example $.Chart.Name to explicitly reference the top-level chart name.

-- David Maze
Source: StackOverflow