Post

Kubernetes Helm Charts

⎈ Kubernetes Helm Charts

As your Kubernetes applications grow in complexity, managing numerous YAML files becomes challenging. Helm, often described as the β€œpackage manager for Kubernetes,” solves this problem by providing a powerful way to define, install, and upgrade even the most complex Kubernetes applications.

πŸ“¦ What is Helm?

Helm is a package manager for Kubernetes that helps you manage Kubernetes applications. Helm Charts are packages of pre-configured Kubernetes resources that can be easily shared and deployed.

Key Helm concepts include:

  • Chart: A Helm package containing all resource definitions necessary to run an application in Kubernetes
  • Release: An instance of a chart running in a Kubernetes cluster
  • Repository: A place where charts can be stored and shared
  • Values: Configuration that can be supplied to customize a chart during installation

πŸ—οΈ Helm Chart Structure

A typical Helm chart has the following structure:

1
2
3
4
5
6
7
8
9
10
mychart/
  β”œβ”€β”€ Chart.yaml           # Metadata about the chart
  β”œβ”€β”€ values.yaml          # Default configuration values
  β”œβ”€β”€ charts/              # Directory containing dependencies
  β”œβ”€β”€ templates/           # Directory containing templates
  β”‚   β”œβ”€β”€ deployment.yaml  # Kubernetes resource templates
  β”‚   β”œβ”€β”€ service.yaml
  β”‚   β”œβ”€β”€ ingress.yaml
  β”‚   └── _helpers.tpl     # Template helpers
  └── .helmignore          # Patterns to ignore when packaging

Chart.yaml

This file contains metadata about the chart:

1
2
3
4
5
6
7
8
9
apiVersion: v2
name: mychart
version: 0.1.0
description: A Helm chart for my application
type: application
appVersion: "1.16.0"
maintainers:
  - name: Your Name
    email: your.email@example.com

values.yaml

This file contains default configuration values that can be overridden during installation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
replicaCount: 1

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

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: false
  hosts:
    - host: chart-example.local
      paths: []

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

Templates

Templates are Kubernetes manifest files with Go templating syntax that allow for dynamic generation of resources based on values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# templates/deployment.yaml
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 }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          resources:
            {{- toYaml .Values.resources | nindent 12 }}

πŸš€ Using Helm

Installing Helm

1
2
3
4
5
6
7
8
9
10
11
# Using Homebrew (macOS)
brew install helm

# Using Chocolatey (Windows)
choco install kubernetes-helm

# Using Snap (Linux)
sudo snap install helm --classic

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

Adding Repositories

1
2
3
4
5
6
7
8
# Add the official Helm stable repository
helm repo add stable https://charts.helm.sh/stable

# Add Bitnami repository (popular for many applications)
helm repo add bitnami https://charts.bitnami.com/bitnami

# Update repositories
helm repo update

Searching for Charts

1
2
3
4
5
# Search for available charts
helm search repo nginx

# Get information about a specific chart
helm show chart bitnami/nginx

Installing Charts

1
2
3
4
5
6
7
8
9
10
11
# Install a chart with default values
helm install my-release bitnami/nginx

# Install with custom values
helm install my-release bitnami/nginx --set replicaCount=3

# Install with values from a file
helm install my-release bitnami/nginx -f custom-values.yaml

# Install in a specific namespace
helm install my-release bitnami/nginx --namespace my-namespace

Managing Releases

1
2
3
4
5
6
7
8
9
10
11
# List all releases
helm list

# Upgrade a release
helm upgrade my-release bitnami/nginx --set replicaCount=5

# Rollback to a previous revision
helm rollback my-release 1

# Uninstall a release
helm uninstall my-release

πŸ› οΈ Creating Your Own Helm Chart

Creating a Chart

1
2
3
4
5
# Create a new chart
helm create mychart

# Or with more modern syntax
helm create chart mychart

Customizing the Chart

Edit the generated files to customize your chart:

  1. Update Chart.yaml with your chart’s metadata
  2. Modify values.yaml with your default configuration
  3. Create or edit templates in the templates/ directory

