Learn to build a simple Helm chart from scratch, utilizing templates for configurable Kubernetes resource names and automating package installations.
In this guide, you’ll learn how to build a simple Helm chart from scratch. We will demonstrate how Helm templates work to create unique and configurable Kubernetes resource names. Helm charts are extremely versatile—they not only automate the installation of Kubernetes packages but also perform additional tasks (like backing up a database before upgrades) much like installation wizards on traditional operating systems.For example, consider an upgrade command such as:
In this section, we will create a Helm chart for a simple “Hello World” application. The application will use an Nginx deployment with two replicas and expose the service through a NodePort.Below are the Kubernetes manifest files for our Hello World application:
Helm charts adhere to a specific directory structure that typically includes the templates/ folder along with files such as Chart.yaml, values.yaml, LICENSE, and README.md.
You do not need to manually create this structure. The Helm CLI can generate a skeleton chart for you:
Copy
Ask AI
$ helm create nginx-chart$ ls nginx-chartcharts Chart.yaml templates values.yaml
Now, you can replace or add your own Kubernetes manifest files (for example, the deployment and service files shown above) into the templates/ directory. Initially, the generated Chart.yaml file contains default data based on the provided chart name.
At this stage, you might want to update the Chart.yaml file to include more detailed metadata. For instance, if your company is developing this chart for internal use, you can update the file with a more descriptive summary and add maintainer contacts.To open and edit the file:
Copy
Ask AI
$ cd nginx-chart$ vi Chart.yaml
The original content may resemble this:
Copy
Ask AI
apiVersion: v2name: nginx-chartdescription: A Helm chart for Kubernetestype: applicationversion: 0.1.0appVersion: "1.16.0"
Modify it to add details and contact information:
Copy
Ask AI
apiVersion: v2name: nginx-chartdescription: Basic nginx websitetype: applicationversion: 0.1.0appVersion: "1.16.0"maintainers: - name: john smith email: [email protected]
Once you have updated the metadata, remove any unnecessary sample template files from the templates/ directory:
Copy
Ask AI
$ cd nginx-chart$ ls templatesdeployment.yaml _helpers.tpl hpa.yaml ingress.yaml NOTES.txt serviceaccount.yaml service.yaml tests
For your simple application, add your custom deployment and service YAML files to this folder, and your chart will be ready for installation.
When you install a Helm chart, the objects in the templates are created exactly as defined. For example:
Copy
Ask AI
$ helm install hello-world-1 ./nginx-chart$ kubectl get deploymentNAME READY UP-TO-DATE AVAILABLE AGEhello-world 0/2 2 0 24s
Since the deployment name is hardcoded as hello-world, installing another release of the same chart leads to name conflicts:
Copy
Ask AI
$ helm install hello-world-2 ./nginx-chartError: rendered manifests contain a resource thatalready exists. Unable to continue with install:Deployment "hello-world" in namespace "default" existsand cannot be imported into the current release:invalid ownership metadata; annotation validationerror: key "meta.helm.sh/release-name" must equal "hello-world-2"; current value is "hello-world-1"
To avoid conflicts, leverage Helm’s templating language to create dynamic names based on the release name. For instance, update your service and deployment definitions as follows:
Now, when you install the chart using different release names, Helm replaces the template directives (e.g., {{ .Release.Name }}) with your specified release name:
Customization is key for production-ready charts. Often, you might want to configure deployment attributes—such as the number of replicas or the container image—to suit different environments. The values.yaml file serves this purpose by storing default configurations that your templates can reference.Consider the following simple values.yaml:
Copy
Ask AI
# Default values for nginx-chart.replicaCount: 2image: nginx
Your deployment template then references these values:
When you install a Helm chart, Helm processes the templates in your templates/ directory together with several sources of configuration:• Release-specific details (such as release name, namespace, and revision)
• Default values defined in values.yaml
• Metadata from Chart.yaml
• Information from your Kubernetes clusterThe resulting manifest files are then used by Kubernetes to deploy your resources. By designing templates with dynamic naming (using {{ .Release.Name }}) and configurable values (via values.yaml), you guarantee that each chart installation creates uniquely named objects and can be tailored easily by users.Happy templating, and see you in the next lesson!For more information on Helm, visit the Helm Documentation.