Helm Charts: Templating, Values, and Hooks
Build Helm charts with values, templates, dependencies, hooks, and schema validation. Examples target Helm v4.3.1 and focus on chart authoring.
A Helm chart packages Kubernetes manifests as a reusable unit. This guide covers chart structure, values, templates, dependencies, hooks, testing, and schema validation with examples for Helm v4.3.1. For repository publishing, chart security, and release or rollback operations, see the dedicated Helm guides.
Helm Charts: Templating, Values, and Hooks
Helm charts package related Kubernetes manifests with the values and templates needed to adapt them to different environments. This guide focuses on chart authoring. Commands and examples target Helm v4.3.1.
If you are new to Kubernetes, start with our Docker Fundamentals and Advanced Kubernetes guides first.
Introduction
The examples use the current chart format (apiVersion: v2). Helm 4 retains this chart API version; the Helm version and chart API version are separate things.
Chart Structure
A chart is a collection of files organized in a specific directory structure:
my-chart/
Chart.yaml # Chart metadata
values.yaml # Default configuration values
values.schema.json # Optional JSON schema validation
charts/ # Downloaded chart dependencies
templates/ # Kubernetes manifest templates
templates/NOTES.txt # Post-install notes
.helmignore # Files to ignore during packaging
Chart.yaml
The Chart.yaml contains the chart’s metadata:
apiVersion: v2
name: my-application
description: A Helm chart for my production application
type: application
version: 1.2.3
appVersion: "2.0.0"
kubeVersion: ">=1.24.0"
keywords:
- web application
- api
home: https://github.com/example/my-app
sources:
- https://github.com/example/my-app
maintainers:
- name: DevOps Team
email: devops@example.com
dependencies:
- name: postgresql
version: "12.8.0"
repository: "https://charts.bitnami.com"
condition: postgresql.enabled
- name: redis
version: "17.0.0"
repository: "https://charts.bitnami.com"
condition: redis.enabled
apiVersion identifies the chart format, not the Helm CLI version. Helm v4.3.1 uses chart format v2.
Values Files
Values files provide configuration that gets merged into templates. Helm uses a cascading values system: default values in values.yaml, overridden by environment-specific files, overridden by command-line flags.
values.yaml
# Default configuration
replicaCount: 3
image:
repository: myregistry/myapp
pullPolicy: IfNotPresent
tag: "1.0.0"
service:
type: ClusterIP
port: 8080
ingress:
enabled: true
className: nginx
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
hosts:
- host: api.example.com
paths:
- path: /
pathType: Prefix
tls:
- secretName: myapp-tls
hosts:
- api.example.com
resources:
limits:
cpu: 1000m
memory: 1Gi
requests:
cpu: 100m
memory: 256Mi
autoscaling:
enabled: true
minReplicas: 3
maxReplicas: 10
targetCPUUtilizationPercentage: 70
postgresql:
enabled: true
auth:
database: myapp
username: myapp
primary:
persistence:
size: 10Gi
redis:
enabled: true
auth:
password: ""
Environment-Specific Values
Create environment-specific value files:
# values-staging.yaml
replicaCount: 2
image:
tag: "1.0.0-staging"
resources:
limits:
cpu: 500m
memory: 512Mi
autoscaling:
enabled: false
ingress:
annotations:
cert-manager.io/cluster-issuer: letsencrypt-staging
hosts:
- host: staging-api.example.com
# values-prod.yaml
replicaCount: 5
image:
tag: "1.0.0-production"
resources:
limits:
cpu: 2000m
memory: 2Gi
autoscaling:
minReplicas: 5
maxReplicas: 20
Deploy with specific values:
helm upgrade --install myapp ./charts/myapp \
--values values-prod.yaml \
--namespace production \
--create-namespace
Template Functions and Pipelines
Helm templates use Go template syntax with Sprig functions for manipulation.
Common Functions
# templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "myapp.fullname" . }}
labels:
{{- include "myapp.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{- include "myapp.selectorLabels" . | nindent 6 }}
template:
metadata:
labels:
{{- include "myapp.selectorLabels" . | nindent 8 }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: {{ .Values.service.port }}
protocol: TCP
livenessProbe:
httpGet:
path: /health
port: http
readinessProbe:
httpGet:
path: /ready
port: http
{{- with .Values.resources }}
resources:
{{- toYaml . | nindent 12 }}
{{- end }}
String Functions
# Uppercase
appName: { { .Values.name | upper } }
# Lowercase
envVar: { { .Values.env | lower | squote } }
# Truncate
shortName: { { .Values.name | trunc 63 } }
# Replace
fixedName: { { .Values.name | replace "_" "-" } }
# Quote
quoted: { { .Values.name | quote } }
# Default value
tag: { { .Values.image.tag | default .Chart.AppVersion } }
Conditional Logic
# Ternary operator
replicas: {{ .Values.replicaCount | default 1 }}
# If-else blocks
{{- if .Values.ingress.enabled }}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ include "myapp.fullname" . }}
spec:
{{- with .Values.ingress }}
ingressClassName: {{ .className }}
{{- end }}
{{- end }}
Range Loops
# Loop over list
env:
{{- range .Values.env }}
- name: {{ .name }}
value: {{ .value | quote }}
{{- end }}
# Loop over key-value map
labels:
{{- range $key, $value := .Values.labels }}
{{ $key }}: {{ $value | quote }}
{{- end }}
# Loop with index
{{- range $i, $v := .Values.replicas }}
- name: replica-{{ $i }}
{{- end }}
Named Templates
Named templates (partials) live in templates/_helpers.tpl and you can include them throughout your chart.
Defining Helpers
# templates/_helpers.tpl
{{/*
Expand the name of the chart.
*/}}
{{- define "myapp.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}
{{/*
Full name including release and chart.
*/}}
{{- define "myapp.fullname" -}}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{/*
Common labels.
*/}}
{{- define "myapp.labels" -}}
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
{{ include "myapp.name" . }}: {{ .Release.Name }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app.kubernetes.io/part-of: {{ include "myapp.name" . }}
{{- end }}
{{/*
Selector labels.
*/}}
{{- define "myapp.selectorLabels" -}}
app.kubernetes.io/name: {{ include "myapp.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
Using Helpers in Templates
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "myapp.fullname" . }}
labels:
{{- include "myapp.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{- include "myapp.selectorLabels" . | nindent 6 }}
Chart Dependencies
Charts can depend on other charts. Define dependencies in Chart.yaml and download them to the charts/ directory.
Dependency Management
# Chart.yaml
dependencies:
- name: postgresql
version: "12.8.0"
repository: "https://charts.bitnami.com"
condition: postgresql.enabled
- name: redis
version: "17.0.0"
repository: "https://charts.bitnami.com"
condition: redis.enabled
Update dependencies:
helm dependency update ./charts/myapp
This creates a Chart.lock file that locks dependency versions.
Sub-chart Values
Parent charts can override sub-chart values:
# values.yaml
postgresql:
primary:
persistence:
size: 50Gi
auth:
database: myapp_prod
username: myapp
redis:
master:
persistence:
enabled: false
Hooks
Hooks are chart resources that run at named points in a release lifecycle, such as pre-install, post-upgrade, or test. They can be useful for a migration Job, but keep the work idempotent: an upgrade or retry may run it again.
For a hook Job, set an explicit cleanup policy. Helm does not track hook resources as part of the release, so helm uninstall will not remove them automatically. The example clears an older Job before a later run and lets Kubernetes delete the completed Job after ten minutes.
# templates/migrations.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "myapp.fullname" . }}-migrations
annotations:
"helm.sh/hook": post-install,post-upgrade
"helm.sh/hook-delete-policy": before-hook-creation
spec:
ttlSecondsAfterFinished: 600
template:
spec:
restartPolicy: Never
containers:
- name: migrations
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
command:
- /app/migrate.sh
helm.sh/hook-weight sorts hook resources for the same lifecycle event. Lower values run first; the default is 0. It does not order hooks against ordinary chart resources or control the order of regular ConfigMaps, Deployments, and other manifests. For example, a pre-upgrade hook runs before Helm updates ordinary resources, regardless of its weight. If a migration needs configuration from a ConfigMap, arrange that dependency inside the hook’s own work or use a lifecycle point where the ConfigMap already exists.
To delete a successful Job immediately, add hook-succeeded to the delete policy and omit the TTL. Review Helm’s hook lifecycle and cleanup behavior before adding hooks to a chart.
Testing Charts
Helm includes a test framework for validating chart installations.
Test Pod
# templates/tests/pod-test.yaml
apiVersion: v1
kind: Pod
metadata:
name: {{ include "myapp.fullname" . }}-test-connection
labels:
{{- include "myapp.labels" . | nindent 4 }}
annotations:
"helm.sh/hook": test
"helm.sh/hook-delete-policy": hook-succeeded
spec:
restartPolicy: Never
containers:
- name: wget
image: busybox:1.36
command:
- wget
args:
- '-O-'
- 'http://{{ include "myapp.fullname" . }}:{{ .Values.service.port }}/health'
Run tests:
helm test myapp --namespace staging
Chart Repositories
Chart packaging, publishing, repository authentication, and OCI distribution are covered in Helm Repository Management. This guide focuses on building a chart that renders and validates correctly.
Library Charts
Library charts provide reusable templates that other charts can include. They are useful for standardizing common patterns across your organization.
Library Chart Structure
library-chart/
Chart.yaml
templates/
_deployment.yaml
_service.yaml
_configmap.yaml
# Chart.yaml
apiVersion: v2
name: myapp-library
type: library
version: 1.0.0
Using Library Chart
# In your application chart
dependencies:
- name: myapp-library
version: "1.0.0"
repository: "https://charts.example.com"
import-values:
- data
JSON Schema Validation
Values schema files validate values provided to your chart:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"replicaCount": {
"type": "integer",
"minimum": 1,
"maximum": 10
},
"image": {
"type": "object",
"properties": {
"repository": { "type": "string" },
"tag": { "type": "string" },
"pullPolicy": {
"type": "string",
"enum": ["IfNotPresent", "Always", "Never"]
}
},
"required": ["repository"]
},
"service": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["ClusterIP", "LoadBalancer", "NodePort"]
},
"port": {
"type": "integer",
"minimum": 1,
"maximum": 65535
}
},
"required": ["type", "port"]
}
},
"required": ["image", "service"]
}
If a user provides invalid configuration, Helm rejects it during template rendering:
helm template myapp ./charts/myapp --values invalid-values.yaml
# Error: values don't meet the schema
Best Practices
A chart is easier to use when its values interface is small, documented, and checked before rendering.
| Area | Authoring practice |
|---|---|
| Values | Group related settings, provide safe defaults, and document constraints in values.yaml. |
| Validation | Add values.schema.json for required fields, types, and allowed ranges. |
| Templates | Put repeated names and labels in _helpers.tpl; keep generated Kubernetes objects explicit. |
| Dependencies | Pin dependency versions and commit Chart.lock for repeatable builds. |
| Hooks | Make Jobs safe to retry, order only hooks for the same event, and define how their resources are cleaned up. |
| Metadata | Use version for the chart and appVersion for the packaged application version. |
Do not put real credentials in chart defaults. For chart distribution, signing, registry access, and deployment security, see Helm Repository Management.
When to Use Helm
Helm fits applications that need reusable Kubernetes manifests with environment-specific values, or a package that can be shared across teams. For a single stable Deployment and Service, plain YAML may be simpler. GitOps tools such as Argo CD and Flux can render Helm charts as part of their desired-state workflow; test that workflow with the chart values and plugins your team plans to use.
Release Operations
This guide stops at chart authoring. For publishing and securing chart repositories, see Helm Repository Management. For chart release versioning, failed deployments, and rollback procedures, see Helm Versioning and Rollback.
Common Pitfalls / Anti-Patterns
Template Pitfalls
Overusing toYaml
# Anti-pattern: Unbounded toYaml
spec:
{{- toYaml .Values.podSpec | nindent 8 }}
# Better: Explicitly define expected fields
spec:
replicas: {{ .Values.replicaCount }}
selector:
{{- include "myapp.selectorLabels" . | nindent 6 }}
Not handling nil values
# Anti-pattern: Will fail if .Values.ingress is nil
annotations:
{{- .Values.ingress.annotations | toYaml | nindent 8 }}
# Better: Use conditional checks
{{- with .Values.ingress }}
annotations:
{{- .annotations | toYaml | nindent 8 }}
{{- end }}
Forgetting the minus sign for whitespace control
# Anti-pattern: Extra blank line before content
spec:
containers:
- name: app
image: { { .Values.image } }
# Better: Hyphen trims preceding whitespace
spec:
containers:
- name: app
image: { { .Values.image } }
Values Structure Pitfalls
Flat values that should be nested
# Anti-pattern: Flat structure makes templating complex
replicaCount: 3
imageRepository: myapp
imageTag: "1.0.0"
servicePort: 8080
# Better: Group related values
replicaCount: 3
image:
repository: myapp
tag: "1.0.0"
service:
type: ClusterIP
port: 8080
Not using JSON schema validation Without schema validation, users can pass any values causing cryptic template errors at render time.
Hook Pitfalls
Do not use hook weights to order a hook against a regular ConfigMap, Deployment, or other chart template. Weights only sort hook resources for the same lifecycle event. Hook resources also outlive release management unless you define cleanup; use an explicit helm.sh/hook-delete-policy or a Job TTL. The migration example above shows both a rerun policy and a TTL.
Hook Jobs should be safe to retry. A migration should detect already-applied changes rather than creating duplicate state each time an upgrade or retry runs.
Dependency Pitfalls
Not pinning versions
# Anti-pattern: Version range can cause unexpected behavior
dependencies:
- name: postgresql
version: ">=12.0.0"
# Better: Pin exact version for reproducibility
dependencies:
- name: postgresql
version: "12.8.0"
Circular dependencies Avoid charts that depend on each other. This causes installation and upgrade ordering issues.
Chart Authoring Checklist
Before sharing a chart, check that:
values.yamldocuments the settings chart users are expected to change.values.schema.jsoncatches invalid or missing values.- Helpers keep repeated names and labels consistent.
- Dependency versions and
Chart.lockare committed. - Hooks use the right lifecycle event, are safe to retry, and have a cleanup policy.
- Rendering and linting pass with representative values.
helm dependency build ./charts/myapp
helm lint ./charts/myapp --strict
helm template myapp ./charts/myapp --values values-prod.yaml
helm template renders locally; it does not ask the cluster to validate API support or live resource state. After installing the chart in a test namespace, run any chart tests with helm test myapp --namespace staging.
Interview Questions
Chart defaults are overridden by supplied values files, with later files taking precedence. Command-line values such as --set take higher precedence. Keep environment differences in values files rather than maintaining separate copies of the chart.
_helpers.tpl?Reusable named templates such as chart names, full names, common labels, and selector labels. Include them with the right context and indent their output with nindent.
It sorts hook resources for the same lifecycle event. Lower weights run first. A weight does not set the order of ordinary chart resources; hooks are also not managed as part of the release, so their cleanup must be planned separately.
values.schema.json help chart users?It checks values for types, required fields, ranges, and allowed strings before templates are rendered. This turns many confusing template failures into a direct validation error.
A library chart supplies reusable templates to application charts. It uses type: library and does not install Kubernetes resources on its own.
Chart.lock record?It records the resolved chart dependency versions. Commit the file and use helm dependency build to recreate the locked dependencies without resolving ranges again.
Run helm lint --strict and render representative values with helm template. Local rendering cannot verify cluster API support or live resources, so also test in a suitable Kubernetes environment.
Further Reading
- Helm 4.3.1 chart hooks
- Helm 4.3.1
helm lint - Helm 4.3.1
helm template - Helm Repository Management
- Helm Versioning and Rollback
Conclusion
Start with the values users need to change and render the chart with representative configurations. Move repeated naming or labels into helpers once you see duplication. For hooks, define how retries work and when Kubernetes should clean up the resource.
Category
Related Posts
Developing Helm Charts: Templates, Values, and Testing
Create production-ready Helm charts with Go templates, custom value schemas, and testing using Helm unittest and ct.
Helm Versioning and Rollback: Safe Recovery
Learn how Helm revisions work, when to roll back, and how to choose a forward fix or data restore when Kubernetes changes affect a database.
Advanced Kubernetes: A Guide to Production Decisions
Choose when to use controllers, network policies, configuration, scheduling, storage, and availability patterns, with versioned checks and focused guides.