Feedback

Chat Icon

Cloud-Native Microservices With Kubernetes - 2nd Edition

A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in Kubernetes

Introduction to Helm
94%

Creating, Testing, and Developing a Chart

Usually, you would use existing charts from public repositories, but in some cases, like when you are developing your own application, you may want to create the chart from scratch.

The general syntax for creating a chart is:

helm create [CHART_NAME]

Let’s create a chart called hello-world:

cd $HOME

# Create the chart
helm create hello-world

This will create a directory called hello-world with the following structure:

hello-world/
├── Chart.yaml
├── charts
├── templates
│   ├── NOTES.txt
│   ├── _helpers.tpl
│   ├── deployment.yaml
│   ├── hpa.yaml
│   ├── httproute.yaml
│   ├── ingress.yaml
│   ├── service.yaml
│   ├── serviceaccount.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

Under the templates directory, you’ll find the templates used to generate the Kubernetes manifests.

  • deployment.yaml — template for a Deployment. It declares how many replicas to run, the container image, probes, resources, and labels. Use it to describe the app’s lifecycle and rollout strategy.

  • service.yaml — template for a Service. It gives the Deployment a stable virtual IP and port.
    Use it to expose Pods inside the cluster (ClusterIP) or externally (LoadBalancer/NodePort).

  • ingress.yaml — template for an Ingress. It’s L7 HTTP routing via an Ingress controller (NGINX, etc.). Use it for basic host/path routing and TLS termination when you’re not using Gateway API.

  • serviceaccount.yaml — template for a ServiceAccount. Use it when your Pods need specific RBAC permissions or identity separation.

  • hpa.yaml — template for a HorizontalPodAutoscaler. Use it to scale the Deployment based on CPU, memory, or custom metrics.

  • httproute.yaml — template for an HTTPRoute (Gateway API). It defines how HTTP traffic is matched (hosts, paths, headers) and routed to backends. It’s optional. Include it when you use Gateway API for routing; otherwise stick with ingress.yaml.

  • NOTES.txt — post-install notes shown by Helm. Use it to print helpful URLs, commands, and next steps.

  • tests/ — Helm tests that validate the release after install/upgrade. Use them to assert readiness or basic responses.

Example: The following is a simple smoke test that checks if the application is reachable via HTTP after installation:

# tests/smoke-test.yaml
apiVersion: v1
kind: Pod
metadata:
  name: "{{ include "myapp.fullname" . }}-test"
  annotations:
    "helm.sh/hook": test
spec:
  restartPolicy: Never
  containers:
    - name: curl
      image: curlimages/curl:8.10.1
      args: ["-f", "http://{{ include "myapp.fullname" . }}.{{ .Release.Namespace }}.svc.cluster.local/healthz"]
  • _helpers.tpl — reusable template helpers (names, labels, common snippets). Use it to keep templates DRY and consistent.

Example:

{{- define "myapp.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end }}

The "myapp.name" helper function, when called, will return the name of the chart or the value of nameOverride from values.yaml, truncated to 63 characters and without a trailing hyphen.

It can be used in other templates like this:

metadata:
  name: {{ include "myapp.name" . }}

Another example is the fullname helper:

{{- define "hello-world.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

This helper function generates a full name for the release based on the release name (.Release.Name), the chart name (.Chart.Name), and any overrides specified in the values.yaml file.

  • values.yaml — default configuration values for the chart. It contains default configuration values for the chart, like the image name, image tag, number of replicas, service type, service port, ingress host, etc. Use it to override defaults during installation or upgrade.

Cloud-Native Microservices With Kubernetes - 2nd Edition

A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in Kubernetes

Enroll now to unlock all content and receive all future updates for free.