How to pull environment variables with Helm charts
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Helm is a powerful package manager for Kubernetes that simplifies the deployment and management of applications by utilizing templated Kubernetes manifests. One common requirement in deploying applications is the configuration of environment variables. Helm charts provide a flexible way to inject these variables into Kubernetes resources such as Pods. This article provides a comprehensive guide on how to pull environment variables within Helm charts, complete with examples and technical explanations.
Understanding Environment Variables in Helm
Environment variables are key-value pairs that can be used to configure applications running in Kubernetes. With Helm charts, you can source these variables from different places like values.yaml, ConfigMaps, or Secrets. This guide will cover these different methods, offering detailed explanations and examples.
Method 1: Using values.yaml
The default way to manage environment variables in a Helm chart is by specifying them in the values.yaml file. This is similar to how configuration options are handled in many other templating languages.
Example
- Define the environment variables in
values.yaml:
- Reference these variables in your template:
Method 2: Sourcing From ConfigMaps and Secrets
When dealing with sensitive data or configuration that changes frequently, it's more secure and functional to use ConfigMaps and Secrets to store environment variables.
Example Using ConfigMaps
- Create a ConfigMap with environment variable data:
- Reference the ConfigMap in your Helm chart:
Example Using Secrets
- Create a Secret with sensitive data in encrypted format:
- Reference the Secret in your Helm chart:
Advanced Usage: Conditional Environment Variables
Helm supports conditional statements, allowing you to include environment variables based on specific conditions.
Conditional Variables Example
- Define conditions in
values.yaml:
- Use a conditional block in your template:
Table: Summary of Methods
| Method | Description | Example Usage |
values.yaml | Store non-sensitive data | Env variables defined directly |
| ConfigMap | Store non-sensitive data with dynamic updates | envFrom - configMapRef
- name: |
| Secret | Store sensitive data securely (base64 encoded) | envFrom - secretRef
- name: |
| Conditional Variables | Custom logic for environment variable insertion | {{- if .Values.useDebug }} behavior |
Conclusion
Injecting environment variables in Helm charts is a flexible and powerful way to manage configurations in Kubernetes applications. By leveraging values.yaml, ConfigMaps, Secrets, and conditional logic, you can tailor the deployment environment to the specific needs of your application. As best practices, always aim to store sensitive data in Secrets and externalize frequently changing configurations to ConfigMaps to streamline updates. This ensures both security and flexibility in your Kubernetes deployments.
Related reading
- How to pull image from dockerhub in kubernetes?
- How to reconnect partitioned nodes in erlang cluster
- How to recover pvReleased data after pvc deletion
- How to recycle pods in Kubernetes
- How to push different local Git branches to Heroku/master
- How to query cloudwatch logs using boto3 in python
- how to redirect http to https using a kubernetes ingress controller on Amazon EKS
- How to reference kubernetes secrets in helm chart?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.