Quick-And-Simple-Introduction-to-Kubernetes-Helm-Charts-in-10-minutes

Kubernetes Helm Charts Tutorial: A Comprehensive Guide

In this tutorial, we will introduce you to the best Helm charts and walk you through the process of creating, deploying, and managing your own Helm charts in Kubernetes.

Kubernetes is an excellent platform for running your container applications, but it lacks application state descriptions using manifest files. Luckily for us, the Kubernetes community developed a fantastic tool that allows us to stop struggling with writing manifests and concentrate on the application deployment process.

The perfect tool name is Helm. Helm for Kubernetes is the same thing as Docker for containers. It allows you to deploy a complete application with a single command.

The workflow is simple. You type the command to deploy an application (the chart), and Helm does the rest:

  • It goes to the chart repository.
  • It downloads the chart (zip archive with application deployment manifests).
  • It deploys downloaded manifests to Kubernetes.

What applications can I deploy using Helm? Thousands of them are available for free at the public Helm repository. Go to Artifact.io to check them out!

What is Helm in Kubernetes?

Before we dive into getting started with Helm charts, let’s first understand what Helm is and why it is essential in Kubernetes deployments.

Helm is the application manager, which helps to manage Kubernetes applications. Helm allows you to define, install, and upgrade even the most complex Kubernetes applications. Helm for Kubernetes is like yum for CentOS or apt for Ubuntu.

Here’s Helm’s workflow visualization:

Quick-And-Simple-Introduction-to-Kubernetes-Helm-Charts-in-10-minutes-Helm-Workflow-1

Why Helm

Helm provides three significant benefits to the process of service deployments to the Kubernetes platform:

  • Deployment speed – you can deploy any application available at the Helm chart repository within a single command.
  • Prebuilt application configurations – Helm allows you to install community-supported applications with ease.
  • Easy rollbacks – Helm allows you to easily roll back your application deployment to the previous version if something goes wrong.

At the same time, Helm maybe not be the best choice for application deployments with specific requirements.

Installing Helm

Installing Helm on your system is an easy task.

Install Helm On Ubuntu, CentOS, And macOS

Here are the commands to install Helm for non-Windows users:

curl -fsSL -o get_helm.sh \
    https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh

Install Helm On Windows

To install Helm on your Windows operating system, you need to use Chocolatey:

choco install kubernetes-helm

Please check out Helm installation instructions from the official documentation for other installation instructions.

To verify the helm installation execute the following command:

helm version

Here’s the correct output:

helm-version-command

After installing Helm on your system, you can start deploying Helm charts to your Kubernetes cluster.

You can create a Helm chart from scratch, use an existing chart from the official Helm repository, or even create your own Helm charts repository to store your custom charts.

Kubernetes Helm Charts deployment

Let’s take a look at how to install applications using the Helm. But before that, it is worth looking at the chart structure to understand what’s happening behind the scenes.

Helm Chart Structure

Every application in the Helm world is ready for distribution as a package. This package is called the Helm chart.

This chart is a bundle of YAML files. Let’s take a look at the Helm charts-repo-actions-demo repository example:

git clone https://github.com/helm/charts-repo-actions-demo
cd charts-repo-actions-demo/charts/example-v2
tree
Helm-chart-structure

Here are the most important elements:

  • The templates/ directory is for Kubernetes app deployment template files.
  • The values.yaml file is storage of default values for your template variables to templates.
  • The Chart.yaml file contains a description of the chart.

Add Repository To Helm

Helm helps you to construct your application using its charts as building blocks.

But before searching or downloading Helm charts for your application, you must update the repositories list.

The Helm repository is the place where the charts are stored. It is like a DockerHub for Docker containers.

There can be two types of repositories:

  • Public repository.
  • Private repository.

By default, there are no repositories present in your local helm configuration. To add the repository, execute the following command:

helm repo add stable https://charts.helm.sh/stable

Here:

To list all available repositories, run the below command:

helm repo list
helm-repo-list-command

Now, for the demo, let’s add bitnami/charts too. Bitnami maintains a very decent amount of open-source packages.

Bitnami-Helm-Charts

To add the Bitnami charts repo, run the below command:

helm repo add bitnami https://charts.bitnami.com/bitnami

Searching Applications In Helm Repository

To search charts for the application you need, type the following command:

helm search repo 

Download Helm Chart

To download the chart for the particular application, execute another command:

