Learn Kubernetes · 12/14

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:

  1. Find the official website.
  2. Download the correct version for your operating system.
  3. Figure out all the dependencies (other software it needs to run) and install them manually.
  4. Follow a long list of installation instructions.
  5. 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:

bash
# 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.sh

This 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:

bash
helm version

You 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:

bash
helm create my-first-chart

This will generate a directory with the following structure:

cmake
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 chart

Let'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.yaml provides this information.
    yaml
    apiVersion: 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.yaml file 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: 80
  • templates/ 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.yaml template:

    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: 80

    Notice the {{ .Values.replicaCount }}. Helm will replace this with the replicaCount value from values.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.

  1. Add the repository:

    bash
    helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
  2. Update your local repository information:

    bash
    helm repo update
  3. Search for the chart:

    bash
    helm search repo prometheus-community/prometheus
  4. Install the chart:

    bash
    helm install my-prometheus prometheus-community/prometheus --namespace monitoring --create-namespace

    Let'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 the monitoring namespace. 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:

bash
helm list -n monitoring

Upgrading a Release

Let's say a new version of the Prometheus chart is released with security fixes. Upgrading is simple.

  1. Update your repositories to get the latest chart versions:

    bash
    helm repo update
  2. Upgrade the release:

    bash
    helm upgrade my-prometheus prometheus-community/prometheus --namespace monitoring

    Helm 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.

  1. Check the revision history:

    bash
    helm history my-prometheus -n monitoring

    This will show you a list of revisions.

  2. Roll back to the previous revision (e.g., revision 1):

    bash
    helm rollback my-prometheus 1 -n monitoring

    And 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.

  1. Create the chart structure:

    bash
    helm create my-nginx-chart
  2. Customize values.yaml:
    Open my-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 to LoadBalancer so we can access it externally (on a cloud provider).

    yaml
    # my-nginx-chart/values.yaml
    ...
    service:
      type: LoadBalancer # Changed from ClusterIP
      port: 80
    ...
  3. Inspect the templates:
    Open my-nginx-chart/templates/deployment.yaml and my-nginx-chart/templates/service.yaml. You can see how the values from values.yaml are being used, like {{ .Values.service.type }} and {{ .Values.service.port }}.

  4. 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-namespace

    We are installing the chart from our local filesystem this time.

  5. Test it out:
    After a minute, get the external IP of your service:

    bash
    kubectl get svc -n web

    You should see an external IP for the my-nginx-release-my-nginx-chart service. 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

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 apiVersion to v2.
  • Name the chart my-nginx.
  • Provide a simple one-line description for an Nginx web server chart.
  • Set the chart version to 0.1.0.
  • Set the appVersion to 1.16.0.

2. values.yaml:

  • Define a default replicaCount of 1.
  • Define an image object with two keys:
    • repository: nginx
    • tag: "stable" (ensure this is a string)
  • Define a service object with one key:
    • port: 80

3. templates/deployment.yaml:

  • Create a standard Kubernetes Deployment manifest (apiVersion: apps/v1, kind: Deployment).
  • The Deployment metadata.name should be {{ .Release.Name }}-nginx.
  • The spec.replicas field must be templated to use the replicaCount from your values.yaml.
  • The pod template's container image must be constructed by combining the image.repository and image.tag from your values.yaml (e.g., "{{ .Values.image.repository }}:{{ .Values.image.tag }}").
  • The container ports.containerPort must be templated to use the service.port value.

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.

yaml

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.