Helm chart for K8 deployment

Dilip Kumar
6 min readFeb 15, 2025

--

1.0 What is helm chart?

Helm is a package manager for Kubernetes. Just like apt, yum, or Homebrew manage software packages on operating systems, Helm manages Kubernetes applications. It simplifies the deployment and management of applications on Kubernetes clusters.

2.0 Helm Architecture

2.1 Helm Client (helm)

  • The command-line tool used to interact with Helm.
  • Handles chart creation, installation, upgrades, and rollbacks.
  • Communicates with the Kubernetes API server.

2.2 Helm Charts

  • Packages containing pre-configured Kubernetes resources.
  • Defined using YAML files and Go templates.
  • Contain a Chart.yaml file (metadata), values.yaml (default configuration), and templates/ directory (manifest templates).

2.3 Helm Releases

  • An instance of a chart running in a Kubernetes cluster.
  • Each installation of a chart creates a new release.
  • Releases are tracked and versioned by Helm.

2.4 Helm Repositories

  • HTTP servers that store and distribute Helm charts.
  • Public repositories (e.g., Artifact Hub) offer a wide range of pre-built charts.
  • Private repositories can be used to store internal charts.

3.0 Helm Chart Structure

A Helm chart is essentially a directory containing a set of files that describe a Kubernetes application. These files work together to define the application’s resources and configuration.

Following are key components.

3.1 Chart.yaml

This is the metadata file for the chart. It contains information about the chart, such as its name, version, description, and maintainer. Following is one example.

apiVersion: v2
name: my-nginx
description: A basic Nginx web server
type: application
version: 0.1.0
appVersion: "1.21.0"
  • apiVersion: The API version of the chart (e.g., v2).
  • name: The name of the chart.
  • version: The semantic version of the chart.
  • appVersion: The version of the application being deployed.
  • description: A brief description of the chart.
  • type: The type of chart, application, or library.

3.2 values.yaml

This file contains the default configuration values for the chart. These values can be overridden during chart installation or upgrade. It uses a hierarchical structure to organize values. Following is one example.

replicaCount: 1
image:
repository: nginx
tag: stable
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 80

3.3 templates/ Directory

This directory contains the Kubernetes manifest templates. These templates are written in YAML and use Go templating syntax to dynamically generate Kubernetes resources. Helm uses the values from values.yaml to fill in the template variables.

Common template files:

  • deployment.yaml: Defines a Kubernetes Deployment.
  • service.yaml: Defines a Kubernetes Service.
  • configmap.yaml: Defines a Kubernetes ConfigMap.
  • _helpers.tpl: contains named templates that can be reused.

Example templates/deployment.yaml snippet:

apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-deployment
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Release.Name }}
template:
metadata:
labels:
app: {{ .Release.Name }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"

3.4 charts/ Directory (Optional)

This directory is used to store subcharts (dependent charts). Subcharts allow you to include other charts as dependencies within your main chart.

3.5 README.md (Optional)

A documentation file that provides information about the chart.

3.6 .helmignore (Optional)

A file similar to .gitignore that specifies files and directories to exclude when packaging the chart.

3.7 How it all works together

  1. When you install a Helm chart, Helm reads the Chart.yaml file to get the chart's metadata.
  2. It then merges the values from values.yaml with any values you provide during installation.
  3. Helm uses the Go templating engine to process the templates in the templates/ directory, substituting the values from the merged configuration.
  4. The resulting YAML manifests are then applied to your Kubernetes cluster, creating the application resources.

4.0 Installing and Managing Charts

4.1 helm install

  1. This command is used to install a Helm chart into a Kubernetes cluster.

2. It creates a new release of the chart.

3. Syntax: helm install [RELEASE_NAME] [CHART] [flags]

  • RELEASE_NAME: A unique name for the release.
  • CHART: The path to the chart directory, a chart name from a repository, or a URL to a chart.
  • flags: Options to customize the installation (e.g., -n namespace, -f values.yaml).

4. Examples:

  • helm install my-nginx ./my-nginx-chart: Installs a chart from a local directory.
  • helm install stable-nginx bitnami/nginx: Installs a chart from the Bitnami repository.
  • helm install my-release my-repo/my-chart -n my-namespace -f my-values.yaml: Installs a chart from a repo, in a specific namespace, using a custom values file.

4.2 helm upgrade

The helm upgrade command is used to update an existing Helm release with a new version of the chart or with updated configuration values. It's a fundamental command for managing the lifecycle of your deployed applications.

Syntax

helm upgrade [RELEASE_NAME] [CHART] [flags]

Following is one example.

helm upgrade my-app ./my-new-app-chart

4.3 helm list

  1. This command lists all the Helm releases in a namespace (or all namespaces).

2. It provides information about the release name, namespace, revision, status, chart, and app version.

3. Syntax: helm list [flags]

4. Flags:

  • -A or --all-namespaces: Lists releases in all namespaces.
  • -n namespace: Lists releases in a specific namespace.

5. Output:

  • Displays a table with release details.

4.4 helm uninstall

  1. This command uninstalls a Helm release from the Kubernetes cluster.

2. It removes all the resources created by the chart.

3. Syntax: helm uninstall [RELEASE_NAME] [flags]

4. Flags:

  • -n namespace: Uninstalls a release from a specific namespace.
  • Example: helm uninstall my-nginx -n my-namespace

5.0 Helm Template Language

Helm charts utilize the Go templating language to create dynamic Kubernetes manifest files. This allows for customization and flexibility when deploying applications.

  • Helm’s templating engine is based on Go’s text/template library.
  • This means you can use Go template syntax within your YAML files to insert variables, perform conditional logic, and iterate over lists.

5.1 Template Syntax

  • {{ ... }}: Used to execute actions or print values.
  • .: Represents the current scope or context.
  • .Values: Accesses values defined in values.yaml.
  • .Release: Provides information about the Helm release (e.g., .Release.Name, .Release.Namespace).
  • .Chart: Provides information about the chart itself (e.g., .Chart.Name, .Chart.Version).

5.2 Variables

  • You can access values from values.yaml using dot notation.
  • Example: {{ .Values.image.repository }}

5.3 Conditionals

  • You can use if, else if, and else statements to conditionally include or exclude parts of your templates.
  • Example:
{{ if .Values.service.enabled }}
apiVersion: v1
kind: Service
# ...
{{ end }}

5.3 Loops

  • You can use range loops to iterate over lists or maps.
  • Example:
{{- range .Values.extraVolumes }}
- name: {{ .name }}
emptyDir: {}
{{- end }}

5.4 Built-in Helm Template Functions

Helm provides a set of built-in functions that you can use in your templates.

Examples:

  • include: Includes another template.
  • toYaml: Converts a value to YAML format.
  • default: Provides a default value if a value is not set.
  • quote: adds quotes around a string.

5.5 Context

The “dot” (.) is very important. It represents the current context. That context changes depending on where you are in the template. When you start, the “dot” represents the top level of the values being passed into the template. When you are inside of a range loop, the “dot” represents the current item in the iteration.

6.0 Configure Helm repo

6.1 Add the Repository

  1. Use the helm repo add command to add the repository to your Helm client.

2. Syntax: helm repo add <repo_name> <repo_url>

  • <repo_name>: A name you choose for the repository.
  • <repo_url>: The URL of the Helm repository.

3. Example:

6.2 Update the Repository Index

  • After adding a repository, you need to update the local index of available charts.
  • Use the helm repo update command.
  • This command fetches the latest chart information from all configured repositories.
  • Syntax: helm repo update

6.3 Search for Charts (Optional):

  • You can search for charts within the added repositories using the helm search repo command.
  • Syntax: helm search repo <search_term>
  • Example: helm search repo nginx

6.4 Install a Chart

  1. Once the repository is added and updated, you can install charts from it using the helm install command.

2. Syntax: helm install <release_name> <repo_name>/<chart_name> [flags]

  • <release_name>: A name you choose for the release.
  • <repo_name>/<chart_name>: The name of the repository and the chart you want to install.
  • [flags] : any optional flags like -n namespace or -f values.yaml

3. Example: helm install my-nginx bitnami/nginx

6.5 Example Workflow

$ helm repo add bitnami https://charts.bitnami.com/bitnami
$ helm repo update
$ helm search repo nginx
$ helm install my-nginx bitnami/nginx

This blog is based on interaction with https://gemini.google.com/.

Happy learning :-)

--

--

Dilip Kumar
Dilip Kumar

Written by Dilip Kumar

With 18+ years of experience as a software engineer. Enjoy teaching, writing, leading team. Last 4+ years, working at Google as a backend Software Engineer.

No responses yet