helm pull

In our example, we are going to install WordPress using the Helm chart, which is available in the Bitnami repo:

helm pull bitnami/wordpress

To get the chart README, execute the following command:

helm inspect readme bitnami/wordpress
helm-inspect-readme-command

To view the complete content of the chart, run the command:

helm inspect all bitnami/wordpress | less

Install Helm Chart

Now, let’s install WordPress by typing the following command:

helm install wordpress bitnami/wordpress --set service.type=NodePort

I’m using –set service.type=NodePort in the command to change the Kubernetes service type, as I’m executing my examples at minikube and not at the real cluster.

To verify that WordPress has been successfully deployed, let’s run:

helm ls

Here’s the expected output:

helm-ls-command

As you can see, the WordPress application has been successfully deployed.

Get Information About Helm Deployment

To see the resources created by the helm chart, use the following command:

kubectl get all

Here’s the expected output:

kubectl-get all-command

In addition to CLI tools, especially for beginners, it is worth using Lens to manage your Kubernetes clusters:

Kubernetes-Lens

Click on the wordpress container and the link to port 80 to get access to the WordPress service:

Kubernetes-Lens-WordPress

Open the URL in the browser of your choice to access the application

Kubernetes-Default-WordPress-page

Now, you can see that Helm helped us to deploy WordPress in less than 5 minutes.

Delete Helm Deployment

To stop and delete the deployed WordPress, run the following command:

helm delete wordpress

To verify that the chart has been deleted, execute:

helm ls

Here’s an expected outcome:

helm ls-command-after-cleanup

Creating Helm chart

In the previous section, we deployed an already existing Helm chart. It was a very simple example to give you a taste of technology.

Let’s try to make it more real-life and create our custom chart. In our article “How to build Anaconda Python Data Science Docker container” we created Docker containers for Machine Learning tasks.

Let’s build a Helm chart to deploy this container to Kubernetes. All the sources are available in our GitHub repository.

Helm Chart Template Structure

Helm allows us to create a templated structure for our future chart by running:

helm create python_data_science

Here’s the created folder structure:

tree python_data_science
helm-chart-python-for-data-science-structure
  • charts – in this folder, we specify the separately managed dependencies for the application. For example, if the application uses the database, we can declare it as a separate chart in this folder. Helm provides various tools for managing dependencies between Helm charts, making it easy to keep track of the relationships and configurations of your application’s components.
  • Charts.yaml: here, we need to specify our chart’s details like name, version, or description.
  • templates – under this folder, we create templated Kubernetes deployment manifests that will be used for your application. You can parameterize those templates. During the deployment, Helm will take parameter values from the values.yaml file, populate templates, and produce the final manifests for deployment to the cluster.
  • tests – under this folder, we can create the tests for our charts, for example, to test the integration between our charts.
  • values.yaml – in this file, we can specify the default values for the variable we’re using in the charts.

Porting Docker Container To Helm

Delete the following files from the templates folder:

  • hpa.yaml.
  • ingress.yaml.
  • NOTES.txt.
  • serviceaccount.yaml.

Here’s what you should stay with:

helm-chart-template-step 1

We’ll use a simple service for this demo: Kubernetes Service and Kubernetes Deployment.

Here’s the content for Kubernetes Service (templates/service.yaml):

apiVersion: v1
kind: Service
metadata:
  name: python-data-science-notebook
  labels:
    app: python-data-science-notebook
spec:
  type: {{ .Values.service.type }}
  selector:
    app: python-data-science-notebook
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8888
      nodePort: 30036

Now, we can make a Kubernetes Deployment declaration (templates/deploymnet.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: python-data-science-notebook
  labels:
    app: python-data-science-notebook
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: python-data-science-notebook
  template:
    metadata:
      labels:
        app: python-data-science-notebook
    spec:
      containers:
        - name: python-data-science-container
          image: {{ .Values.image.name }}  
          ports:
            - containerPort: 8888

Finally, we need to declare the default values for our template parameters (values.yaml):

replicaCount: 1
image:
  name: amaksimov/python_data_science
  pullPolicy: IfNotPresent
  tag: "latest"
service:
  type: NodePort
  port: 80

The values from the values.yaml will be rendered into the templates in the templates folder during Helm installation.

Now type the below command in the terminal to install the chart:

helm install python-data-science-notebook python_data_science/

To see the resources created by the chart, execute the following command, or use Lens ?:

helm-install-python-data-science-notebook-command
kubectl get all

Here’s an expected output:

kubectl-get all-after-python-data-science-notebook-deployment-command

Type the command below to get Kubernetes Service IP and port to connect to the application:

export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}") && echo " URL: http://$NODE_IP:30036"

