Helm: Package Management for Kubernetes
9 min read
Helm: The Package Manager for Kubernetes 舵
Welcome, Kubernetes navigators! You've learned how to create deployments, services, and all sorts of other Kubernetes objects. It's powerful stuff, but you might have noticed it can get a little… repetitive. Deploying even a simple application can involve writing multiple YAML files. Now, imagine deploying something complex like a production-grade database cluster. That's a whole fleet of YAML! 🚢
This is where Helm comes in to save the day. Let's dive in!
1. Introduction to Helm: The Kubernetes Package Manager 📦
What is a Package Manager?
Before we talk about Helm, let's talk about package managers in general. If you've ever used apt on Debian/Ubuntu, yum on CentOS, brew on macOS, or npm in the JavaScript world, you've used a package manager.
The Problem: Imagine you want to install a new software on your computer. Without a package manager, you would have to:
- Find the official website.
- Download the correct version for your operating system.
- Figure out all the dependencies (other software it needs to run) and install them manually.
- Follow a long list of installation instructions.
- Repeat this entire process every time you want to update the software.
It’s tedious and error-prone.
The Solution: A package manager automates all of this. You simply say, "I want to install software X," and it handles finding the software, downloading it, installing its dependencies, and configuring it for you. It's like having a super-efficient assistant for your software.
Helm: The Package Manager for Kubernetes
Now, let's apply that to Kubernetes.
The Problem: Deploying applications in Kubernetes often requires creating a set of YAML files for Deployments, Services, ConfigMaps, Secrets, Ingresses, etc. Managing all these files for every application, across different environments (like development, staging, and production), can become a nightmare. How do you share your application setup with a colleague? How do you version control your application's Kubernetes configuration?
The Solution: Helm is the package manager for Kubernetes. It allows you to package all those YAML files into a single, manageable unit called a Chart. You can then install, upgrade, and manage applications with simple commands, just like you would with apt or npm.
Key Helm Concepts:
- Chart: A Helm package. It's a collection of files that describe a related set of Kubernetes resources. Think of it as the recipe and all the ingredients needed to deploy an application.
- Release: An instance of a chart running in a Kubernetes cluster. You can install the same chart multiple times into the same cluster, and each one will be a separate release with its own name.
- Repository: A place where charts can be collected and shared. It's like a library of pre-built application packages for Kubernetes.
Interesting Fact: Helm was the first project to graduate from the Cloud Native Computing Foundation (CNCF) after Kubernetes itself. It's a testament to how fundamental it is to the Kubernetes ecosystem.
Getting Started: Installing Helm
Let's get Helm installed on your machine. The official and most common way is to use their installer script.
From the official docs: Open your terminal and run the following commands:
# For MacOS/Linux
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.shThis script will install the Helm client on your local machine. Helm is a client-side tool that communicates with your Kubernetes cluster's API server, so there's no server-side component (Tiller) to worry about like in older versions of Helm.
To verify your installation, run:
helm versionYou should see the client version information. Now you're ready to set sail! ⛵️
2. Helm Charts: Understanding the Anatomy and Structure 🗺️
A Helm chart is essentially a folder with a specific structure. When you package a chart, it gets bundled into a versioned archive file (e.g., my-chart-0.1.0.tgz). Let's dissect the structure of a chart.
You can create a new chart with a handy command:
helm create my-first-chartThis will generate a directory with the following structure:
my-first-chart/
├── Chart.yaml # Metadata about the chart
├── values.yaml # Default configuration values for the chart
├── charts/ # A directory for chart dependencies
├── templates/ # A directory of templates that, when combined with values, generate Kubernetes manifest files
│ ├── tests/ # A directory of test files
│ │ └── test-connection.yaml
│ ├── NOTES.txt # A plain text file containing helpful information for the user after installation
│ ├── _helpers.tpl # A place to put template helpers that you can re-use throughout your chart
│ ├── deployment.yaml # A basic deployment manifest
│ ├── service.yaml # A basic service manifest
│ └── ...
└── .helmignore # Specifies files to ignore when packaging the chartLet's break down the most important files:
Chart.yaml: This file contains the metadata about your chart.- The Problem: How do you know what version of a chart you are using? What is its name?
- The Solution:
Chart.yamlprovides this information.
yamlapiVersion: v2 name: my-first-chart description: A Helm chart for Kubernetes type: application # Can be 'application' or 'library' version: 0.1.0 # This is the chart version appVersion: "1.16.0" # This is the version of the app being deployed (e.g., nginx version)values.yaml: This is where you define the default configuration for your chart.- The Problem: You want to deploy the same application in different environments, but with slight variations (e.g., a different number of replicas or a different container image tag). Hardcoding these values in your templates would be inefficient.
- The Solution: The
values.yamlfile externalizes the configuration. These values can be easily overridden by the user during installation.
yaml# Example values.yaml replicaCount: 1 image: repository: nginx pullPolicy: IfNotPresent tag: "" # Defaults to the chart's appVersion service: type: ClusterIP port: 80templates/directory: This is the heart of the chart. It contains template files that are processed by Helm's template engine.- The Problem: How do you generate valid Kubernetes YAML files while allowing for customization?
- The Solution: Helm uses Go templates. It takes the values from
values.yaml(or user-provided values) and injects them into the templates to produce the final Kubernetes manifests.
Here's a simplified snippet from a
deployment.yamltemplate:yaml# templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: {{ .Release.Name }}-deployment spec: replicas: {{ .Values.replicaCount }} # This value comes from values.yaml template: spec: containers: - name: {{ .Chart.Name }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" ports: - containerPort: 80Notice the
{{ .Values.replicaCount }}. Helm will replace this with thereplicaCountvalue fromvalues.yaml. This templating is what makes Helm so powerful and flexible.
3. Installing, Upgrading, and Rolling Back Third-Party Apps 🔄
One of the main uses of Helm in the industry is to manage off-the-shelf software like databases, monitoring tools, and message queues. Let's try it out with a real-world example.
Finding and Installing a Chart
Let's say we want to install Prometheus, a popular monitoring system. First, we need to add a repository that contains the Prometheus chart. The prometheus-community repository is a good choice.
Add the repository:
bashhelm repo add prometheus-community https://prometheus-community.github.io/helm-chartsUpdate your local repository information:
bashhelm repo updateSearch for the chart:
bashhelm search repo prometheus-community/prometheusInstall the chart:
bashhelm install my-prometheus prometheus-community/prometheus --namespace monitoring --create-namespaceLet's break down that command:
helm install: The command to install a chart.my-prometheus: This is the release name we've given to our installation. It must be unique within a namespace.prometheus-community/prometheus: The name of the chart we want to install (repository/chart-name).--namespace monitoring: We are installing it in themonitoringnamespace. It's a good practice to keep things organized.--create-namespace: This flag tells Helm to create the namespace if it doesn't exist.
You now have a full Prometheus stack running in your cluster! You can see the status of your release with:
helm list -n monitoringUpgrading a Release
Let's say a new version of the Prometheus chart is released with security fixes. Upgrading is simple.
Update your repositories to get the latest chart versions:
bashhelm repo updateUpgrade the release:
bashhelm upgrade my-prometheus prometheus-community/prometheus --namespace monitoringHelm will figure out what has changed between your current release and the new version of the chart and apply only those changes to your cluster. This is called a "3-way strategic merge patch," which is a fancy way of saying it's very smart about how it applies updates.
Rolling Back a Release: The Undo Button! ⏪
Uh oh! The new version has a bug, and our monitoring is down. This is where Helm truly shines. Every time you upgrade, Helm creates a new revision. You can easily roll back to a previous, stable revision.
Check the revision history:
bashhelm history my-prometheus -n monitoringThis will show you a list of revisions.
Roll back to the previous revision (e.g., revision 1):
bashhelm rollback my-prometheus 1 -n monitoringAnd just like that, you've rolled back to the previous version. Crisis averted! This ability to reliably roll back changes is a lifesaver in production environments.
4. Creating Your First Custom Helm Chart 🎨
While using third-party charts is great, the real power comes when you package your own applications. Let's create a simple chart for an Nginx web server.
Create the chart structure:
bashhelm create my-nginx-chartCustomize
values.yaml:
Openmy-nginx-chart/values.yaml. The generated file has a lot of options. For our simple case, let's make sure the image repository and tag are what we want. The defaults are usually fine for a basic Nginx setup. Let's change the service type toLoadBalancerso we can access it externally (on a cloud provider).yaml# my-nginx-chart/values.yaml ... service: type: LoadBalancer # Changed from ClusterIP port: 80 ...Inspect the templates:
Openmy-nginx-chart/templates/deployment.yamlandmy-nginx-chart/templates/service.yaml. You can see how the values fromvalues.yamlare being used, like{{ .Values.service.type }}and{{ .Values.service.port }}.Install your custom chart:
Navigate to the directory containing your chart and run:bash# Make sure you are in the directory that contains my-nginx-chart/ helm install my-nginx-release ./my-nginx-chart --namespace web --create-namespaceWe are installing the chart from our local filesystem this time.
Test it out:
After a minute, get the external IP of your service:bashkubectl get svc -n webYou should see an external IP for the
my-nginx-release-my-nginx-chartservice. Open that IP in your browser, and you will see the "Welcome to nginx!" page.
Congratulations! You have packaged your first application with Helm. Now you can easily share it, version it, and deploy it consistently across different clusters.
Happy Helming! 🚀
Further Reading & References
- Official Helm Documentation: https://helm.sh/docs/
- Artifact Hub (for finding charts): https://artifacthub.io/
- Bitnami Charts (a popular chart repository): https://bitnami.com/stacks/helm
Challenge
You are tasked with creating a basic Helm chart to deploy an Nginx web server. In the editor below, write the complete contents for the following three files: Chart.yaml, values.yaml, and templates/deployment.yaml.
1. Chart.yaml:
- Set
apiVersiontov2. - Name the chart
my-nginx. - Provide a simple one-line
descriptionfor an Nginx web server chart. - Set the chart
versionto0.1.0. - Set the
appVersionto1.16.0.
2. values.yaml:
- Define a default
replicaCountof1. - Define an
imageobject with two keys:repository:nginxtag:"stable"(ensure this is a string)
- Define a
serviceobject with one key:port:80
3. templates/deployment.yaml:
- Create a standard Kubernetes Deployment manifest (
apiVersion: apps/v1,kind: Deployment). - The Deployment
metadata.nameshould be{{ .Release.Name }}-nginx. - The
spec.replicasfield must be templated to use thereplicaCountfrom yourvalues.yaml. - The pod template's container
imagemust be constructed by combining theimage.repositoryandimage.tagfrom yourvalues.yaml(e.g.,"{{ .Values.image.repository }}:{{ .Values.image.tag }}"). - The container
ports.containerPortmust be templated to use theservice.portvalue.
Expected result
Your submission should contain the complete and valid YAML content for all three files, placed under their respective comment markers.
Chart.yaml:
A valid YAML file containing the specified apiVersion, name, description, version, and appVersion.
values.yaml:
A valid YAML file defining the nested structure for replicaCount, image.repository, image.tag, and service.port with the specified default values.
templates/deployment.yaml:
A complete Kubernetes Deployment YAML manifest. The manifest must correctly use Helm's Go template syntax (e.g., {{ .Values... }}, {{ .Release.Name }}) to reference the corresponding values from the values.yaml file for the replica count, the full image name, and the container port, as instructed.