How to Use Environment Variables with Helm Charts

February 24, 2021

Introduction

Helm charts are a convenient and efficient way to collect Kubernetes resources and build application clusters. They can also use environment variables on local machines to define values for your Kubernetes deployment to use.

This tutorial will cover different ways you can include environment variables in your Kubernetes deployment.

How to use environment variables with Helm charts

Prerequisites

How to Use Environment Variables with Helm

There are two methods of using environment variables with Helm charts:

For this tutorial, we are using the USERNAME and PASSWORD environment variables.

Note: Environment variables with Helm work similarly to environment variables in Linux, in Windows or any other OS. It passes information that helps define the environment.

Mounting Environment Variables in a Kubernetes Deployment

1. Add the following lines to the values.yaml file in your Helm chart:

username: root
password: password
Add username and password to the values file

2. Create a new file called secret.yaml and add it to the template folder. Add the following content to the file:

apiVersion: v1
kind: Secret
metadata:
  name: {{ .Release.Name }}-auth
data:
  password: {{ .Values.password | b64enc }}
  username: {{ .Values.username | b64enc }}
Create the secrets.yaml file

3. Edit the env section of your Kubernetes deployment to include the new variables defined in the secret.yaml file:

...
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:          
            - name: "USERNAME"
              valueFrom:
                secretKeyRef:
                  key:  username
                  name: {{ .Release.Name }}-auth
            - name: "PASSWORD"
              valueFrom:
                secretKeyRef:
                  key:  password
                  name: {{ .Release.Name }}-auth
...
Edit the env section of your deployment

4. Set the environment variables to your desired values. For example, set the USERNAME variable to hello_user:

export USERNAME=hello_user

5. Apply the variables to the Helm chart by combining them with the helm install command:

helm install --set username=$USERNAME [chart name] [chart path]

Where:

  • [chart name] is the name of the Helm chart you are using.
  • [chart path] is the path to the Helm chart you are using.

If you want to test the new settings out before applying them, use the dry run mode:

helm install --dry-run --set username=$USERNAME --debug [chart name] [chart path]
Test out the new settings

Adding a Custom Helper in Helm

1. Use the env section of the values.yaml file to define sensitive and non-sensitive variables. Use the normal and secret categories to list the appropriate variables:

secret:
  name: app-env-var-secret
env:
  normal:
    variable1: value1
    variable2: value2
    variable3: value3
  secret:
    variable4: value4
    variable5: value5
    variable6: value6

Using this method, we then add the USERNAME and PASSWORD variables to the secret category:

…
  secret:
    USERNAME: [username]
    PASSWORD: [password]

Where:

  • [username] is the value you want to set for the USERNAME variable.
  • [password] is the value you want to set for the PASSWORD variable.
Add the username and password variables to the secret category of the values file

2. Add the path to the values.yaml file to the bottom of your .gitignore file:

charts/values.yaml

3. Create a file called secrets.yaml in the templates folder and add the following content:

apiVersion: v1
kind: Secret
metadata:
  name: {{ .Values.secret.name }}
type: Opaque
data:
  {{- range $key, $val := .Values.env.secret }}
  {{ $key }}: {{ $val | b64enc }}
  {{- end}}
Create the secrets.yaml file in the templates folder

4. Find the helpers.tpl file in the templates folder. Add the following to the bottom of the file to write a new helper function:

{{- define "helpers.list-env-variables"}}
{{- range $key, $val := .Values.env.secret }}
- name: {{ $key }}
  valueFrom:
    secretKeyRef:
      name: app-env-secret
      key: {{ $key }}
{{- end}}
{{- end }}
Create a new helper in the helpers.tpl file

5. Call on the helper you created by adding the following to your pod.yaml file:

…
spec:
  containers:
  - name: mycontainer
    image: redis
    env:
    {{- include "helpers.list-env-variables" . | indent 6 }}
  restartPolicy: Never
Call on the new helper in the pod.yaml file

Note: Learn more about using Helm charts with our tutorials such as:

Conclusion

After following this tutorial, you should have your deployment set up to use environment variables.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
How To Create A Helm Chart
February 3, 2021

Helm charts are application packages that use Kubernetes resources. They provide...
Read more
How to Roll Back Changes with Helm
February 3, 2021

Helm is a package manager for Kubernetes that offers a host of useful...
Read more
How to Add, Update or Remove Helm Repositories
December 21, 2020

Helm is Kubernetes' equivalent to apt and yum. It is a package manager used for...
Read more
How to Install Helm on Ubuntu, Mac and Windows
December 10, 2020

Helm is a package manager for Kubernetes that simplifies deployment...
Read more