Bird
Raised Fist0
Kubernetesdevops~3 mins

Why Chart templates and values.yaml in Kubernetes? - Purpose & Use Cases

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
The Big Idea

What if you could deploy many app versions by changing just one small file instead of dozens?

The Scenario

Imagine you need to deploy the same app many times with small differences, like different colors or names, and you write a new config file each time by hand.

The Problem

Manually copying and changing config files is slow and easy to mess up. One wrong value can break the whole app, and updating many files takes forever.

The Solution

Chart templates with values.yaml let you write one flexible template and supply different settings in values.yaml files. This way, you reuse the same template safely and quickly for many deployments.

Before vs After
Before
apiVersion: v1
kind: Pod
metadata:
  name: myapp-blue
spec:
  containers:
  - name: app
    image: myimage:latest
    env:
    - name: COLOR
      value: blue
After
apiVersion: v1
kind: Pod
metadata:
  name: {{ .Values.name }}
spec:
  containers:
  - name: app
    image: {{ .Values.image }}
    env:
    - name: COLOR
      value: "{{ .Values.color }}"
What It Enables

You can deploy many app versions easily by just changing values.yaml, without touching the template code.

Real Life Example

A team deploys the same web app to test, staging, and production with different settings by using one Helm chart and separate values.yaml files for each environment.

Key Takeaways

Manual config copying is slow and error-prone.

Chart templates with values.yaml separate code from settings.

This makes deploying many app versions fast and safe.

Practice

(1/5)
1. What is the main purpose of the values.yaml file in a Helm chart?
easy
A. To store default configuration values for templates
B. To define Kubernetes resource limits
C. To write deployment scripts
D. To list all Kubernetes nodes

Solution

  1. Step 1: Understand Helm chart structure

    Helm charts use templates with placeholders to create Kubernetes manifests dynamically.
  2. Step 2: Role of values.yaml

    The values.yaml file provides default values for these placeholders, allowing customization without changing templates.
  3. Final Answer:

    To store default configuration values for templates -> Option A
  4. Quick Check:

    values.yaml = default settings [OK]
Hint: Remember: values.yaml holds default settings for templates [OK]
Common Mistakes:
  • Confusing values.yaml with deployment scripts
  • Thinking it defines resource limits directly
  • Assuming it lists Kubernetes nodes
2. Which of the following is the correct syntax to reference a value named replicaCount from values.yaml inside a Helm template?
easy
A. {{ .Values.replicaCount }}
B. {{ .replicaCount }}
C. {{ values.replicaCount }}
D. {{ .Config.replicaCount }}

Solution

  1. Step 1: Understand Helm template syntax

    Helm templates access values using the .Values object followed by the key name.
  2. Step 2: Match syntax for replicaCount

    The correct way is {{ .Values.replicaCount }} to get the value from values.yaml.
  3. Final Answer:

    {{ .Values.replicaCount }} -> Option A
  4. Quick Check:

    Use .Values.key to access values [OK]
Hint: Use .Values.key to get values in templates [OK]
Common Mistakes:
  • Omitting the .Values prefix
  • Using lowercase 'values' instead of .Values
  • Confusing .Config with .Values
3. Given this snippet in values.yaml:
replicaCount: 3
image:
  repository: nginx
  tag: stable
What will be the output of this Helm template snippet?
{{ .Values.replicaCount }} replicas of {{ .Values.image.repository }}:{{ .Values.image.tag }}
medium
A. replicaCount replicas of image.repository:image.tag
B. Error: undefined values
C. 3 replicas of nginx:latest
D. 3 replicas of nginx:stable

Solution

  1. Step 1: Read values.yaml keys and values

    replicaCount is 3, image.repository is 'nginx', and image.tag is 'stable'.
  2. Step 2: Substitute values in template

    The template outputs: '3 replicas of nginx:stable' by replacing placeholders with values.
  3. Final Answer:

    3 replicas of nginx:stable -> Option D
  4. Quick Check:

    Values replaced correctly = 3 replicas of nginx:stable [OK]
Hint: Match keys exactly to get correct output [OK]
Common Mistakes:
  • Using wrong tags like 'latest' instead of 'stable'
  • Not accessing nested keys properly
  • Expecting literal placeholders in output
4. You have this template snippet:
{{ if .Values.enableFeature }}Feature is enabled{{ else }}Feature is disabled{{ end }}
But the output always shows "Feature is disabled" even when you set enableFeature: true in values.yaml. What is the likely cause?
medium
A. The template syntax is incorrect and missing a closing tag
B. enableFeature is set as a string "true" instead of boolean true
C. The values.yaml file is not saved properly
D. Helm does not support boolean values in values.yaml

Solution

  1. Step 1: Check boolean handling in values.yaml

    YAML treats unquoted true as boolean, but quoted "true" is a string, which evaluates as true in some contexts but false in Helm conditionals.
  2. Step 2: Understand Helm conditional evaluation

    Helm expects boolean true, so if enableFeature is a string, the condition fails and goes to else.
  3. Final Answer:

    enableFeature is set as a string "true" instead of boolean true -> Option B
  4. Quick Check:

    Boolean true must be unquoted in values.yaml [OK]
Hint: Use unquoted true/false for booleans in values.yaml [OK]
Common Mistakes:
  • Quoting booleans as strings
  • Assuming template syntax error without checking values
  • Not saving values.yaml after changes
5. You want to create a Helm chart template that sets the container port only if service.port is defined in values.yaml. Which template snippet correctly implements this conditional logic?
hard
A. {{- if .Values.service.port }} containerPort: "{{ .Values.service.port }}" {{- else }} containerPort: 80 {{- end }}
B. {{- if .service.port }} containerPort: {{ .service.port }} {{- end }}
C. {{- if .Values.service.port }} containerPort: {{ .Values.service.port }} {{- end }}
D. {{- if .Values.service.port != null }} containerPort: {{ .Values.service.port }} {{- end }}

Solution

  1. Step 1: Identify correct value reference

    Use .Values.service.port to access the port value from values.yaml.
  2. Step 2: Use proper conditional syntax

    Helm templates use {{- if .Values.service.port }} to check if the value exists and is non-empty.
  3. Step 3: Evaluate options

    {{- if .Values.service.port }} containerPort: {{ .Values.service.port }} {{- end }} correctly uses the conditional and outputs the port only if defined. {{- if .service.port }} containerPort: {{ .service.port }} {{- end }} misses .Values. {{- if .Values.service.port }} containerPort: "{{ .Values.service.port }}" {{- else }} containerPort: 80 {{- end }} adds an else block which is not requested. {{- if .Values.service.port != null }} containerPort: {{ .Values.service.port }} {{- end }} uses invalid syntax (!= null is not valid in Helm templates).
  4. Final Answer:

    {{- if .Values.service.port }} containerPort: {{ .Values.service.port }} {{- end }} -> Option C
  5. Quick Check:

    Use if .Values.key for conditionals [OK]
Hint: Check existence with if .Values.key, no need for != null [OK]
Common Mistakes:
  • Omitting .Values prefix
  • Using invalid comparison operators
  • Adding unnecessary else blocks