Kubernetes-helm: using one template for multiple applications Kubernetes-helm: using one template for multiple applications kubernetes kubernetes

Kubernetes-helm: using one template for multiple applications


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-apphelm install --set repository=myApp.mycompany.com/demo/my-second-app --name my-second-app /path/to/helm/chart my-second-app


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.