Helm charts allow you to package Kubernetes applications into a convenient bundle that includes all necessary resources for deployment and management. Charts automate the creation, updating, and deletion of applications in Kubernetes, making them easier and more flexible to manage.
To create a new chart, use the following command:
helm create <chart_name>
Helm will automatically generate a basic directory structure and files containing the necessary templates and configurations for deploying the application in Kubernetes.
The main file of the chart containing metadata: name, version, description, and application version. It can also include dependency information if the application relies on other charts.
Usage Example:
A file with default parameters used to set values applied to chart templates for generating the final Kubernetes manifests.
Usage Example:
--set
flag or an additional file during installation.This directory contains YAML templates for Kubernetes files that define objects (pods, deployments, services, etc.) that will be created when the chart is installed. Templates use the Go language to dynamically generate manifests based on values from values.yaml
.
Usage Example:
deployment.yaml
, service.yaml
).A directory for dependencies, i.e. other charts required for your application to run. Dependencies can be installed locally or downloaded from remote repositories.
Usage Example:
Dependencies are downloaded with helm dependency update
if specified in Chart.yaml
.
This file functions like .gitignore
, excluding files and directories when packaging the chart into an archive.
Usage Example:
Excludes unnecessary files (e.g., documentation or temporary files) to keep them out of the final package.
Now that we’ve reviewed the basic structure of a chart and the purpose of the key files, let’s move on to a practical example. We’ll create a chart for deploying NGINX, configure all the necessary parameters, and see how templates use data from values.yaml to generate Kubernetes resources.
Start by creating the basic structure of the chart. Enter the command:
helm create nginx-chart
This will create a directory nginx-chart/
with the basic files.
nginx-chart
├── charts
├── Chart.yaml
├── templates
│ ├── deployment.yaml
│ ├── _helpers.tpl
│ ├── hpa.yaml
│ ├── ingress.yaml
│ ├── NOTES.txt
│ ├── serviceaccount.yaml
│ ├── service.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml
The Chart.yaml
file is a key element of any Helm chart. This file specifies metadata for the chart, such as its name, version, description, and information about dependencies.
Example of the Chart.yaml
content for the NGINX chart:
apiVersion: v2
name: nginx-chart
description: A Helm chart for deploying NGINX
version: 0.1.0
appVersion: "1.21.0"
Key fields:
apiVersion
: The Helm API version for this chart. Use v2
for Helm 3 charts.name
: The name of your chart. This should be unique in your repository.description
: A brief description of what this chart does.version
: The version of the chart itself. Each time you make changes to the chart, you should update this version.appVersion
: The version of the application (in this case, NGINX) that will be deployed. This field does not affect Helm’s logic but helps users see which version of the application they are installing.The values.yaml
file is used to specify default values for parameters that can be changed during chart installation. Configure it as follows:
replicaCount: 2
image:
repository: nginx
tag: "1.21.0"
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 80
serviceAccount:
create: true
name: ""
ingress:
enabled: false
annotations: {}
hosts:
- host: chart-example.local
paths: []
tls: []
autoscaling:
enabled: false
minReplicas: 1
maxReplicas: 5
targetCPUUtilizationPercentage: 80
resources: {}
nodeSelector: {}
tolerations: []
affinity: {}
This file contains the following settings:
replicaCount
: The number of application replicas.image
: Settings for the NGINX container image, including the version (tag
).service
: The type of service and the port through which NGINX will be accessible.serviceAccount
: Creates a service account for the chart.ingress
: Parameters for Ingress (disabled by default).autoscaling
: Parameters for auto-scaling (disabled by default).Next, let’s configure the templates in the templates
folder that will use values from values.yaml
.
A template for creating a Kubernetes service:
apiVersion: v1
kind: Service
metadata:
name: {{ include "nginx-chart.fullname" . }}
labels:
app: {{ include "nginx-chart.name" . }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: 80
selector:
app: {{ include "nginx-chart.name" . }}
A template for creating an NGINX deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "nginx-chart.fullname" . }}
labels:
app: {{ include "nginx-chart.name" . }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ include "nginx-chart.name" . }}
template:
metadata:
labels:
app: {{ include "nginx-chart.name" . }}
spec:
serviceAccountName: {{ if .Values.serviceAccount.create }}{{ include "nginx-chart.serviceAccountName" . }}{{ else }}{{ .Values.serviceAccount.name }}{{ end }}
containers:
- name: nginx
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
ports:
- containerPort: 80
A template for creating a service account:
{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "nginx-chart.serviceAccountName" . }}
{{- end }}
Check the chart for errors:
helm lint ./nginx-chart
Install the chart:
helm install test-nginx ./nginx-chart
Check the release status:
helm status test-nginx
Check the state of the pods:
kubectl get pods
When the application is no longer needed, it can be removed:
helm uninstall test-nginx