Introduction to Helm
Managing YAML-files for Kubernetes can be sometimes really complicated: You have multiple files which describe for Kubernetes how to deploy the application. You have for example a Deployment, a Service, a ConfigMap and a Secret. All that files describe one desired state of your application.
Managing all of these files and keeping an overview of all them can get a little bit hard.
Furthermore you canβt reuse these deployment files for another deployment without copying them and changing what you need for this other deployment.
Helm-Charts
Helm helps with managing all these different YAML-files by grouping them into one file - called chart (or also a template).
A chart has at minimum name, a description and a version.
E.g. of this minimum Chart.yaml file:
apiVersion: The chart API version (required)
name: The name of the chart (required)
version: A SemVer 2 version (required)
The directory structure
You can create a chart with the following command:
helm create new-chart
This will create a new directory which contains the chart structure. In the created templates directory are the Services, Deployments and other Kubernetes objects grouped.
When you want to bring your existing application into a Helm chart, you just need to replace these files with your existing ones.
The command from above will create the following directory structure:
new-chart
βββ Chart.yaml
βββ charts
βββ templates
β βββ NOTES.txt
β βββ _helpers.tpl
β βββ deployment.yaml
β βββ hpa.yaml
β βββ ingress.yaml
β βββ service.yaml
β βββ serviceaccount.yaml
β βββ tests
β βββ test-connection.yaml
βββ values.yaml
3 directories, 10 files
Lets have a quick look at it:
templates/directory contains all template files which will be evaluated by Helm.NOTES.txtis the help text of the chart and will be displayed when the user runshelm install._helpers.tplis the place to put template helpers in.values.yamlcontains the default values of a chart.Chart.yamlcontains the description of a chart.charts/can contain other charts (also subcharts).
Injecting parameters (.Values)
With the generated chart you can reduce the number of your YAML-files by just injecting parameters into the templates.
When you open the service.yaml which was generated by Helm you see the following:
apiVersion: v1
kind: Service
metadata:
name: {{ include "new-chart.fullname" . }}
labels:
{{- include "new-chart.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
{{- include "new-chart.selectorLabels" . | nindent 4 }}
As you see there are some values which are in curly braces, which is an indicator for the Go templating language, which Helm is using.
The following line makes use of the parameter service.type:
spec:
type: {{ .Values.service.type }}
.Values parameters are accessed from the values.yaml file.
If you open up the values.yaml you can see that it was set to ClusterIP:
service:
type: ClusterIP
port: 80
Other values
In some cases you will also find the access to a .Chart value. This is some metadata of a Helm Chart which are accessed from the Chart.yaml file. The Chart.yaml file has its own structure and doesnβt allow the values which are not defined by Helm itself.
There are also other values which you can find in the documentation
Render the templates locally
If you want to see which values get populated by Helm, you can run the helm template command, so that you donβt need to deploy your application to check which parameters get used!
helm template [NAME] [CHART]
E.g.:
helm template my-app new-chart
Overriding default values
If you want to override the default values of a Helm chart (like e.g. of a download helm chart), you need to create a new YAML-file which contains these values.
You donβt need to copy the whole values.yaml file, you just can override the ones you want and install the Helm Chart by handing over this file:
helm install -f myvals.yaml ./new-chart
Another form of injecting parameters is via the CLI --set option:
helm install --set param=val
And this is the key feature of Helm, which makes our YAML-files generic!