Sign In
Sign In

Creating Helm Charts

Updated on 29 October 2024

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.

Creating a New Chart

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.

Key Files and Directories

Chart.yaml

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:

  • Specifies which application is being deployed and its version.
  • Simplifies chart version management and change tracking.

values.yaml

A file with default parameters used to set values applied to chart templates for generating the final Kubernetes manifests.

Usage Example:

  • Defines parameters such as replica count, service type, and container image.
  • Allows overriding values via the --set flag or an additional file during installation.

templates/

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:

  • Templates for creating Kubernetes objects (e.g., deployment.yaml, service.yaml).
  • Logic for flexible resource configuration (e.g., replica count or service type).

charts/

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.

.helmignore

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.

Creating the Chart Basic Structure

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

Configuring the Chart.yaml File

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.

Configuring the values.yaml File

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).

Chart Templates

Next, let’s configure the templates in the templates folder that will use values from values.yaml.

templates/service.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" . }}

templates/deployment.yaml

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

templates/serviceaccount.yaml

A template for creating a service account:

{{- if .Values.serviceAccount.create }}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ include "nginx-chart.serviceAccountName" . }}
{{- end }}

Chart Verification and Installation

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

Chart Removal

When the application is no longer needed, it can be removed:

helm uninstall test-nginx
Was this page helpful?
Updated on 29 October 2024

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support