Managing Kubernetes with Helm:

The DevOps DojoThe DevOps Dojo
4 min read

Introduction

Kubernetes has revolutionized container orchestration, enabling teams to deploy, manage, and scale applications efficiently. However, managing Kubernetes manifests for complex applications can be challenging. This is where Helm, the package manager for Kubernetes, comes in.

In this article, we will explore Helm, focusing on its chart structure, Kubernetes manifests, values.yaml, and the process of deploying applications using helm install. By the end, you’ll have a solid understanding of how to manage Kubernetes applications with Helm effectively.


What is Helm?

Helm is a powerful tool that streamlines the deployment of applications on Kubernetes. It helps package Kubernetes resources into Helm Charts, making deployments repeatable, version-controlled, and manageable. Helm acts as a templating engine and a package manager for Kubernetes.

Why Use Helm?

  • Simplifies Kubernetes deployments by bundling YAML manifests into reusable charts.

  • Allows parameterization using values.yaml, reducing redundancy.

  • Supports versioning and rollbacks to track changes efficiently.

  • Enhances reusability across different environments.


Understanding Helm Chart Structure

A Helm Chart is a collection of files that describe a Kubernetes application. When you create a new chart using:

helm create my-chart

You get a default directory structure:

my-chart/
├── charts/           # Dependencies (other charts)
├── templates/        # YAML templates for Kubernetes manifests
│   ├── deployment.yaml
│   ├── service.yaml
│   ├── ingress.yaml
│   ├── _helpers.tpl  # Template helper functions
│   ├── NOTES.txt     # User instructions
│   ├── tests/        # Helm test files
├── values.yaml       # Configurable values for the chart
├── Chart.yaml        # Metadata for the Helm chart
├── README.md         # Documentation

Each component serves a critical function:

  • Chart.yaml: Defines metadata such as name, version, and dependencies.

  • values.yaml: Contains default values for templated variables.

  • templates/: Holds Kubernetes manifests in templated format.

  • charts/: Stores dependencies as subcharts.

  • README.md: Provides documentation for the chart.


Writing Kubernetes Manifests in Helm Charts

Instead of defining raw YAML manifests, Helm allows you to template Kubernetes resources.

Deployment Template

Let's look at a templates/deployment.yaml file:

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

This template uses Helm’s templating syntax ({{ .Values }}) to inject variables dynamically.


Configuring Helm Charts with values.yaml

The values.yaml file allows users to provide configurable parameters:

replicaCount: 3

image:
  repository: nginx
  tag: latest

service:
  port: 80

When Helm processes templates, it replaces placeholders with values from values.yaml, making it easy to customize deployments.


Deploying Applications with Helm

Once your Helm Chart is ready, deploying an application on Kubernetes is straightforward.

Installing a Helm Chart

To deploy the chart, run:

helm install my-release ./my-chart

Where my-release is the release name, and ./my-chart is the chart directory.

Listing Helm Releases

To view installed Helm releases:

helm list

Upgrading and Rolling Back Changes

Helm allows upgrades with:

helm upgrade my-release ./my-chart --set replicaCount=5

If an update causes issues, roll back using:

helm rollback my-release 1

Uninstalling a Helm Release

To remove a deployed application:

helm uninstall my-release

Helm Best Practices

To maximize Helm’s potential, follow these best practices:

  1. Use _helpers.tpl for Reusable Templates Define common labels or annotations in _helpers.tpl to avoid repetition.

     {{- define "common.labels" -}}
     app: {{ .Chart.Name }}
     release: {{ .Release.Name }}
     {{- end -}}
    
  2. Use .Values for Configuration Instead of hardcoding values, make them configurable in values.yaml.

  3. Leverage Helm Hooks for Lifecycle Management Helm supports pre-install and post-install hooks to run jobs before or after deployments.

  4. Maintain Chart Versioning Keep track of Helm chart versions using Chart.yaml.

  5. Test Helm Charts Use helm test to validate deployments.


Conclusion

Helm is an indispensable tool for managing Kubernetes applications. By using Helm Charts, teams can achieve standardized, reusable, and scalable deployments. This article provided a deep dive into Helm’s architecture, templating system, and best practices.

Start using Helm today to simplify Kubernetes application management and enhance your DevOps workflow!

For more DevOps insights, follow The DevOps Dojo on Hashnode and Substack.


Further Reading:

0
Subscribe to my newsletter

Read articles from The DevOps Dojo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

The DevOps Dojo
The DevOps Dojo