Deploying to Kubernetes using Helm2
By: Madeesha S Tech Space
Tags: Deployment, Helm, Deploy, Chart, Kubernetes

This post will guide you to create and deploy Kubernetes resources to your cluster using Helm. If you know nothing about helm, that’s fine. It won’t be a problem at all. At the end of this article, you will have a piece of good knowledge of how to work with helm.

What is Helm?

Helm is the package manager for Kubernetes. Helm allows you to develop and maintain even the most complex deployments very easily. Believe me, it is very easy. Okay, let’s go ahead. Helm uses a packaging format called charts. A chart is a collection of files that describe a set of Kubernetes resources. A chart can be used to deploy a single resource or multiple resources.

Say you need to deploy a Kubernetes pod and a service in your cluster. If so what did you do? you will write a deployment.yaml and a service.yaml files and deploy them using the below command.

kubectl apply -f <file-name>

Since you have two files you need to execute the above command twice. In the case, you need to deploy 50 Kubernetes resources, you have to execute the above command 50 times for each resource file. However, If you have a single helm chart containing all those 50 resource files, you only need to execute a single command to deploy all those 50 files!! How easy?

When we are talking about Helm, we cannot forget the Tiller. Tiller is the Helm server which runs in Kubernetes and deploys all the helm charts. Therefore, before using helm to deploy your resources in K8S cluster, you need to install helm to your Kubernestes cluster as well. When you are installing helm to your cluster, it will deploy a “Tiller Pod” which handles all the helm packages.

How to Install Helm?

Before start using helm as your Kubernetes package manager, you need to install Helm to your local machine and install tiller pod in your Kubernetes cluster as shown in the above diagram.

Install Helm: Please use the helm official document to install Helm to your machine.

Deploy Tiller: If you can use “kubectl” command in your local machine to connect to your k8s cluster, it is very easy to deploy tiller pod in your Kubernetes cluster. Just run the below command to install tiller in your K8S cluster,

helm init

NOTE: if you encounter with the below error when running the helm init command, please use the steps mentioned in the blog to fix it. “Error: release nginx-ingress failed: namespaces “default” is forbidden: User “system:serviceaccount:kube-system:default” cannot get resource “namespaces” in API group “” in the namespace “default””

Now you have successfully configured your local machine and your Kubernetes cluster to work with Helm.

Building a Helm chart

Now you are ready to play with Helm. A chart is organized as a collection of files inside of a directory. First, create the chart structure using the below command.

helm create test-helm

Now go inside the “test-helm” directory and you can see the below structure has been created inside that directory.

├── charts

├── Chart.yaml

├── templates

│ ├── deployment.yaml

│ ├── _helpers.tpl

│ ├── ingress.yaml

│ ├── NOTES.txt

│ ├── service.yaml

│ └── tests

│ └── test-connection.yaml

└── values.yaml

Inside the templates folder, you can see two files called deployment.yaml and service.yaml. These two files will be deployed in your Kubernetes cluster if you install this helm chart in the cluster.

The most important file in this directory is the values.yaml file, because it stores variables for the template files inside the templates directory. Therefore let’s have a look at the values.yaml file first. The part of the values.yaml file is shown below which inside the test-helm directory that we created just before.

# Default values for test-helm.

# This is a YAML-formatted file.

# Declare variables to be passed into your templates. replicaCount: 1 image:

repository: nginx

tag: stable

pullPolicy: IfNotPresent nameOverride: ""

fullnameOverride: "" service:

type: ClusterIP

port: 80 ingress:

enabled: false

annotations: {}

# kubernetes.io/ingress.class: nginx

# kubernetes.io/tls-acme: "true"

hosts:

- host: chart-example.local

paths: []

When you look at the “image” block of the above file, you can see nginx:stable image is used inside. If you need to change the image of the deployment.yaml file, you simply need to change the “image” block section of the values.yaml file. Because the image details are taken from the values.yaml file to the deployment.yaml helm template file as shown below.

...

.....

spec:

containers:

- name: {{ .Chart.Name }}

image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"

imagePullPolicy: {{ .Values.image.pullPolicy }}

.......

...

As you can see, if you look at the complete deployment.yaml file, you can clearly understand how the variables inside values.yaml file are referred in the helm template files.

Likewise, if you need to deploy more resources to your cluster using helm, just insert those YAML files inside the template folder.

As you can see in the above, you can create any of the k8s resources in your cluster using helm. It will either be a deployment, a service, a role, a service account or any resource which can deploy in a k8s cluster.

Installing a Helm chart

Once you have created your helm chart, there is only just one command to install it in the cluster. Go inside “test-helm” directory we just created above and run below command,

helm install --name my-helm-deployment .

This will deploy all the YAML files you put inside the template folder. That’s why I told you at the beginning, you can deploy any number of Kubernetes resources from a single helm command.

Hope this post clears all your doubts about helm :)

Happy Helming !!!