Helm is a package manager for Kubernetes applications that simplifies the process of defining, installing, and upgrading even the most complex Kubernetes applications. It provides a way to package, share, and manage applications and their dependencies in a consistent way. This tutorial will guide you through the basics of Helm and how to use it with Kubernetes.
Let’s create a simple Helm chart with examples of Chart.yaml
, values.yaml
, and template files.
1. Create a Helm Chart
# Create a new Helm chart named "mywebapp"
helm create mywebapp
cd mywebapp
2. Update Chart.yaml
Edit the Chart.yaml
file to define metadata for your chart:
# mywebapp/Chart.yaml
apiVersion: v2
name: mywebapp
description: A Helm chart for deploying a simple web application
version: 0.1.0
3. Update values.yaml
Define default values for your chart in the values.yaml
file:
# mywebapp/values.yaml
replicaCount: 1
image:
repository: nginx
tag: stable
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 80
ingress:
enabled: false
hosts:
- host: chart-example.local
paths:
- /
4. Create Template Files
Edit the template files in the templates
directory:
Deployment Template (templates/deployment.yaml
)
# mywebapp/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "mywebapp.fullname" . }}
labels:
app: {{ include "mywebapp.name" . }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ include "mywebapp.name" . }}
template:
metadata:
labels:
app: {{ include "mywebapp.name" . }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- containerPort: 80
Service Template (templates/service.yaml
)
# mywebapp/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: {{ include "mywebapp.fullname" . }}
labels:
app: {{ include "mywebapp.name" . }}
spec:
ports:
- port: {{ .Values.service.port }}
targetPort: 80
selector:
app: {{ include "mywebapp.name" . }}
Ingress Template (templates/ingress.yaml
)
# mywebapp/templates/ingress.yaml
{{- if .Values.ingress.enabled -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ include "mywebapp.fullname" . }}
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: {{ .Values.ingress.hosts.host }}
http:
paths:
- path: {{ .Values.ingress.hosts.paths | quote }}
pathType: Prefix
backend:
service:
name: {{ include "mywebapp.fullname" . }}
port:
number: {{ .Values.service.port }}
{{- end }}
5. Install the Helm Chart
# Install the chart with release name "mywebapp-release"
helm install mywebapp-release ./mywebapp
6. Upgrade the Helm Chart
# Upgrade the chart with a new image tag
helm upgrade mywebapp-release ./mywebapp --set image.tag=latest
7. Uninstall the Helm Chart
# Uninstall the chart
helm uninstall mywebapp-release
Feel free to customize the template files and values according to your application’s requirements. This example demonstrates a basic structure for a Helm chart with deployment, service, and optional ingress components.