Helm Package Management

Learn how to use Helm, the package manager for Kubernetes. Understand Helm charts, releases, repositories, and how to create, install, and manage Helm packages.

Table of Contents

1. Helm Overview

Helm is the package manager for Kubernetes. It helps you manage Kubernetes applications by packaging them into charts that can be easily installed, upgraded, and managed.

1.1 Why Use Helm?

  • Simplifies Kubernetes application deployment
  • Enables application versioning
  • Allows parameterization of deployments
  • Facilitates sharing and reusing configurations
  • Manages dependencies between applications
  • Provides rollback capabilities

1.2 Helm Components

  • Helm Client: Command-line tool for users
  • Helm Charts: Packages containing Kubernetes resources
  • Helm Releases: Installed instances of charts
  • Helm Repositories: Collections of charts

2. Installing Helm

2.1 Install Helm Client

# macOS
brew install helm

# Linux
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

# Windows
choco install kubernetes-helm

# Verify installation
helm version

2.2 Helm 3 vs Helm 2

Helm 3 is the current version and doesn't require Tiller (server component). It's simpler and more secure than Helm 2.

3. Helm Concepts

3.1 Charts

A Chart is a collection of files that describe a related set of Kubernetes resources. Charts are packaged as tar archives.

Chart structure:

mychart/
  Chart.yaml          # Chart metadata
  values.yaml         # Default configuration values
  templates/          # Template files
    deployment.yaml
    service.yaml
    ingress.yaml
  charts/             # Chart dependencies
  README.md

3.2 Releases

A Release is an instance of a chart running in a Kubernetes cluster. One chart can be installed multiple times, each creating a new release.

3.3 Repositories

A Repository is a collection of charts. The default public repository is Artifact Hub (formerly Helm Hub).

4. Using Charts

4.1 Search for Charts

# Search Artifact Hub
helm search hub nginx

# Search in added repositories
helm search repo nginx

# List all repositories
helm repo list

4.2 Add Repository

# Add official stable repository
helm repo add stable https://charts.helm.sh/stable

# Add Bitnami repository
helm repo add bitnami https://charts.bitnami.com/bitnami

# Update repository index
helm repo update

4.3 Install Chart

# Install from repository
helm install my-nginx bitnami/nginx

# Install with custom values
helm install my-nginx bitnami/nginx -f values.yaml

# Install with inline values
helm install my-nginx bitnami/nginx \
  --set service.type=LoadBalancer \
  --set replicaCount=3

# Install from local chart
helm install my-app ./mychart

# Install in specific namespace
helm install my-nginx bitnami/nginx --namespace production --create-namespace

4.4 List Releases

# List all releases
helm list

# List releases in namespace
helm list --namespace production

# List all releases (including deleted)
helm list --all

# Show release status
helm status my-nginx

4.5 Upgrade Release

# Upgrade release
helm upgrade my-nginx bitnami/nginx

# Upgrade with new values
helm upgrade my-nginx bitnami/nginx -f new-values.yaml

# Upgrade and wait for completion
helm upgrade my-nginx bitnami/nginx --wait

# Upgrade with timeout
helm upgrade my-nginx bitnami/nginx --timeout 5m

4.6 Rollback Release

# View release history
helm history my-nginx

# Rollback to previous version
helm rollback my-nginx

# Rollback to specific revision
helm rollback my-nginx 2

4.7 Uninstall Release

# Uninstall release
helm uninstall my-nginx

# Uninstall and keep history
helm uninstall my-nginx --keep-history

5. Creating Charts

5.1 Create New Chart

# Create new chart
helm create mychart

# This creates:
# mychart/
#   Chart.yaml
#   values.yaml
#   templates/
#   charts/
#   .helmignore

5.2 Chart.yaml

apiVersion: v2
name: mychart
description: A Helm chart for my application
type: application
version: 0.1.0
appVersion: "1.0"
dependencies:
  - name: postgresql
    version: "12.0.0"
    repository: "https://charts.bitnami.com/bitnami"

5.3 values.yaml

replicaCount: 1

image:
  repository: nginx
  pullPolicy: IfNotPresent
  tag: "1.21"

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: false
  className: ""
  annotations: {}
  hosts:
    - host: chart-example.local
      paths:
        - path: /
          pathType: Prefix
  tls: []

resources:
  limits:
    cpu: 100m
    memory: 128Mi
  requests:
    cpu: 100m
    memory: 128Mi

6. Chart Templates

Helm uses Go templates with Sprig functions for templating Kubernetes manifests.

6.1 Template Syntax

# Access values
{{ .Values.replicaCount }}

# Default value
{{ .Values.image.tag | default "latest" }}

# Conditional
{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
{{- end }}

# Range over list
{{- range .Values.env }}
- name: {{ .name }}
  value: {{ .value }}
{{- end }}

6.2 Template Example

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "mychart.fullname" . }}
  labels:
    {{- include "mychart.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "mychart.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "mychart.selectorLabels" . | nindent 8 }}
    spec:
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        ports:
        - containerPort: {{ .Values.service.port }}
        resources:
          {{- toYaml .Values.resources | nindent 10 }}

6.3 Template Helpers

Common template helpers in _helpers.tpl:

{{- define "mychart.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}

{{- define "mychart.labels" -}}
helm.sh/chart: {{ include "mychart.chart" . }}
app.kubernetes.io/name: {{ include "mychart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

6.4 Testing Templates

# Dry run - see rendered templates
helm install my-app ./mychart --dry-run --debug

# Template command - render templates
helm template my-app ./mychart

# Validate chart
helm lint ./mychart

# Test with different values
helm template my-app ./mychart -f test-values.yaml

7. Helm Repositories

7.1 Public Repositories

  • Artifact Hub: Default public repository (artifacthub.io)
  • Bitnami: Production-ready charts
  • Kubernetes Official: Official Kubernetes charts

7.2 Create Private Repository

Host charts in a private repository:

# Package chart
helm package ./mychart

# Create index
helm repo index . --url https://charts.example.com

# Serve via HTTP server or object storage
# Add to Helm
helm repo add myrepo https://charts.example.com
helm repo update

7.3 Chart Dependencies

# Update dependencies
helm dependency update ./mychart

# Build dependencies
helm dependency build ./mychart

# List dependencies
helm dependency list ./mychart

8. Best Practices

8.1 Version Your Charts

Use semantic versioning for charts. Update version in Chart.yaml for each change.

8.2 Use Values Files

Keep default values in values.yaml. Use separate values files for different environments (dev, staging, prod).

8.3 Validate Before Deploying

Always use helm lint and helm template --dry-run before installing charts.

8.4 Use Template Helpers

Create reusable template helpers in _helpers.tpl to avoid duplication.

8.5 Document Your Charts

Include comprehensive README.md with installation instructions, configuration options, and examples.

8.6 Test Charts

Use helm test to run test Pods that verify your chart works correctly.

8.7 Use Helm Secrets

For sensitive values, use helm-secrets plugin or external secret management systems.

8.8 Keep Charts Simple

Keep charts focused and simple. Use dependencies for complex applications.

Summary: Helm is the package manager for Kubernetes that simplifies application deployment through charts. Use Helm to install, upgrade, and manage applications. Create reusable charts with templates and values files. Always validate and test charts before deploying to production.

Post a Comment

0 Comments