Introduction
Application deployment in Kubernetes is a bit complex. Many YAML files are required to deploy different resources inside the cluster for a single application.
Helm is here to solve those issues and make the deployment of an application as simple as possible.
What is Helm?
Helm is a Package Manager for Kubernetes that makes it easy to configure and deploy applications into Kubernetes clusters. Helm is similar to package managers like apt, pip, yum, etc.
Helm is capable of:
- Installing and Upgrading Applications
- Fetch Application Packages from Repositories
- Configure Application Deployments
Understanding a Helm Chart Package
A Helm Package is also known as a chart. It consists of YAML Configuration files and templates that are rendered into a Kubernetes Manifest File. This is the basic directory structure of a Helm chart.
helm-chart/
Chart.yaml
templates/
Values.yaml
Here,
Chart.yaml
= It's a YAML file with metadata about the chart like chart name, version, maintainer, description, etc.
Values.yaml
= It consists of configuration values for the chart, which are modifiable.
templates/
= This directory has files that are used to define Kubernetes resources. These files are used in conjunction with the values.yaml
file.
Installing Helm
Helm can be installed via package managers as well as binary releases. Installation commands for different systems are given below:
Brew (Linux/macOS) = brew install helm
apt (Debian) = apt install helm
yum (CentOS/Fedora/RedHat) = yum install helm
Chocolatey (Windows) = choco install kubernetes-helm
Scoop (Windows) = scoop install helm
A Bash Script is also provided by Helm to make installation easier:
Alternatively,
Creating a Helm Chart
Now that we've installed Helm let's learn the basics by creating a custom Helm Chart for Nginx WebServer.
We'll go through different steps, after which you'll be comfortable enough to start creating your own custom Helm Charts.
Happy Helming!
Step 1: Creating a Namespace
Let's create a namespace called helm-demo
in our cluster.
Switch to the newly created helm-demo
namespace.
kubectl config set-context --current --namespace=helm-demo
Now we'll create a directory to store our helm chart.
Step 2: Creating Helm Definition files
Create a Chart.yaml
file.
Add the following lines to the file.
Here:
apiVersion
= API that Helm will use to communicate with Kubernetes before helm 3 v1 is used.
name
= name of the helm chart.
version
= Helm Chart Version.
appVersion
= Application deployment version.
type
= Chart Type.
description
= Chart Description (will be visible during helm searches).
Step 3: Creating the Kubernetes Deployment Files
Create a directory called templates, where we will store the definition files.
Let's create a deployment file for Nginx.
We can use the fry run argument in kubectl to easily generate a template for our deployment and service files.
That will create a basic configuration for the deployment.
Now let's create a service file for Nginx.
The directory structure should look something like this now!
Step 4: Installing the Helm Chart
Now let's install the chart in the Kubernetes Cluster.
Here,
nginx-helm-demo
= Name of the Chart to be Deployed
$HOME/helm-chart-demo/charts/nginx-helm-demo
= Path to Chart
Run the following command to get the status of objects in our cluster
But this is not all. The reason we use Helm is because of the extensibility it provides in customizing the values of our deployments
Customizing the Helm Deployment
Let's create a values.yaml
file in the nginx-helm-demo directory
We'll try to customize the value of the replica set and service port.
Add the following to the values file.
Now we'll customize the deployment files with reference to these values so that the helm controller knows where customization is required.
In the Nginx deployment file, customize the spec for replicas as follows.
And in the Nginx service file, customize the spec for the port as follows:
Upgrading the Helm Chart
The changes that we made to the configuration can be deployed easily using the helm upgrade command. As we've already installed the helm chart before this command will update the configuration of the existing chart.
If you've left the terminal for watch
open, you should see that a new pod has been deployed, and the service port has been changed to 85.
Let's change the value back to 80 again!
Let's again upgrade the helm chart.
The Service Port has now been changed to 80 with the latest upgrade!
Let's port-forward and check if the deployment and service are working as intended!
Helm Rollback
Our Helm Chart has deployed the required resources we wanted, but what if an unintended change was performed? Can it be reverted?
Yes! Helm allows us to switch between different versions of a chart as required.
For this example, let's deploy back to version 1, where only one pod was present.
Here,
1
= The version to revert to (It can be any version that you've already deployed before)
The helm rollback was a success, and we only have one pod running in our cluster!
To display installed charts, we can run the following command:
Uninstalling the Helm Chart
To Uninstall the Chart, we simply run.
The resources deployed into the cluster will now be deleted!
In this blog, we reviewed Helm, a package manager for Kubernetes. We learned about the fundamental components that make up a Helm Chart. We also learned to create helm charts, perform upgrades, roll back to a different version and uninstalled the created chart!
So this is it for the blog on Helm basics, I'll be back with more Helm content later on! Until then Happy Helming!