🐶
Kubernetes

Helm Multiple values.yaml Files: A Guide

By Jan on 02/06/2025

Learn how to effectively manage configuration complexity in Helm by using multiple values.yaml files to organize and override your application settings.

Helm Multiple values.yaml Files: A Guide

Table of Contents

Introduction

When deploying applications with Helm, you often need to provide different sets of values for various environments or purposes. Helm simplifies this process by allowing you to use multiple values files during installation or upgrades. You can achieve this by using the --values flag (or its shorthand -f) multiple times, followed by the path to each values file.

Step-by-Step Guide

You can use multiple values files with Helm by using the --values flag (or -f) multiple times.

helm install ./path --values ./internalValues.yaml --values ./secrets.yaml

Helm merges the values files in the order they are specified, with the later files overriding values from earlier files.

For example, if internalValues.yaml sets replicaCount: 2 and secrets.yaml sets replicaCount: 3, the final value for replicaCount will be 3.

This merging process is a "deep merge," meaning that nested values are merged recursively.

However, there are some cases where Helm's deep merge might not behave as expected, especially with lists.

To see the final rendered values after merging, you can use the helm template command with the --debug flag.

helm template -f values1.yaml -f values2.yaml mychart --debug 

This will print the rendered template to the console, allowing you to verify the final values used.

Code Example

This code demonstrates how to deploy a simple web application using a Helm chart with multiple values files. It showcases how Helm merges values from different files, with later files overriding earlier ones. The example also includes how to use Helm's debug mode to view the final rendered template with merged values, ensuring the application is deployed with the desired configuration.

Let's imagine we have a simple Helm chart for deploying a web application.

Chart Structure:

mychart/
├── Chart.yaml
├── values.yaml
└── templates
    └── deployment.yaml

values.yaml:

replicaCount: 1
image:
  repository: nginx
  tag: stable
resources: {}

templates/deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}-web
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}-web
    spec:
      containers:
      - name: web
        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        resources:
          {{ toYaml .Values.resources | indent 10 }}

Now, let's create two values files:

internalValues.yaml:

replicaCount: 2
resources:
  requests:
    memory: "64Mi"
    cpu: "250m"

secrets.yaml:

image:
  tag: 1.23.2
resources:
  limits:
    memory: "128Mi"
    cpu: "500m"

Installation and Debugging:

  1. Install the chart with both values files:
helm install myapp ./mychart --values ./internalValues.yaml --values ./secrets.yaml
  1. View the rendered template with debug mode:
helm template -f ./internalValues.yaml -f ./secrets.yaml myapp ./mychart --debug

This will output the final rendered deployment manifest, showing the merged values:

---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-web
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: myapp-web
    spec:
      containers:
      - name: web
        image: nginx:1.23.2
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
          requests:
            memory: "64Mi"
            cpu: "250m"

As you can see, the final values are a result of merging values.yaml, internalValues.yaml, and secrets.yaml, with later files overriding earlier ones. The resources section demonstrates the deep merge functionality, combining values from both internalValues.yaml and secrets.yaml.

This example demonstrates how to use multiple values files and debug the final rendered template to ensure your application is deployed with the desired configuration.

Additional Notes

Best Practices:

  • Organization: Use separate values files for different purposes, such as environment-specific settings (production.yaml, staging.yaml), component configurations (database.yaml, frontend.yaml), and secrets (secrets.yaml).
  • Naming Conventions: Adopt a consistent naming scheme for values files to improve readability and maintainability.
  • Secrets Management: Avoid storing sensitive information directly in values files. Utilize Helm secrets plugins or external secrets management solutions like HashiCorp Vault for secure handling of secrets.

Advanced Techniques:

  • Values from Standard Input: You can pipe values from standard input using -f -, allowing for dynamic value generation.
  • Programmatic Value Manipulation: Use tools like yq or kustomize to programmatically modify values files before passing them to Helm.
  • Conditional Logic in Templates: Leverage Go template functions like if, range, and with to conditionally include or modify resources based on values.

Caveats:

  • List Merging Behavior: Helm's deep merge for lists appends elements by default. Use strategic merge patches or custom logic in templates for more fine-grained control over list merging.
  • Value Overrides: Be mindful of the order of values files, as later files override earlier ones. Ensure that overrides are intentional and do not introduce unintended consequences.

Troubleshooting:

  • Unexpected Merge Results: Use helm template --debug to inspect the final rendered template and identify any unexpected value merging outcomes.
  • Conflicting Values: Helm may report errors if conflicting values are detected. Review the error messages and adjust the values files accordingly.

By understanding these concepts and best practices, you can effectively leverage multiple values files to manage complex Helm deployments and tailor configurations for different environments and scenarios.

Summary

Feature Description
Multiple Values Files Use multiple values files with helm install or helm template by specifying the --values (or -f) flag multiple times.
Merging Order Values are merged in the order they are specified, with later files overriding earlier ones.
Deep Merge Merging is recursive, applying to nested values within the files.
Unexpected Behavior Deep merge might not always behave as expected, especially with lists.
Debugging Merged Values Use helm template -f values1.yaml -f values2.yaml mychart --debug to print the final rendered template and verify the merged values.

Conclusion

In conclusion, Helm's ability to use multiple values files provides flexibility and organization for managing application deployments across different environments. By understanding the merging process, leveraging best practices, and utilizing debugging techniques, developers can harness the full potential of Helm's values management capabilities. However, it's crucial to be aware of potential caveats, especially with list merging, and to thoroughly test and verify the final rendered templates to ensure desired application configurations. As you delve deeper into Helm, exploring advanced techniques like programmatic value manipulation and conditional logic in templates can further enhance your deployment workflows. By mastering these concepts, you can streamline deployments, reduce errors, and confidently manage complex Kubernetes applications with Helm.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait