Getting Started with Helm Charts

MontaF - Sept. 13, 2024

Helm is a powerful package manager for Kubernetes that helps manage complex Kubernetes applications.
By using Helm charts, you can bundle Kubernetes manifests (YAML files) into a single reusable package, making it easier to manage, version, and deploy complex Kubernetes applications.
In this tutorial, we’ll go over the basics of Helm and demonstrate how you can convert a bunch of Kubernetes YAML files into a Helm chart for easier management and deployment.
1. What is Helm?
Helm simplifies deploying applications on Kubernetes by providing a package manager for Kubernetes resources. It allows you to define, install, upgrade, and roll back Kubernetes applications using charts. A Helm chart is a collection of files that describe a related set of Kubernetes resources.
Key Features of Helm:
- Templating: Helm allows you to template Kubernetes YAML files, which means you can use variables in your configurations.
- Versioning: Helm helps you version your Kubernetes deployments, which is useful when managing multiple environments.
- Reusability: You can reuse charts across projects and environments.
- Rollbacks: Easily roll back to previous versions of your application.
2. Installing Helm
Prerequisites:
- You need a running Kubernetes cluster.
- Helm installed on your local machine.
Installing Helm:
1.On macOS (using Homebrew):
brew install helm
2.On Linux or Windows (using curl):
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
3.Verify Helm Installation:
helm version
You should see the Helm client version displayed.
Initializing Helm in the Cluster (Helm v3 does not require server-side setup):
Helm v3 doesn’t require Tiller (the server component of Helm), so there’s no need for additional cluster-side setup.
3. Understanding Helm Chart Structure
A Helm chart consists of a few key directories and files:
my-chart/
Chart.yaml # Metadata about the chart (name, version, etc.)
values.yaml # Default configuration values
templates/ # Directory containing Kubernetes YAML templates
charts/ # Directory for chart dependencies (optional)
Key Files in a Chart:
- Chart.yaml: Contains metadata about the chart, like its name, version, and description.
- values.yaml: Holds default configuration values for the chart. This file is often used to provide dynamic values for the templates.
- templates/: Contains the Kubernetes YAML files, but in a templated format (more on this later).
4. Creating Your First Helm Chart
Let’s assume you already have a set of Kubernetes YAML files that you use to deploy your application.
We will now convert them into a Helm chart.
Step 1: Create a New Helm Chart
Run the following command to scaffold a new Helm chart:
helm create my-chart
This will create a directory called my-chart
with the basic structure of a Helm chart.
Step 2: Understand the Default Files
my-chart/Chart.yaml
: This file contains metadata about the chart.
apiVersion: v2
name: my-chart
description: A Helm chart for Kubernetes
version: 0.1.0
my-chart/values.yaml
: This file contains default values that can be passed into your templates.
replicaCount: 1
image:
repository: nginx
pullPolicy: IfNotPresent
tag: "latest"
my-chart/templates/
: This directory contains YAML templates that represent your Kubernetes resources (deployments, services, etc.).
5. Converting Kubernetes YAML Files to Helm Templates
Let’s say you have the following Kubernetes YAML files:
Deployment YAML (deployment.yaml
):
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:latest
Service YAML (service.yaml
):
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
Step 1: Move YAML Files to Templates Directory
Move your existing YAML files into the templates/
directory of your Helm chart:
my-chart/
templates/
deployment.yaml
service.yaml
Step 2: Convert Static Values into Template Variables
In Helm, you can use Go templates to dynamically inject values into your Kubernetes YAML files.
For instance, you can make the number of replicas configurable using the values.yaml
file.
Convert deployment.yaml
to Use Helm Templating:
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: {{ .Release.Name }}
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
{{ .Release.Name }}
refers to the name of the Helm release.{{ .Values.replicaCount }}
pulls thereplicaCount
value fromvalues.yaml
.
Convert service.yaml
to Use Helm Templating:
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}-service
spec:
selector:
app: {{ .Release.Name }}
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
Step 3: Define Values in values.yaml
In the values.yaml
file, define the dynamic values that will be injected into the templates:
replicaCount: 2
image:
repository: my-app
tag: latest
6. Installing and Testing the Helm Chart
Step 1: Package the Chart
Once you’ve templated your Kubernetes manifests, you can install them as a Helm release.
To package the chart and deploy it, run:
helm install my-release ./my-chart
This will:
- Deploy the Kubernetes resources defined in your templates.
- Use the values from
values.yaml
by default.
Step 2: Override Values
You can override values in values.yaml
when installing or upgrading the chart. For example, to change the replica count:
helm install my-release ./my-chart --set replicaCount=5
This will deploy 5 replicas instead of the default 2.
Step 3: Viewing Installed Charts
To view installed Helm charts:
helm list
This will show you all the Helm releases in your Kubernetes cluster.
Step 4: Upgrading the Chart
If you want to upgrade the chart (e.g., change the image version), run:
helm upgrade my-release ./my-chart --set image.tag=1.0.1
Step 5: Rolling Back a Release
If you need to roll back a chart to a previous version:
helm rollback my-release 1
7. Best Practices for Helm Charts
- Use
values.yaml
for Configurations: Keep your Kubernetes templates clean and usevalues.yaml
to store configuration options. - Leverage Helm Hooks: Helm allows you to define hooks that run at different stages of a release lifecycle, such as before install or after upgrade. These are useful for performing custom actions like database migrations.
- Version Control Your Charts: Store your Helm charts in a version-controlled repository to track changes and manage updates more easily.
- Separate Environment-Specific Configurations: You can create multiple
values.yaml
files for different environments (e.g.,values.prod.yaml
,values.dev.yaml
).
Conclusion
Helm is an essential tool for managing Kubernetes applications, allowing you to bundle, version, and deploy Kubernetes resources efficiently. By converting your existing Kubernetes YAML files into Helm charts, you gain the ability to reuse configurations, manage deployments more effectively, and apply updates or rollbacks with ease.
With this guide, you should now be able to:
- Install Helm.
- Create your first Helm chart.
- Convert Kubernetes YAML files to templates.
- Deploy and manage Kubernetes applications with Helm.
Helm helps simplify the process of managing complex Kubernetes applications by providing a templating engine and package management features, making it easier to maintain and deploy your infrastructure as code.