๐Ÿถ
Kubernetes

Helm Charts: Guide to Pulling Environment Variables

By Jan on 02/02/2025

Learn how to effectively manage and inject environment variables into your Kubernetes deployments using Helm charts.

Helm Charts: Guide to Pulling Environment Variables

Table of Contents

Introduction

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.

Step-by-Step Guide

  1. Define variables in values.yaml:

    # values.yaml
    appName: myapp
    replicaCount: 1
    image:
      repository: nginx
      tag: 1.14.2
  2. Access 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 }}
  3. Override variables during installation:

    helm install myapp . --set replicaCount=3 --set image.tag=1.15.0
  4. Use environment variables in values.yaml (not recommended):

    # values.yaml
    appName: myapp
    replicaCount: {{ env "REPLICA_COUNT" | default "1" }}
  5. Pass environment variables as arguments:

    export REPLICA_COUNT=3
    helm install myapp . --set replicaCount=$REPLICA_COUNT
  6. Use 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: databaseUrl
  7. Use Helm hooks for complex logic:

    # deployment.yaml
    spec:
      template:
        metadata:
          annotations:
            "helm.sh/hook": pre-install,pre-upgrade

    This allows you to execute scripts or commands at different stages of the Helm lifecycle.

Code Example

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.0

4. 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_COUNT

6. 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: databaseUrl

7. 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.sh

This 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.

Additional Notes

General:

  • Clarity and Organization: Well-defined and organized values.yaml files are crucial for maintainability. Use comments liberally to explain the purpose of each variable.
  • Defaults: Providing sensible defaults in values.yaml makes your chart easier to use. Users can then override only the values they need to change.
  • Testing: Test your Helm charts thoroughly, including different value combinations, to ensure they behave as expected in various scenarios.

Specific to Techniques:

  • Overriding Values:
    • Besides --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.
  • Environment Variables:
    • While direct use in values.yaml is discouraged, environment variables can be helpful for dynamically configuring Helm commands themselves (e.g., setting the Kubernetes context or target namespace).
  • ConfigMaps and Secrets:
    • For sensitive data, always use Secrets. Never store sensitive information directly in ConfigMaps.
    • Consider using external Secrets management solutions for enhanced security.
  • Helm Hooks:
    • Hooks provide great flexibility but can also increase complexity. Use them judiciously and document their purpose clearly.
    • Explore the different types of hooks available (pre-install, post-install, pre-upgrade, etc.) to find the one that best suits your needs.

Beyond the Basics:

  • Helm Plugins: Extend Helm's functionality with plugins. Some plugins offer advanced ways to manage values and configurations.
  • Templating Functions: Helm's templating language provides many built-in functions (e.g., default, quote, toYaml) that can help you manipulate and work with values effectively.
  • Chart Repositories: Package and share your Helm charts in repositories (e.g., Artifactory, Harbor) for easier distribution and version management.

Summary

This document outlines different methods for managing application configuration in Helm charts.

1. Defining Variables in values.yaml:

  • Use values.yaml to store default configuration values for your application.
  • Define variables using YAML syntax (e.g., appName: myapp).

2. Accessing Variables in Templates:

  • Access variables within your Kubernetes YAML templates using the {{ .Values.<variableName> }} syntax.

3. Overriding Variables During Installation:

  • Override default values during helm install using the --set flag.
  • Example: helm install myapp . --set replicaCount=3 --set image.tag=1.15.0

4. Using Environment Variables (Not Recommended):

  • While possible, directly referencing environment variables in values.yaml is discouraged due to potential security risks and reduced portability.

5. Passing Environment Variables as Arguments:

  • A safer approach is to pass environment variables as arguments to the --set flag during installation.

6. Utilizing ConfigMaps and Secrets:

  • Store sensitive data like database credentials in ConfigMaps and Secrets.
  • Reference these values within your deployments using valueFrom and configMapKeyRef or secretKeyRef.

7. Leveraging Helm Hooks:

  • Execute custom scripts or commands at specific stages of the Helm lifecycle (e.g., pre-install, pre-upgrade) using Helm hooks.
  • Define hooks within your templates using annotations like "helm.sh/hook": pre-install.

By employing these techniques, you can effectively manage and customize your application deployments with Helm.

Conclusion

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.

References

Were You Able to Follow the Instructions?

๐Ÿ˜Love it!
๐Ÿ˜ŠYes
๐Ÿ˜Meh-gical
๐Ÿ˜žNo
๐ŸคฎClickbait