0
0
KubernetesHow-ToBeginner · 4 min read

How to Create a Helm Chart for Kubernetes Applications

To create a Helm chart, use the command helm create <chart-name> which generates a chart template with necessary files. Customize the files like Chart.yaml and values.yaml to define your app and configuration before deploying it with helm install.
📐

Syntax

The basic command to create a Helm chart is:

  • helm create <chart-name>: Generates a new chart directory with default files.
  • Chart.yaml: Metadata about the chart like name, version, description.
  • values.yaml: Default configuration values for templates.
  • templates/: Folder containing Kubernetes manifest templates.
bash
helm create mychart
Output
Created directory mychart Created Chart.yaml Created values.yaml Created templates/deployment.yaml Created templates/service.yaml ... (other template files)
💻

Example

This example shows creating a Helm chart named myapp, customizing the values.yaml to set the app image, and installing it on Kubernetes.

bash
helm create myapp

# Edit myapp/values.yaml to set image.repository: nginx

helm install myapp-release ./myapp
Output
NAME: myapp-release LAST DEPLOYED: 2024-06-01 12:00:00 NAMESPACE: default STATUS: deployed REVISION: 1 NOTES: 1. Get the application URL by running these commands: export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=myapp,app.kubernetes.io/instance=myapp-release" -o jsonpath="{.items[0].metadata.name}") kubectl port-forward $POD_NAME 8080:80 Visit http://127.0.0.1:8080 to access your application.
⚠️

Common Pitfalls

Common mistakes when creating Helm charts include:

  • Not updating Chart.yaml metadata, causing confusion about chart identity.
  • Forgetting to customize values.yaml leading to default or incorrect app settings.
  • Incorrect indentation or syntax errors in YAML templates causing deployment failures.
  • Using hardcoded values in templates instead of referencing values.yaml.

Always validate your YAML files and test the chart with helm lint before installing.

yaml
### Wrong: Hardcoded image in deployment.yaml template
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  template:
    spec:
      containers:
      - name: myapp
        image: nginx:latest

### Right: Use values.yaml reference
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "myapp.fullname" . }}
spec:
  template:
    spec:
      containers:
      - name: myapp
        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
📊

Quick Reference

Helm Chart Creation Cheat Sheet:

Command/FilePurpose
helm create <name>Generate new chart scaffold
Chart.yamlChart metadata (name, version)
values.yamlDefault config values for templates
templates/Kubernetes manifest templates
helm install <release> <chart>Deploy chart to cluster
helm lint <chart>Check chart for errors
Command/FilePurpose
helm create Generate new chart scaffold
Chart.yamlChart metadata (name, version)
values.yamlDefault config values for templates
templates/Kubernetes manifest templates
helm install Deploy chart to cluster
helm lint Check chart for errors

Key Takeaways

Use 'helm create ' to generate a chart scaffold quickly.
Customize 'Chart.yaml' and 'values.yaml' to define your app and configuration.
Always use template references to 'values.yaml' instead of hardcoding values.
Run 'helm lint' to catch errors before installing your chart.
Install your chart with 'helm install ' to deploy on Kubernetes.