Learn how to effectively manage and inject environment variables into your Kubernetes deployments using Helm charts.
Helm charts provide a powerful way to manage Kubernetes application configurations. One of the key features of Helm is the ability to use variables, allowing for flexible and reusable charts. This flexibility is achieved through various methods, each suited for different scenarios.
Define variables in values.yaml:
# values.yaml
appName: myapp
replicaCount: 1
image:
repository: nginx
tag: 1.14.2Access variables in templates:
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
spec:
replicas: {{ .Values.replicaCount }}
template:
containers:
- name: {{ .Values.appName }}
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}Override variables during installation:
helm install myapp . --set replicaCount=3 --set image.tag=1.15.0Use environment variables in values.yaml (not recommended):
# values.yaml
appName: myapp
replicaCount: {{ env "REPLICA_COUNT" | default "1" }}Pass environment variables as arguments:
export REPLICA_COUNT=3
helm install myapp . --set replicaCount=$REPLICA_COUNTUse ConfigMaps and Secrets for sensitive data:
# configmap.yaml
apiVersion: v1
kind: ConfigMap
data:
databaseUrl: "https://example.com"# deployment.yaml
env:
- name: DATABASE_URL
valueFrom:
configMapKeyRef:
name: myapp-config
key: databaseUrlUse Helm hooks for complex logic:
# deployment.yaml
spec:
template:
metadata:
annotations:
"helm.sh/hook": pre-install,pre-upgradeThis allows you to execute scripts or commands at different stages of the Helm lifecycle.
This code repository provides examples of managing variables and values in Helm charts. It showcases defining variables in values.yaml, accessing them in templates, overriding them during installation, and using environment variables. The examples also cover utilizing ConfigMaps and Secrets for sensitive data and employing Helm hooks for complex logic. The repository includes a sample application chart structure with deployment and configmap templates. It emphasizes best practices like avoiding direct environment variable use in values.yaml for improved predictability and portability.
This repository demonstrates different ways to manage variables and values in Helm charts.
Directory Structure:
helm-values-demo/
โโโ charts/
โ โโโ myapp/
โ โโโ templates/
โ โ โโโ deployment.yaml
โ โ โโโ configmap.yaml
โ โโโ values.yaml
โ โโโ Chart.yaml
โโโ scripts/
โโโ pre-install.sh
1. Define variables in values.yaml:
# charts/myapp/values.yaml
appName: myapp
replicaCount: 1
image:
repository: nginx
tag: 1.14.2
databaseUrl: "https://example.com"2. Access variables in templates:
# charts/myapp/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.appName }}
spec:
replicas: {{ .Values.replicaCount }}
template:
metadata:
labels:
app: {{ .Values.appName }}
spec:
containers:
- name: {{ .Values.appName }}
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
env:
- name: DATABASE_URL
value: {{ .Values.databaseUrl }}
---
# charts/myapp/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Values.appName }}-config
data:
databaseUrl: {{ .Values.databaseUrl }}3. Override variables during installation:
helm install myapp charts/myapp --set replicaCount=3 --set image.tag=1.15.04. Use environment variables in values.yaml (not recommended):
# charts/myapp/values.yaml
# ... other values ...
replicaCount: {{ env "REPLICA_COUNT" | default "1" }}5. Pass environment variables as arguments:
export REPLICA_COUNT=3
helm install myapp charts/myapp --set replicaCount=$REPLICA_COUNT6. Use ConfigMaps and Secrets for sensitive data:
# charts/myapp/templates/deployment.yaml
# ... other parts of the deployment ...
env:
- name: DATABASE_URL
valueFrom:
configMapKeyRef:
name: {{ .Values.appName }}-config
key: databaseUrl7. Use Helm hooks for complex logic:
# charts/myapp/templates/deployment.yaml
# ... other parts of the deployment ...
template:
metadata:
annotations:
"helm.sh/hook": pre-install,pre-upgrade# scripts/pre-install.sh
#!/bin/bash
# Example: Check if database exists
echo "Checking if database exists..."
# ... your logic here ...Remember to make the script executable:
chmod +x scripts/pre-install.shThis example demonstrates various ways to manage values and variables in Helm charts. You can choose the approach that best suits your needs and project requirements.
Note: Using environment variables directly in values.yaml is generally not recommended as it can lead to unpredictable behavior and make your charts less portable.
General:
values.yaml files are crucial for maintainability. Use comments liberally to explain the purpose of each variable.values.yaml makes your chart easier to use. Users can then override only the values they need to change.Specific to Techniques:
--set, Helm offers other ways to override values, such as using -f or --values to provide a separate YAML file or using --set-string for values that need to be treated as strings.values.yaml is discouraged, environment variables can be helpful for dynamically configuring Helm commands themselves (e.g., setting the Kubernetes context or target namespace).Beyond the Basics:
default, quote, toYaml) that can help you manipulate and work with values effectively.This document outlines different methods for managing application configuration in Helm charts.
1. Defining Variables in values.yaml:
values.yaml to store default configuration values for your application.appName: myapp).2. Accessing Variables in Templates:
{{ .Values.<variableName> }} syntax.3. Overriding Variables During Installation:
helm install using the --set flag.helm install myapp . --set replicaCount=3 --set image.tag=1.15.0
4. Using Environment Variables (Not Recommended):
values.yaml is discouraged due to potential security risks and reduced portability.5. Passing Environment Variables as Arguments:
--set flag during installation.6. Utilizing ConfigMaps and Secrets:
valueFrom and configMapKeyRef or secretKeyRef.7. Leveraging Helm Hooks:
"helm.sh/hook": pre-install.By employing these techniques, you can effectively manage and customize your application deployments with Helm.
Helm charts are a powerful tool for managing Kubernetes applications, and understanding how to effectively use values and variables is key to unlocking their full potential. By defining default values, overriding them when needed, and leveraging techniques like ConfigMaps, Secrets, and Helm hooks, you can create flexible, reusable, and secure Helm charts for your applications. Remember to prioritize clarity, organization, and thorough testing to ensure your charts are maintainable and behave as expected in different environments. As you delve deeper into the Helm ecosystem, explore advanced features like Helm plugins, templating functions, and chart repositories to further enhance your Kubernetes deployment workflows.
How to Manage Environment Variables in Helm Charts: A ... | Helm is renowned not only as a package manager for Kuberentes but also for its robust templating and customization capabilities. Theseโฆ
How to Use Environment Variables with Helm Charts | pNAP KB | Learn how to use environment variables with Helm charts to make deploying Kubernetes applications easy and efficient.
An Advanced API for Environment Variables in Helm Charts | by ... | Environment variables are, besides config files, a widespread way to configure applications deployed to Kubernetes. So any Helm chartโฆ
Using Environment variables in helm chart with config maps | by ... | Overview
Variables - Helm | Using variables in templates.
Pass environment variables into Helm Chart? | Jhooq | Jan 7, 2021 ... This command will set the replicaCount to 2. You can verify it using the following command -. 1kubectl get deployment.