Helm Charts: A Comprehensive Guide for Kubernetes Package Management

Ashutosh Kumar Sah
5 min readSep 25, 2024

--

Introduction

Helm is an open-source tool used to manage Kubernetes applications, often referred to as “the package manager for Kubernetes.” At the core of Helm is the Helm Chart, which provides a blueprint for Kubernetes deployments. With Helm, users can define, install, and upgrade even the most complex Kubernetes applications in a repeatable and efficient manner.

This article delves deep into the workings of Helm Charts, explaining their structure, functionality, and best practices.

What are Helm Charts?

A Helm Chart is a collection of files that describe a set of Kubernetes resources. These resources might include Deployments, Services, ConfigMaps, and other Kubernetes objects. In essence, Helm Charts are templates that allow users to define, package, share, and manage Kubernetes applications with ease.

Helm Charts abstract the complexity of Kubernetes YAML files and provide the following benefits:

  • Reusability: A single chart can be reused across different environments (e.g., dev, test, prod) by overriding configuration values.
  • Versioning: Helm provides versioning, enabling you to track changes and roll back if necessary.
  • Dependency Management: Helm handles dependencies between multiple Kubernetes resources or between charts themselves.

Helm Architecture

The Helm ecosystem is built upon two main components:

Helm CLI: The command-line interface that developers use to interact with Helm Charts. Common commands include:

  • helm install: To install a chart.
  • helm upgrade: To upgrade an existing deployment.
  • helm rollback: To roll back to a previous chart version.

Helm Tiller (Helm v2 only): This was a server-side component that ran in your Kubernetes cluster. It was responsible for deploying and managing the state of your Helm releases. However, Tiller was deprecated in Helm v3, which now uses a client-only architecture that directly interacts with the Kubernetes API server.

Structure of a Helm Chart

Each Helm Chart follows a specific directory structure and contains essential files that define how the application is deployed in Kubernetes. Below is a breakdown of the key components:

my-chart/
├── Chart.yaml
├── values.yaml
├── templates/
│ ├── deployment.yaml
│ ├── service.yaml
│ └── _helpers.tpl
└── charts/

Key Files

  • Chart.yaml: This is the metadata file for the chart. It contains information such as the name, version, and description of the chart.
apiVersion: v2
name: my-app
description: A Helm chart for my Kubernetes app
version: 0.1.0
  • values.yaml: This file contains the default configuration values for the chart. These values can be overridden when the chart is deployed, making it highly configurable.
replicaCount: 2
image:
repository: nginx
tag: latest
service:
type: LoadBalancer
port: 80
  • templates/: This directory contains the Kubernetes manifests that will be rendered and deployed by Helm. Templates allow you to define dynamic values using placeholders. These values are substituted based on the configurations in values.yaml.
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-nginx
spec:
replicas: {{ .Values.replicaCount }}
template:
spec:
containers:
- name: nginx
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
  • _helpers.tpl: This file is used for reusable template snippets, such as defining common labels or annotations.
  • charts/: This directory holds dependent charts, enabling you to manage multiple interdependent applications.

Working with Helm Charts

Installing a Chart

To install a chart, use the helm install command. You can install a chart from a remote repository or a local directory:

helm install my-release my-chart/

In this command:

  • my-release is the name of the release.
  • my-chart/ is the directory containing the chart.

Customizing Values

While installing the chart, you can override the default values in values.yaml by passing custom values using the --set flag or a custom values.yaml file.

helm install my-release my-chart/ --set replicaCount=3

Alternatively, you can use a custom values.yaml:

helm install my-release my-chart/ -f custom-values.ya

Upgrading a Chart

Once the chart is installed, you may need to upgrade it (e.g., updating configurations or chart version). You can do so using the helm upgrade command:

helm upgrade my-release my-chart/

Helm ensures that changes are applied incrementally and keeps track of the release history, making it easy to roll back if necessary.

Rolling Back a Chart

In case of an error or misconfiguration, you can quickly revert to a previous version of a release using helm rollback:

helm rollback my-release 1

The 1 indicates the revision number to which you want to roll back.

Advanced Features

Helm Hooks

Helm provides a powerful hook system that allows you to trigger actions at certain points during the lifecycle of a release. Common hooks include:

  • pre-install: Executes before the chart is installed.
  • post-install: Executes after the chart is installed.
  • pre-upgrade: Executes before an upgrade.
  • post-upgrade: Executes after an upgrade.

Example:

apiVersion: batch/v1
kind: Job
metadata:
name: pre-install-job
annotations:
"helm.sh/hook": pre-install

Managing Dependencies

If your application relies on other charts, Helm can manage dependencies through the requirements.yaml (Helm v2) or Chart.yaml (Helm v3) files.

Example in Chart.yaml:

dependencies:
- name: redis
version: 5.0.0
repository: https://charts.bitnami.com/bitnami

You can then run helm dependency update to fetch the required charts.

Chart Repositories

Helm charts can be shared and distributed through chart repositories. You can add repositories with the helm repo add command:

helm repo add bitnami https://charts.bitnami.com/bitnami
helm install my-release bitnami/nginx

Best Practices for Helm Charts

  1. Use Semantic Versioning: Follow semantic versioning to help users understand the scope of changes between versions.
  2. Template Best Practices: Use built-in functions, like tpl and quote, to handle values correctly. Always validate inputs and default to reasonable values.
  3. Leverage Subcharts: Use subcharts to modularize components, making them easier to maintain and reuse.
  4. Documentation: Provide clear documentation in your README.md and ensure values.yaml includes comprehensive comments for configuration options.
  5. Security: Scan your Helm Charts for vulnerabilities and ensure they are aligned with Kubernetes security practices, such as using non-root containers and properly configured Role-Based Access Control (RBAC).

Conclusion

Helm Charts are a fundamental tool for managing and deploying Kubernetes applications, streamlining the packaging, versioning, and release management process. By understanding the structure of Helm Charts, the commands, and the best practices, you can ensure smooth Kubernetes operations, whether for small applications or large-scale enterprise deployments. With the flexibility and power of Helm, Kubernetes application management becomes simpler, more efficient, and much more scalable.

--

--

Ashutosh Kumar Sah
Ashutosh Kumar Sah

Written by Ashutosh Kumar Sah

DevOps Engineer | Ex - Teqfocus | Microsoft Certified: Az-900, Ai -900, Dp-900 | 2x Oracle cloud infrastructure certified fundamental 2022 | Aviatrix certified

No responses yet