Using Environment variables in helm chart with config maps

Yash Gupta
2 min readMar 24, 2022

Overview

In the last post we discussed how to pass environment variables to a docker image from Kubernetes pod manifest. So continuing where we left off, In this post we will be using environment variables with Config maps in helm charts. We will create a Helm Chart & see how we can use environment variable with config map & use them in our docker image.

For full code refer this repository

Prerequisites

Steps

  1. Create a folder named helmchart. Directory structure will look like this:
helmchart
|--templates
|-- |--config.yaml
|-- |--pod.yaml
|--Chart.yaml
|--values.yaml

2. Create helmchart/Chart.yaml

This file provides the metadata about the chart to your definitions such as name, description, version.

3. Create helmchart/values.yaml

This file contains the configuration value- message in this case. These values can then be used in templates file by using syntax {{ .Values.message }}

4. Create helmchart/templates/config.yaml

Here we create a ConfigMap where we use the value of message from values.yaml

5. Create helmchart/templates/pod.yaml

This file contains the pod configuration. We are using the image yg18/website for our container. We are passing environment variable which is using the value from config.yaml which specifies the message value as environment variable.

6. Run the following commands to install helm chart & deploy the chart to minikube:

  • Start minikube & Install the helm application
minikube start
kubectl config use-context minikube
helm install <app-name> <chart-name>
helm install mypod helmchart/
  • View the chart and pod created in minikube
kubectl get pods
helm ls
  • Port forward the pod to view the application in localhost:3001
kubectl port-forward <podName> 3001:3000kubectl port-forward helm-practice-1 3001:3000
  • Now we see the message we set in values.yaml in our running application. Now let’s change the message value in values.yaml & redeploy the pod.
helm upgrade mypod .kubectl delete pod helm-practice-1

We deleted the pod because image was using the earlier value of message. Restarting the pod will get the latest value from the ConfigMap & rerun the pod with latest value

Conclusion

In this post you learned about how to create Helm Charts & use environment variables in ConfigMap & pass the values over to Pod manifest to be used by the docker image.

--

--