Here’s our Machine Learning Jupyter Notebook up and running at Kubernetes:

Machine-Learning-Jupyter-Notebook-launched-in-Kubernetes-using-Helm

Upgrading Helm Chart

Now, what should you do to change something in your application? Let’s say you want to increase the number of pods in the nodes by changing the replica set.

You can do it using a single command too.

Make the required changes in your Helm chart and run the following command to upgrade it:

helm upgrade python-data-science-notebook python_data_science/

Helm will immediately tell you that the revision of your deployed application increased:

helm-upgrade-python-data-science-notebook-command

Now you can see that the revision of the app is now 2.

Rollback Helm upgrade

Sometimes you may find that new rolled-out changes break the application. You can revert the application changes to the old version in this case.

To do that, type the following command:

helm rollback python-data-science-notebook 1

Where:

  • python-data-science-notebook is the application name.
  • 1 is the previous version number.

Yes, so simple!

helm-rollback-python-data-science-notebook-command

Delete Helm Application

To clean up the deployment and delete the application, run the following command:

helm uninstall python-data-science-notebook

Helm charts best practices

Now that we have covered the basics of creating, configuring, and deploying Helm charts, let’s explore some best practices for managing Helm charts and maintaining your Kubernetes applications:

  1. Use a clear and descriptive naming convention: Choose a meaningful and concise name for your Helm chart that reflects its purpose. This will make it easier for others to understand the function of the chart and its components.
  2. Follow the standard directory structure: Adhere to the standard Helm chart directory structure, which includes a Chart.yaml file, a values.yaml file, a templates/ directory, and an optional charts/ directory for dependencies.
  3. Leverage template functions and variables: Use Helm’s built-in template functions and variables to create dynamic and reusable templates. This will allow for greater flexibility and easier maintenance of your charts.
  4. Provide detailed documentation: Include a comprehensive README.md file explaining the chart’s purpose, its dependencies, and how to configure and deploy it. This will help users understand how to use your chart effectively.
  5. Keep your charts DRY (Don’t Repeat Yourself): Avoid duplicating code or configurations within your chart. Instead, create reusable components using template helpers, variables, and partials.
  6. Use semantic versioning: Follow semantic versioning practices when updating your charts. This will help users easily identify the compatibility and changes introduced in each new version.
  7. Validate input values: Ensure your chart checks for required values and validates input data types and formats. This will help prevent errors during deployment and ensure that users provide the correct information.
  8. Make your chart configurable: Provide a values.yaml file with default values for all configurable parameters in your chart. This allows users to customize the chart to suit their needs easily.
  9. Manage dependencies: Use Helm’s built-in dependency management features to define, version, and maintain dependencies between charts. This will help keep your charts modular and easy to maintain.
  10. Test your charts: Develop and use test cases to validate the functionality and configurations of your chart. This will help ensure that your chart works as expected and is error-free.

By following these best practices, you can create Helm charts that are easy to use, maintain, and scale, ultimately streamlining your Kubernetes deployments.

FAQ

Is Helm the same as Docker?

Helm is not the same as Docker. Helm is a package manager for Kubernetes that helps you install and manage applications on your cluster, while Docker is a container platform that allows you to package, deploy, and run applications in isolated containers.

What is the purpose of a Helm chart?

The purpose of a Helm chart is to provide an organized way to manage and deploy applications or services in Kubernetes clusters. A Helm chart provides all the necessary information to deploy a complete application, including its container images, configuration settings, and dependencies. Helm charts also help streamline deployments by providing a single package that contains all the components needed for application deployment.

Helm chart vS. Kubernetes YAML

Helm simplifies the deployment and management of Kubernetes applications by using Helm charts, which are a collection of files describing the application’s resources, configurations, and metadata. This offers a more efficient and flexible alternative to manually writing Kubernetes YAML files.

Summary

This article covered the beginner’s introduction to the Helm, its installation process, and its most commonly used commands. You also learned how to create the Helm chart for your application. If yes, please, help us spread it to the world.

I hope you found this article useful. If you still have questions, please, feel free to ask them in the comments section below.

Similar Posts