Helm serves as a comprehensive package management solution for Kubernetes, functioning similarly to package managers like yum or apt in Linux environments. It simplifies the deployment of pre-packaged applications to Kubernetes clusters, which traditionally required managing numerous YAML files for complex applications.
Core Helm Components
- Helm Client: A command-line interface tool designed for creating, packaging, publishing, and managing Kubernetes application charts.
- Chart: A collection of files that describe a related set of Kubernetes resources. It acts as a blueprint for deploying applications.
- Release: An instance of a chart deployed to Kubernetes. Each time a chart is deployed, it creates a release that generates actual running resources in the cluster.
Installing the Helm Client
To get started with Helm, download the appropriate binary for your system:
wget https://get.helm.sh/helm-v3.8.0-linux-amd64.tar.gz
tar -zxvf helm-v3.8.0-linux-amd64.tar.gz
sudo mv linux-amd64/helm /usr/local/bin/
Essential Helm Commands
Configuring Chart Repositories
Add repoistories to access pre-built charts:
helm repo add azure-charts https://mirror.azure.cn/kubernetes/charts
helm repo add aliyun-charts https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
helm repo update
List available repositories:
helm repo list
helm search repo azure-charts
Remove a repository when needed:
helm repo remove aliyun-charts
Basic Helm Operations
Discovering Charts
Search for available charts:
helm search repo
helm search repo postgresql
Inspecting Charts
Examine chart details before installation:
helm show chart azure-charts/postgresql
Installing Charts
Deploy a chart to your cluster:
helm install database azure-charts/postgresql
Check the deployment status:
helm status database
Customizing Chart Configuration
You can customize chart installations using two approaches:
-
Using a values file:
helm show values azure-charts/mysql > custom-values.yaml # Edit custom-values.yaml helm install db -f custom-values.yaml azure-charts/mysql -
Using command-line parameters:
helm install db --set imageTag=1.20.0 --set persistence.size=10Gi azure-charts/mysql
Creating Custom Charts
Generate a new chart template:
helm create myapp-chart
The chart structure includes:
Chart.yaml: Contains metadata about the chart (name, version, description)values.yaml: Default values for template variablestemplates/: Directory for Kubernetes manifest templatescharts/: Contains dependent chartsNOTES.txt: Usage instructions displayed after installation_helpers.tpl: Reusable template helpers
Chart Template Example
Modify templates/deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-app
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 }}
Managing Releases
Upgrading Releases
Update an existing release:
helm upgrade --set image.tag=1.21.0 myapp myapp-chart
helm upgrade -f new-values.yaml myapp myapp-chart
Rolling Back Releases
If an upgrade doesn't work as expected, revert to a previous version:
helm history myapp
helm rollback myapp 2
Uninstalling Releases
Remove a release from the cluster:
helm uninstall myapp
Template Debugging
Validate templates without deploying:
helm install --dry-run --debug myapp ./myapp-chart
Helm Template Objects
Helm provides several built-in objects for templates:
Working with Values
Values can be sourced from multiple locations, with precedence:
- Chart's
values.yaml - Parent chart's values
- Values files specified with
-for--values - Command-line
--setparameters
Example values file (values.yaml):
replicaCount: 3
image:
repository: nginx
tag: 1.21.0
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 80
resources:
limits:
cpu: 100m
memory: 128Mi
Conditional Logic in Templates
Use conditionals to control template rendering:
{{- if eq .Values.env "production" }}
env:
- name: ENV
value: "prod"
{{- else if eq .Values.env "staging" }}
env:
- name: ENV
value: "stage"
{{- else }}
env:
- name: ENV
value: "dev"
{{- end }}
Template Functions and Pipelines
Helm supports various template functions:
# Indentation
spec:
{{- .Values.resources | indent 8 }}
# String manipulation
name: {{ upper .Values.appName }}
title: {{ title .Values.appName }}
# Numeric operations
count: {{ add .Values.replicaCount 1 }}
Using 'with' for Scoped Templates
Restrict the scope of a variable:
{{- with .Values.service }}
type: {{ .type }}
port: {{ .port }}
{{- end }}
Iterating with Range
Process arrays or maps:
env:
{{- range $key, $value := .Values.environmentVariables }}
- name: {{ $key }}
value: {{ $value | quote }}
{{- end }}
Creating Shared Templates
Define reusable templates in _helpers.tpl:
{{/* Define a named template */}}
{{- define "myapp.fullname" -}}
{{- .Chart.Name }}-{{ .Release.Name }}
{{- end }}
{{/* Use the template */}}
metadata:
name: {{ template "myapp.fullname" . }}
Include templates with specific context:
{{- include "myapp.commonLabels" . | indent 4 }}
By mastering these Helm techniques, you can efficiently manage Kubernetes applications through reusable, versioned packages that simplify deployment and maintenance processes.