Helm
Kubernetes
Environment Variables
DevOps
Configuration Management

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.

Practice system design

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

  1. Define the environment variables in values.yaml:
yaml
1    env:
2      - name: APP_ENV
3        value: production
4      - name: APP_DEBUG
5        value: "false"
  1. Reference these variables in your template:
yaml
1    # deployment.yaml
2    apiVersion: apps/v1
3    kind: Deployment
4    metadata:
5      name: myapp
6    spec:
7      template:
8        spec:
9          containers:
10            - name: myapp
11              image: myimage
12              env:
13                {{- range .Values.env }}
14                - name: {{ .name }}
15                  value: {{ .value }}
16                {{- end }}

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

  1. Create a ConfigMap with environment variable data:
yaml
1    kind: ConfigMap
2    apiVersion: v1
3    metadata:
4      name: app-config
5    data:
6      APP_ENV: production
7      APP_DEBUG: "false"
  1. Reference the ConfigMap in your Helm chart:
yaml
1    # deployment.yaml
2    apiVersion: apps/v1
3    kind: Deployment
4    metadata:
5      name: myapp
6    spec:
7      template:
8        spec:
9          containers:
10            - name: myapp
11              image: myimage
12              envFrom:
13                - configMapRef:
14                    name: app-config

Example Using Secrets

  1. Create a Secret with sensitive data in encrypted format:
yaml
1    kind: Secret
2    apiVersion: v1
3    metadata:
4      name: app-secrets
5    data:
6      DATABASE_PASSWORD: c2VjcmV0cGFzcw===
  1. Reference the Secret in your Helm chart:
yaml
1    # deployment.yaml
2    apiVersion: apps/v1
3    kind: Deployment
4    metadata:
5      name: myapp
6    spec:
7      template:
8        spec:
9          containers:
10            - name: myapp
11              image: myimage
12              envFrom:
13                - secretRef:
14                    name: app-secrets

Advanced Usage: Conditional Environment Variables

Helm supports conditional statements, allowing you to include environment variables based on specific conditions.

Conditional Variables Example

  1. Define conditions in values.yaml:
yaml
    useDebug: true
  1. Use a conditional block in your template:
yaml
1    # deployment.yaml
2    apiVersion: apps/v1
3    kind: Deployment
4    metadata:
5      name: myapp
6    spec:
7      template:
8        spec:
9          containers:
10            - name: myapp
11              image: myimage
12              env:
13                - name: APP_ENV
14                  value: production
15                {{- if .Values.useDebug }}
16                - name: APP_DEBUG
17                  value: "true"
18                {{- else }}
19                - name: APP_DEBUG
20                  value: "false"
21                {{- end }}

Table: Summary of Methods

MethodDescriptionExample Usage
values.yamlStore non-sensitive dataEnv variables defined directly
ConfigMapStore non-sensitive data with dynamic updatesenvFrom - configMapRef - name:
SecretStore sensitive data securely (base64 encoded)envFrom - secretRef - name:
Conditional VariablesCustom 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.