Introduction
In the first part we saw where to find an existing chart (Helm Hub) and how to install it on a Kubernetes cluster. We looked at how you can transfer your own configurations to the chart and how to upgrade and rollback. Today we want to go one step further and see how you can create your own chart.
Building a chart
First, let’s take a look at how a helm chart is structured. The easiest way to do this is to create our own chart. Helm gives us a simple command that we can use to create the skeleton of our own chart.
|
|
Let’s see what happened. A folder has been created in which the required configurations are located.
|
|
Chart.yml
This file contains the metadata of the chart, such as name, description, type and version. ApiVersion must always be set and is “v2” for Helm 3.
|
|
Directory: charts
This directory is empty. Here we have the option of adding existing charts that are required for our chart, i.e. dependencies.
Another way to add dependencies, preferred by Helm, is as a dependencies field in the Chart.yaml
file.
We can add dependencies here and then download these dependencies into the charts folder with the command helm dependency update
.
For our simple example we don’t need this function for now.
|
|
Further information can be found on Helm page.
Directory: templates
Now we come to the heart of our chart. All configuration files that we need for the installation on the Kubernetes cluster can be found in this directory. All files that are in the templates directory are sent through the Helm Template Rendering Engine. The results of the templates are collected and then sent to Kubernetes.
|
|
Here we see various files that Helm has already created for us.
-
NOTES.txt
- This is the help text for a chart. This is displayed to the user when he runs helm install
-
deployment.yaml
,service.yaml
,hpa.yaml
,ingress.yaml
,serviceaccount.yaml
:- Basic framework to create a Kubernetes resource
-
_helpers.tpl
:- Here are helpers for templates that can be reused in the chart.
-
tests
:- Here are tests to verify the functionality of the chart in the cluster.
values.yaml
The values.yaml
file is important to parse the templates. All default values are located here, which can be overwritten by the user with helm install
or helm upgrade
.
With this knowledge we can now develop our first own chart.
The first own chart
Let’s start with a simple example. To do this, we first delete all files in the templates folder so that we can start our little tutorial from scratch.
|
|
Then we create our first own template. We create a file named configmap.yaml
with the following content:
|
|
We now have an unknown syntax in the file.
{{ .Release.Name }}
and {{.Values.myName}}
. Both are special placeholders that are replaced by the template rendering engine.
As the name suggests, {{ .Release.Name }}
is dynamically replaced by the name of the release that we specify when installing.
{{.Values.myName}}
is replaced by the property myName from the file values.yaml
. Let’s take a closer look at this file.
|
|
Now we already have a fully executable and installable chart. Let’s see what happens during the installation. To do this, we can append the flag --dry-run
to the install command to simulate the installation. With --debug
we activate “verbose” output.
|
|
As we can see, our placeholders have been replaced as requested.
Now we can install our chart.
However, we don’t want to use the default name “b-nova” during the installation, we want to assign a name of our own.
We have already seen above that we can overwrite the values by specifying our own values.yaml
file, or by using the --set
flag during installation.
We want to try the second variant at this point.
|
|
Success! The chart was successfully installed and the default name was replaced with the dynamic value.
Now we want to verify again whether our ConfigMap was really deployed correctly. So we look directly on the cluster with kubectl
.
|
|
As we can see, the ConfigMap was created with the correct name. Let’s also take a look at the content for completeness.
|
|
You can find the entire code in the b-nova Github.
The content also corresponds to the expected result. We have successfully created our first chart and installed it on our cluster.
Next steps:
Helm Charts has a great future in the managed container environment. We will therefore be eager to follow the development of Helm. In the next TechUp I want to look at the following topics with you using a practical example:
-
Monocular - the front end for helms
-
Host your own repository
-
Template:
-
Template functions and pipelines
-
flow control
-
Built-In Objects
-
Named templates
-
subcharts
-
Debugging Templates
-
Post rendering
-
File handling
-
-
Best practices
Have fun and stay tuned!
This text was automatically translated with our golang markdown translator.