Pass environment variables into Helm Chart?

Helm chart provides a couple of ways to access or pass environment variables into the templates

  1. set
  2. values.yaml

But before you start reading the article let us make some baseline.

In this article, I have created only one chart helloworld. So I will be taking the same hellworld helm chart to pass the environment variables.

I have used the following Helm Command to create helloworld

1 helm create helloworld

To learn more about Helm Chart you can visit - Getting Started with Helm Chart

Table of Content

  1. Using --set
  2. Using --values.yaml



1. --set

1.1 Basic Example

To begin with, I would like to take a very basic example with --set where we are going to set the replicatCount=2

Here is the command to install helloworld

1helm install --set replicaCount=2 helloworld-1 world

This command will set the replicaCount to 2.

You can verify it using the following command -

1kubectl get deployment
1NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
2helloworld-1                  2/2     1            2           2m 

1.2 Set multiple values and Overriding

--set also provides a way to use multiple values as well as you can override the values also.

Please refer to the following command where I have --set multiple values as well as overridden the replicaSet to 3.

1helm install --set replicaCount=2  --set replicaCount=3 helloworldrelease helloworld

You can check the status by running `kubectl get deployment

1NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
2helloworldrelease            3/3     3            3           12s 


2. values.yaml

2.1 Basic Example

Let see how to use values.yaml

To begin with we are going to take a very basic example where we are going to set the replicaCount=2

First, create myvalues.yaml -

1vi myvalues.yaml

Add following replica count entry to it

1replicaCount: 2

Install the helm chart -

1helm install -f myvalues.yaml helloworldrelease helloworld 

Verify the replica count status -

1kubectl get deployment
1NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
2helloworldrelease            2/2     1            2           2m 

As you can we have passed the variable replicaCount via myvalues.yaml.



2.2 Iterate over key-value map

Create a file named env-values.yaml and store the above key-value map inside it.

1vi env-values.yaml
1examplemap:
2 - name: "USERNAME"
3   value: "test1"
4 - name: "PASSWORD"
5   value: "test2" 

Helm template can be made dynamic by using values.yaml.

Using values.yaml you can store the parameters in the form of maps.

After creating the en-values.yaml, let's see how to access the env-values.yaml.

To iterate or loop over the map first we need to create a place holder inside the Helm template.

Here are my placeholders -

1env:
2    {{- range .Values.examplemap }}
3    - name: {{ .name }}
4      value: {{ .value }}
5    {{- end }} 

In my case, I am taking deployment.yaml which is present at /template/deployment.yaml.

This is how my deployment.yaml would look like after putting the placeholder -

 1      securityContext:
 2        {{- toYaml .Values.podSecurityContext | nindent 8 }}
 3      containers:
 4        - name: {{ .Chart.Name }}
 5          securityContext:
 6            {{- toYaml .Values.securityContext | nindent 12 }}
 7          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
 8          imagePullPolicy: {{ .Values.image.pullPolicy }}
 9          env:
10            {{- range .Values.examplemap }}         --Iteration begins over here
11            - name: {{ .name }}
12              value: {{ .value }}
13            {{- end }}                              --Iteration ends over here

Let's verify the Helm chart with helm template command

1helm template -f env-values.yaml helloworld 

The above command should generate the complete template including service.yaml, deployment.yaml but we will only focus on deployment.yaml

Here is the sample output snippet of the command helm template -f env-values.yaml helloworld

 1      securityContext:
 2        {{- toYaml .Values.podSecurityContext | nindent 8 }}
 3      containers:
 4        - name: {{ .Chart.Name }}
 5          securityContext:
 6            {{- toYaml .Values.securityContext | nindent 12 }}
 7          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
 8          imagePullPolicy: {{ .Values.image.pullPolicy }}
 9          env:
10            - name: "USERNAME"         --Placeholders has been replaced with actual values
11              value: "test1"           
12            - name: "PASSWORD"        
13              value: "test2"           


2.3 Helm overriding values.yaml with another values-override.yaml

In the previous section, we have seen how to use values.yaml and iterate over the values map.

What if you have multiple values.yaml with similar key values pair?

Is overriding possible with multiple values.yaml?

Answer - Yes you can use multiple values.yaml and overriding is possible.

First, create myvalues.yaml

1vi myvalues.yaml

Add following entry

1replicaCount: 2

Now let's override this value in myvalues-override.yaml

1vi myvalues-override.yaml
1replicaCount: 3

Alright let's use both the yamls - myvalues.yaml, myvalues-override.yaml (we expect the replicas count to be overriden by myvalues-override.yaml with 3)

1helm install -f myvalues.yaml -f myvalues-override.yaml  helloworldrelease helloworld

Use the following command to verify the deployment status as well as replica count -

1kubectl get deployment 

We are expecting 3 replicas count

1NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
2helloworldrelease            3/3     3            3           12m 

As you can see we have overridden the replicas count and now we have total of 3 replicas deployed.



Conclusion

To conclude this topic and what we have learned -

  1. How to use --set for passing environment variables into the command line
  2. Also we looked at how to use myvalues.yaml for storing the environment variables and later passing it to Helm Chart.

Read More -

  1. Helm chart - How to Add/Install plugins
  2. Getting started with Helm Chart
  3. Helm chart - WordPress Installation with MariaDB on Kubernetes
  4. Helm chart - Build you first helm chart with Spring Boot
  5. Helm Chart - Convert Kubernetes YAML into Helm Chart YAML
  6. Helm Chart - Pass environment variables
  7. Helm Chart - Plugin
  8. Helm Chart - Dry Run Install
  9. Helm Chart - How to create multiple values files inside helm chart?
  10. Helmfile - How to use Helmfile for managing helm chart?

Posts in this Series