- What is a Helm Chart?
- Prerequisites
- Helm Chart example
- Step 1: Initialize the Helm Chart
- Step 2: Define your application in values.yaml
- Step 3: Create deployment resources in deployment.yaml
- Step 4: Create Service resources in services.yaml
- Step 5: Customize your Helm Chart in Chart.yaml
- Step 6: Package your Chart
- Step 7: Install your Helm Chart
- Step 8: Verify your Chart Deployment
- Conclusion
- Ansible shell module: Run remote shell commands - Tue, Nov 21 2023
- Install Loki and visualize logs with Grafana - Thu, Nov 9 2023
- Git reset vs. git revert - Mon, Oct 23 2023
What is a Helm Chart?
Here are some key concepts to understand when working with Helm:
Helm Chart: An application package for Kubernetes, like traditional packages for package managers such as apt, yum, or npm. It is a set of files describing Kubernetes resources, such as deployments, services, and configuration maps necessary for running an application on Kubernetes.
Release: An instance of a Chart deployed into a Kubernetes cluster with its own distinct name. It can feature custom configurations or values.
Values: Parameters that allow customized Chart deployments to suit individual needs.
Repository: Helm Charts can be stored in remote repositories for easy sharing and distribution across Kubernetes clusters.
Several YAML files define Kubernetes resources for your application. You can create or modify these files based on your application's requirements. There are two main types of configuration files in Helm:
- Template files
- Values files
Template files allow the Helm Chart administrator to specify the resources that will be created during deployment of their Helm Chart by using placeholder values that allow for customization of these newly generated resources.
Values files provide values to fill placeholders within template files, providing users with more control in creating resources tailored specifically for various environments, or providing more flexibility when dealing with changes that impact those environments.
There are two types of values files in a Helm Chart:
- values.yaml: Contains default values for the templates in the Chart. It is typically included in every Chart.
- User-supplied values files: Can be used to override the default values in the values.yaml file when the Chart is installed.
Now that we have a basic understanding of Helm, let's dive into creating a Helm Chart for packaging an application.
Prerequisites
Before we begin creating our Helm Chart, make sure you have the following tools installed:
Helm: You will need Helm installed on your local machine. You can install it using the Helm installation guide for your platform.
Kubernetes Cluster: Ensure you have a running Kubernetes cluster to deploy your application. You can set up a local cluster using tools like Minikube or a cloud-based Kubernetes service.
Helm Chart example
We'll create a Helm Chart of a simple web application using NGINX.
Step 1: Initialize the Helm Chart
The first step is to initialize a new Helm Chart. You can do this using the helm create command.
helm create mywebapp cd mywebapp
This command will create a mywebapp directory with the necessary files and directories for your Helm Chart.
Step 2: Define your application in values.yaml
The values.yaml file contains default values for the templates in a Helm Chart. It is a YAML file that is typically included in every Chart. You can use it to override the defaults when the Chart is installed.
The following are some examples of the information that can be found in a values.yaml file:
- Image name and tag for a container image
- Number of replicas for a Deployment
- Port number for a Service
- Name of a PersistentVolumeClaim
- Environment variables for a container
Below is an example of NGINX configuration values that you can configure in values.yaml:
replicaCount: 1 # Number of NGINX replicas to deploy image: repository: nginx tag: latest # NGINX Docker image tag pullPolicy: IfNotPresent # Image pull policy service: port: 80 # Port to expose the NGINX service on
Step 3: Create deployment resources in deployment.yaml
The deployment.yaml file in Helm Charts is used to define Kubernetes Deployment resources, which manage the creation and lifecycle of replicated Pods and ensure that the desired number of pods is running at all times.
In this example, we're creating a Kubernetes Deployment for the NGINX container.
Below is an example of a deployment.yaml file.
apiVersion: apps/v1 kind: Deployment metadata: name: my-deployment spec: replicas: 2 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80
Step 4: Create Service resources in services.yaml
The services.yaml file in Helm Charts is used to define Kubernetes Service resources, which expose deployed applications to other applications and Services within the cluster. To add a Service to the Chart, you need to create the file in the templates directory. The contents of this file will look like this:
apiVersion: v1 kind: Service metadata: name: my-service spec: selector: app: my-app ports: - port: 80 targetPort: 80
This Service exposes the application on Port 80. The selector field specifies that the Service should only be exposed to pods that have the label app=my-app.
Step 5: Customize your Helm Chart in Chart.yaml
Chart.yaml is a YAML file that contains metadata about a Helm Chart. It is required for every Chart. The Chart.yaml file is used by Helm to determine the Chart's compatibility with the Kubernetes cluster, install the Chart's dependencies, and generate the Kubernetes resources that will be deployed.
It is important to note that the Chart.yaml file does not contain any templates or values. These are stored in separate files in the Chart directory.
The following fields are available in the Chart.yaml file:
- apiVersion: The API version of the Chart
- name: The name of the Chart
- version: The version of the Chart
- kubeVersion: A SemVer range of compatible Kubernetes versions
- description: A single-sentence description of the Chart
- type: The type of the Chart
- keywords: A list of keywords about the Chart
- home: The URL of the Chart's home page
- sources: A list of URLs to source code for the Chart
- dependencies: A list of the Chart's dependencies
It must be located in the root directory of the Chart.
Here is an example of a Chart.yaml file:
apiVersion: v2 name: mywebapp description: Nginx Helm Chart for Kubernetes version: 1.0.0 kubeVersion: ">=1.19.0" description: A Helm Chart for my application. type: application keywords: [my-application, helm-chart] home: https://example.com/my-application sources: - https://github.com/example/my-application dependencies: - name: nginx version: "1.23.1"
Step 6: Package your Chart
Once you've defined your Kubernetes resources and customized your Helm Chart, you can package it into a compressed Chart archive (.tgz) using the helm package command:
helm package mywebapp
This command will create a .tgz file in the current directory, which contains your Helm Chart.
Step 7: Install your Helm Chart
Now that your Helm Chart is packaged, you can install it on your Kubernetes cluster using the helm install command. Replace mywebapp-chart with the desired release name:
helm install mywebapp-chart ./mywebapp-0.1.0.tgz
This command will deploy your application to the Kubernetes cluster using the values specified in the values.yaml file.
Step 8: Verify your Chart Deployment
You can verify that your application is running using Kubernetes commands. For example, to list all the running pods in your cluster:
kubectl get pods
Now, you can access your NGINX web application in your browser at http://127.0.0.1:8080.
Subscribe to 4sysops newsletter!
Conclusion
This article introduced the concept of Helm Charts in Kubernetes. Please note that I only scratched the surface of this powerful tool for deploying applications. For a deep dive into Helm Charts, please refer to the official documentation.
Read the latest IT news and community updates!
Join our IT community and read articles without ads!
Do you want to write for 4sysops? We are looking for new authors.