Testing Your Chart

1
2
3
4
5
6
7
8
# Validate your chart
helm lint mychart

# Render templates locally without installing
helm template mychart

# Test installation with dry-run
helm install --dry-run --debug my-release ./mychart

Packaging and Sharing

1
2
3
4
5
# Package your chart
helm package mychart

# Create a local chart repository
helm repo index --url https://example.com/charts .

🧩 Helm Chart Dependencies

You can define dependencies in your Chart.yaml:

1
2
3
4
5
6
7
8
9
dependencies:
  - name: mysql
    version: 8.8.26
    repository: https://charts.bitnami.com/bitnami
    condition: mysql.enabled
  - name: redis
    version: 16.13.1
    repository: https://charts.bitnami.com/bitnami
    condition: redis.enabled

Manage dependencies with:

1
2
3
4
5
# Update dependencies
helm dependency update mychart

# Build dependencies
helm dependency build mychart

πŸ”„ Helm Chart Hooks

Helm provides hooks to intervene at certain points in a release’s lifecycle:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# templates/pre-install-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "mychart.fullname" . }}-pre-install
  annotations:
    "helm.sh/hook": pre-install
    "helm.sh/hook-weight": "-5"
    "helm.sh/hook-delete-policy": hook-succeeded
spec:
  template:
    spec:
      containers:
        - name: pre-install-job
          image: "busybox"
          command: ["/bin/sh", "-c", "echo Pre-install job running"]
      restartPolicy: Never

Common hook types include:

  • pre-install: Executes before any resources are installed
  • post-install: Executes after all resources are installed
  • pre-delete: Executes before any resources are deleted
  • post-delete: Executes after all resources are deleted
  • pre-upgrade: Executes before any resources are upgraded
  • post-upgrade: Executes after all resources are upgraded
  • pre-rollback: Executes before any resources are rolled back
  • post-rollback: Executes after all resources are rolled back

πŸ” Helm Chart Testing

Helm provides a plugin for testing charts:

1
2
3
4
5
6
# Install the helm-test plugin
helm plugin install https://github.com/helm/chart-testing

# Run tests
ct lint --all
ct install --all

🌟 Helm Best Practices

  1. Use Semantic Versioning: Follow semantic versioning for your charts
  2. Document Values: Add comments to your values.yaml file
  3. Use Helper Templates: Create reusable templates in _helpers.tpl
  4. Validate Resources: Use helm lint and helm template to validate
  5. Set Resource Limits: Always define resource requests and limits
  6. Use Conditions: Make components optional with conditions
  7. Test Thoroughly: Test your charts in different environments
  8. Keep Charts Simple: One chart should do one thing well
  9. Use Labels Consistently: Follow Kubernetes labeling best practices
  10. Secure Your Values: Don’t commit sensitive data to your chart

Some popular Helm charts include:

  • Prometheus: Monitoring and alerting
  • Grafana: Visualization and dashboards
  • Elasticsearch: Search and analytics
  • Jenkins: Continuous integration and delivery
  • MySQL/PostgreSQL: Databases
  • RabbitMQ/Kafka: Message brokers
  • Nginx/Traefik: Ingress controllers

πŸ”„ Helm vs. Other Tools

Tool Purpose Comparison with Helm
Kustomize Customization of Kubernetes manifests No templating, overlay-based approach
Operators Application lifecycle management More powerful for day-2 operations, but more complex
Plain YAML Direct Kubernetes configuration Simple but lacks templating and versioning
Jsonnet Data templating language More powerful but steeper learning curve

🏁 Conclusion

Helm Charts provide a powerful way to package, distribute, and manage Kubernetes applications. By using Helm, you can:

  • Simplify complex deployments
  • Manage application configurations
  • Version and share your applications
  • Rollback to previous versions easily
  • Manage dependencies between applications

As you continue your Kubernetes journey, mastering Helm will significantly enhance your ability to deploy and manage applications at scale.

This post is licensed under CC BY 4.0 by the author.