I am new to Helm and I want to create multiple deployments in k8s from a single deployment YAML file. I've used a placeholder in the deployment.yaml like
metadata:
name: job-{{ .Values.app.ujn }}
...
replicas: {{ .Values.app.replicas }}
and my values.yaml looks like
app:
replicas: 10
ujn: 1
---
app:
replicas: 20
ujn: 2
---
app:
replicas: 30
ujn: 3
The use case is something like that I want to create many deployment files with similar configuration but some params changed and I also don't want to make multiple values file for each deployment. Can I do something like that in the above example and create multiple files from a single values.yaml?
Also a follow up question, if I deploy this chart on the cluster and if I modify the number of deployments in next deploy, will helm delete the old ones or do those have to be deleted manually?
CodePudding user response:
The syntax you have puts multiple YAML documents into a single file. It's unusual for tools to support this well (though kubectl apply
and the output of Helm templates do support it); the most common case is that a tool will only read the first document.
Instead of having multiple documents, you can put a YAML list into the values file.
# values.yaml
apps:
- name: '1'
replicas: 10
- name: '2'
replicas: 20
- name: '3'
replicas: 30
Then in your template file, you can use a range
loop to loop over this. Here Helm does support multiple documents in a single output stream, so you need to make sure each template begins with the YAML start-of-document marker. Also remember that the range
loop rebinds .
so you may need different syntax for the top-level Helm objects like .Values
or .Release
, or if you call a helper template.
{{- range $app := .Values.apps -}}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ $.Release.Name }}-job-{{ $app.name }}
# labels: {{- include "mychart.labels" $ | nindent 4 }}
spec:
replicas: {{ $app.replicas }}
template: { ... }
{{ end }}