Helm charts are packages of pre-configured Kubernetes resources. They allow you to define, install, and upgrade even the most complex Kubernetes applications. This tutorial will guide you through the process of creating a basic Helm chart.
Prerequisites:
- Helm installed on your system.
- kubectl configured to connect to your Kubernetes cluster.
1. Setting up the Chart Structure
Use the helm create
command to generate a basic chart structure:
helm create my-first-chart
This will create a directory named my-first-chart
with the following structure:
my-first-chart/ ├── charts/ ├── templates/ │ ├── NOTES.txt │ ├── _helpers.tpl │ ├── deployment.yaml │ └── service.yaml ├── Chart.yaml └── values.yaml
charts/
: Contains chart dependencies (subcharts).templates/
: Holds Kubernetes manifest templates.Chart.yaml
: Contains metadata about the chart.values.yaml
: Defines default values for the templates.
2. Understanding Chart.yaml
Open my-first-chart/Chart.yaml
and examine its contents:
apiVersion: v2 name: my-first-chart description: A Helm chart for Kubernetes type: application version: 0.1.0 appVersion: "1.16.0"
apiVersion
: The API version of the chart.name
: The name of the chart.description
: A brief description of the chart.type
: The type of the chart (application or library).version
: The chart’s version.appVersion
: The version of the application being deployed.
Modify these values as needed for your application.
3. Modifying values.yaml
Open my-first-chart/values.yaml
and add or modify values as required. For example:
replicaCount: 1 image: repository: nginx pullPolicy: IfNotPresent tag: "1.16.0" service: type: ClusterIP port: 80
These values will be used to populate the templates in the templates/
directory.
4. Editing the Deployment Template
Open my-first-chart/templates/deployment.yaml
and modify it to match your application’s requirements. For example:
apiVersion: apps/v1 kind: Deployment metadata: name: {{ .Release.Name }}-deployment labels: app: {{ .Release.Name }} spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: app: {{ .Release.Name }} template: metadata: labels: app: {{ .Release.Name }} spec: containers: - name: {{ .Release.Name }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" imagePullPolicy: {{ .Values.image.pullPolicy }} ports: - containerPort: {{ .Values.service.port }}
{{ .Release.Name }}
: Accesses the release name.{{ .Values.replicaCount }}
: Accesses thereplicaCount
value fromvalues.yaml
.{{ .Values.image.repository }}
: Accesses theimage.repository
value.{{ .Values.image.tag }}
: Accesses theimage.tag
value.{{ .Values.image.pullPolicy }}
: access the pull policy of the image.{{ .Values.service.port }}
: Accesses the service port.
5. Editing the Service Template
Open my-first-chart/templates/service.yaml
and modify it:
apiVersion: v1 kind: Service metadata: name: {{ .Release.Name }}-service labels: app: {{ .Release.Name }} spec: type: {{ .Values.service.type }} ports: - port: {{ .Values.service.port }} targetPort: {{ .Values.service.port }} protocol: TCP selector: app: {{ .Release.Name }}
6. Installing the Chart
Navigate to the chart’s directory:
cd my-first-chart
Install the chart:
helm install my-release .
my-release
: The release name..
: The current directory (where the chart is located).
Verify the deployment:
kubectl get deployments kubectl get services
7. Upgrading the Chart
- Modify
values.yaml
ortemplates/
as needed. - Upgrade the release:
helm upgrade my-release .
Verify the changes:
kubectl get deployments
8. Packaging the Chart
Package the chart:
helm package .
This will create a .tgz
file that can be distributed.
9. Installing a packaged chart
Install the packaged chart:
helm install my-release my-first-chart-0.1.0.tgz
10. Uninstalling the release
helm uninstall my-release