In our previous article, we discussed the file and folder structure of the Helm Charts and discussed a simple template file by creating it and installing it into the Kubernetes Cluster. Now, we will discuss what is Helm Chart Built-in Objects and Values For Template. Along with that, we will discuss a sample Deployment using The Helm chart.
Table of Contents
Sample Deployment.
Create a folder structure using CLI as the first step and it will create the folders as we discussed in our previous article.
- Chart.yaml
- values.yaml
- template/
- charts/
Then, we are going to create a sample deployment by deploying ConfigMaps
into the Kubernetes cluster.
ConfigMaps
Config map is an entity within Kubernetes that will have the key-value pair. This will be used by other entities to read the values respective to the keys. We can use the kubectl
command to do the same but we can also do this using the helm chart to deploy the config map to the Kubernetes cluster. Let’s create the Configmap.
apiVersion: v1
kind: ConfigMap
metadata:
name: my-chart-configmap
data:
my-key: "my-value"
Now, Place the configmaps.yml
file into the folder called my-chart
inside the Template folder.
Deploying ConfigMap using Helm.
Now run the following command to deploy the ConfigMap entity using Helm.
$ helm install helm-my-chart-configmap ./my-chart
This will give you output as followed.
NAME: helm-my-chart-configmap
LAST DEPLOYED: Mon Oct 25 20:21:41 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
So, now you can see the Kubernetes entities available in the cluster using helm ls command. Now, Let’s see how to use Templating in the above chart.
Templating the Chart
As we discussed, Templating means having the template file and passing the dynamic values to create different files. To create the dynamic files, we are going to use Template derivative which will be the placeholder for the variables. This placeholder will be defined by opening and closing using double curly braces ({{ value goes here }}
). This Template derivative will be changed by the variable name from the variable files or by the Built-in Objects. As we already discussed, the Variable file is a separate file that will carry the required variables for the projects. Another method is to substitute the derivatives is by using built-in objects.
By Using Built-in Objects
We will discuss the variable file later in our article, now will discuss how to use built-in objects in place of template derivatives. Built-in objects or nothing but every project will have the default properties and values of the project. Then this can be retrieved using built-in object variable names.
So here we are going to use one of the built-in and objects called .Release.Name
to substitute the template derivative. So let’s see how we can replace the name of the config map using this built-in object.
apiVersion: v1
kind: ConfigMap
metadata:
name: helm-{{ .Release.Name }}-configmap
data:
my-key: "my-value"
Now let’s see how we are going to install this particular templated chart using Helm CLI.
$ helm install my-release-config ./my-chart.
This will give you the below output.
LAST DEPLOYED: Mon Oct 25 20:40:32 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
How you can verify the deployment of config map using the templating by using kubectl
describe command.
By Using values file
As we already discussed while creating the folder structure of the helm chart, we are going to have values.yml
as part of it. So now we are going to add more values as a key-value pair inside the values.yml
file.
$ cat values.yml
my-text : “this is sample value for my-text”
So, have added a variable that is going to have a string value that will be substituted in place of the template derivative we are going to create in configmap.yml
. So let’s create the template derivative in the congifmap.yml
.
apiVersion: v1
kind: ConfigMap
metadata:
name: helm-{{ .Release.Name }}-configmap
data:
my-key: "my-value"
my-text: {{ .Values.message }}
So in the above file .Values
is the object which will refer to the values.yml
file and .my-text
will transfer to the key name inside the values.yml
file.
Then, I will install the Configmap template file using the helm install command
$ helm install my-release-config ./my-chart.
So this will give you the following output.
NAME: my-release-config-configmap
LAST DEPLOYED: Mon Oct 25 20:40:32 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
Check the Manifest file
Now to verify all the installed entities with the templated values we can use the manifest file of the helm chart. Manifest of the helm is nothing but a YAML representation of cabinet is resources an entity that was generated by using helm chart. To view the manifest file can use the following helm command.
$ helm get manifest my-release-config
This will show you the below output.
apiVersion: v1
kind: ConfigMap
metadata:
name: helm- my-release-config-configmap
data:
my-key: "my-value"
my-text: this is sample value for my-text.
As you can see we have successfully substituted the value which we entered in the values.yml
file by using the helm template.
Conclusion
In this article, we discussed discuss what is Helm Chart Built-in Objects and Values For Template. Along with that, we have discussed a sample deployment using a helm chart. In our upcoming article, we will discuss what is a dry run and debug mode, Template functions, Subcharts, control flow, scoping, variables, and more. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOps, Agile, DevSecOps, and App Development.
Experienced DevSecOps Practitioner, Tech Blogger, Expertise in Designing Solutions in Public and Private Cloud. Opensource Community Contributor.