Package and Deploy Your Application Using Helm Chart

0_vHdn3nKjGh-xLIZ9.jpg

I was given a task in my project to set up a CI-CD pipeline on GCP using helm to package k8s objects and deploy them on the GKE cluster. Having very little exposure to DevOps, I did my hands dirty and learned about helm which I am going to share in this blog. What will I do? I am going to show an end to end creation of spring boot application to deploy it as a helm chart on a minikube cluster.

Agenda:

1. Create an spring boot app

2. Create docker image

3. Creating k8s objects

4. Create helm chart

5. Deploy them to minikube cluster

Although we don’t need to create k8s objects,but it will make sense while learning helm. I am assuming that reader has a basic understanding of docker. If not please read below blog.

What is helm chart?

From the official page of helm, they define it like this.

Helm helps you manage Kubernetes applications — Helm Charts help you define, install, and upgrade even the most complex Kubernetes application.

Charts are easy to create, version, share, and publish — so start using Helm and stop the copy-and-paste.

Don’t worry, if it seems a bit abstract. I will explain with an analogy.

what docker is for source code, helm is for k8s objects.

We create and share docker image for any application to run on different environments without thinking about various configuration, dependencies, installing other softwares or tools etc. We get the docker image of any app or database, override the default configuration if required and start consuming it. Now think of sharing source code to someone, he or she has to do a lot of prerequisite before it starts. In the same way, helm helps us to get rid of creating multiple k8s objects for same application to run on multiple environments and with one single command it will deploy your k8s application to cluster.

Think of what you need to do, if youwant to run apache kafka on a k8s cluster. You will download kafka, install it, create k8s objects according to various environments and apply kubectl command. Now using helm, you just need to pull helm chart of apache kafka from helm repo of any trusted vendor and apply helm install command with overriding default variables depending on environments.

In this way, it is pretty easy to use with helm chart. No download, no install, no k8s objects all gone.

So now, see helm in action by creating a spring boot app and deploy it to minikube cluster using helm chart.

In this way, it is pretty easy to use with helm chart. No download, no install, no k8s objects all gone.

So now, see helm in action by creating a spring boot app and deploy it to minikube cluster using helm chart.

Get Spring Boot Application

I have created a simple spring boot application where if you hit /hello it will return Hello World! Please download it from here.

It contains a class HelloWorldController with and endpoint /hello returning a string Hello World!.

You will also find Dockerfile so that we can run this as a docker container. Try running app alone and as a docker container by going inside your project and running below commands.

  • docker build . -t helm-chart-demo-docker-img
  • docker run -p 9899:9899 helm-chart-demo-docker-img

Then hit localhost:9899/hello, it should return Hello World! in each case.

So our app is running on its own(spring provides embedded tomcat server) and as a docker container. Now let us run it inside our k8s pod.

Create And Deploy K8s objects

From Spring Boot k8s page I found this definition of Kubernetes,

It is an open-source system for automating the deployment, scaling, and management of containerized applications. It groups containers that make up an application into logical units for easy management and discovery.

In simple terms, k8s can manage large number of docker(or other) containers, auto scale it up/down , deploy it, make sure in any case your desired configurations are met.

If you are a newbie to k8s and want to know an intro, follow this guide.

To run our app inside k8s cluster, we need minikube on our local machine. To install minikube, follow section from 1 to 3 on this page.

Minikube creates a single node cluster on our local machine.It is like a VM on our machine.

Now if you check our spring boot app directory carefully, you we see k8s directory containing deployment.yaml and service.yaml files. These are basic minimum two k8s objects we need to deploy our app inside k8s cluster. Deployment.yaml contains configuration of docker image, database connection details etc. Service.yaml make configuration to expose our application outside world. In short, deployment.yaml deploy our app and service.yaml expose our app to outside world. For more depth knowledge on this two file follow this small blog.

Now let us try to run our app inside minikube cluster. Make sure you are at root of your project. Please follow below steps make deployment.

  1. Start minikube by running minikube start
  2. eval $(minikube docker-env) by running this command we asking docker client to use docker service inside our minikube VM.
  3. Create docker image of our application
  • docker build -t helm-chart-demo-docker-img .
  1. Apply deployment by running below command
  • kubectl apply -f k8s/Deployment.yaml
  1. To know about status of deployment run this command
  • kubectl get deployments
  1. To know pods details run this command
  • kubectl get pods
  1. To see pod logs run below command
  • kubectl logs <pod naem from above command>
  1. In the same way apply service
  • kubectl apply -f k8s/Service.yaml
  • kubectl get services
  1. Now to hit api /hello run this command
  • minikube service helm-chart-demo-svc — url

When you run the last command to get url to access our app, it gives you something like this http://127.0.0.1:57230 url.

Hit http://127.0.0.1:57230/hello to see if it works or not.

Whoa! It works!!! But, that’s lot of commands to just make deployment also we have to create k8s objects. Now imagine creating k8s objects(We created just two, there are many more which arerequired to be configured in real world development) for multiple environments and deploy it.

There is one more short command kubectl apply -f k8s/ , this deploy both or whatever k8s objects are inside this folder.

But real point is as helm said who will manage these k8s objects which in many case are repeating. There I find helm to ease our task. Let us explore to make it clear.

Magic of helm

Understanding the helm conceptually

Let us revisit the definition of helm again.

“Helm helps you manage Kubernetes applications — Helm Charts help you define, install, and upgrade even the most complex Kubernetes application.
Charts are easy to create, version, share, and publish — so start using Helm and stop the copy-and-paste.
A Chart is a Helm package. It contains all of the resource definitions necessary to run an application, tool, or service inside of a Kubernetes cluster.”

Now focus on key words like

  • Manage Kubernetes application
  • Helm Charts help you define, install, and upgrade even the most complex Kubernetes application
  • Charts are easy to create, version, share, and publish

So using helm chart you can define k8s configuration like we have done in deployment.yaml and service.yaml, install like we did by running kubectl apply command, upgrade means installing new versions.

Charts are easy to create, version, share, and publish just like docker images. Like docker it also has repository where ready made charts are available to use.

Now having a fundamental understanding of helm, it’s time to create helm chart of our spring boot app.

Chart File Structure

In out application directory, there is a folder called helm-chart-demo, just go through it. You will see folder structure like this.

Let us understand the intent of each file.

  • .helmignore — Here you can add pattern of files to ignore while building chart packages.
  • Chart.yaml — This file contains information about chart like type of chart, dependencies, versions etc.
  • Values.yaml — This file contains all the values which are default for the configuration.
  • Templates — This directory contains templates for configuration like in our case deployment.yaml and service.yaml. These templates when combine with values.yaml will generate k8s objects configuration.
  • NOTES.txt and _helpers.tpl — For now we can ignore these two files as two are anyway not going to contribute in creating k8s configuration by helm.

If you see deployment.yaml and service.yaml, they are similar to k8s/Deployment.yaml and k8s/Service.yaml with the exception that now in these files we are referring values from values.yaml file. We are not hard wired values here like k8s/Deployment.yaml and k8s/Service.yaml.

So what helm did, it is outsourcing values from values.yaml that is default location for values. When you deploy helm chart you can override these default values by various means.(We see later.)

But one question you ask, how did you create these files. So answer is there is a command provided by helm to create a default configuration and directory files.

                helm create <chart-name>
            

When you run this command then you will see a directory with named <chart-name> and other files and folders within it.

For example if run helm create test-helm , following directory structure with default values and configuration gets created.

So what we can do, we can delete not required files and modify required files as per our needs. That’s what I did in our application helm-chart-demo folder.

After understanding what helm is and how to create helm configuration, now let us create chart and deploy it.

How to deploy helm chart

After having you helm configuration ready you can always dry run to see if configuration files are correct. Make sure your minikube cluster is up.

                helm install <release-name> helm-chart-demo –dry-run
            

If all things go correct, it will return the configuration. Try running it.

Now let us deploy it. In helm universe a deployment is called release.

A Release is an instance of a chart running in a Kubernetes cluster like container is running instance of docker image.

Let us call our release by name helm-char-demo-r-1

                helm install helm-char-demo-r-1 helm-chart-demo
            

Run helm status helm-char-demo-r-1 to see status of your deployment. It will return something like below.

So status is successful, Hurray! . We have done it. Now stop a bit think the differences you feel while making deployment using plain k8s objects and using helm.

So now you can use kubectl command to see status of services,pods, deployment objects and that url command to fetch a url to hit /hello to double confirm if everything is right. Go ahead and try it.

Overriding default values in helm

Let us now see how to override the default values if we try to deploy it again on the same environment or other. Right now we have configured replicaCount value to 1. Let us change it and set it to 2.

Try running

                helm install helm-char-demo-r-1 helm-chart-demo –set replicaCount = 2
            

And check number of pods deployed by using kubectl get pods it should show 2 pods.

Note : If you face error Error: INSTALLATION FAILED: cannot re-use a name that is still in use ,then use

                helm upgrade helm-char-demo-r-1 helm-chart-demo –set replicaCount= 2
            

this command will upgrade earlier release with changed values.

But here issue is what if we have large set of values to be override ?

Then you can create a yaml file with updated values and refer it with -f or –values flag while making release or upgrading earlier releases.

                helm upgrade helm-char-demo-r-1 helm-chart-demo –f <path-to-yaml-file>
            

Working with helm repository

  • helm repo list — List all repos your system connected with
  • helm repo add <Repo-Name> <URL> — will add helm repo to pull various chart from <URL>
  • helm search hub <search-text> — searches the Artifact Hub, which lists helm charts from dozens of different repositories.
  • helm search repo <search-text> — searches the repositories that you have added to your local helm client (with helm repo add).
  • helm repo remove <Repo-Name> — Will remove the added repo to helm client

Some useful helm commands.

  • helm package <path-to-helm-chart-config> — package the chart in a .tgz file which can be installed by helm install <release-name> <path to .tgz file>
  • helm uninstall <release-name>- uninstall a release
  • helm show values <path-to-chart>- show the values from values.yaml file of chart
  • helm show all <path-to-chart> — show values as well chart details
  • helm create <chart-name> — create helm default directory with default config and values
  • helm install <release-name> <path-to-chart> –dry-run — command to see if your configuration has any error
  • helm install <release-name> <path-to-chart> — install a release of chart
  • helm status <release-name> — show status of release
  • helm list — list all release made
  • helm lint <path-to-chart>- runs a series of tests to verify that the chart is well-formed.
  • helm upgrade <release-name> <path-to-chart> — upgrade earlier release with updated chart
  • helm upgrade <release-name> <path-to-chart> –f <path-to-yaml-file> — upgrade earlier release with updated chart and configuration provide in yaml file
  • helm upgrade <release-name> helm-chart-demo –set <key-name> = <value>- upgrade earlier release with updated chart and updated value with –set flag
  • helm get values <release-name>- show the new values if any new values has been applied using –set , -f or –values flag.

Thank you so much for reading. Follow to get updates for such blogs. Clap and share.


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies and get more readers

Join other developers and claim your FAUN account now!

Avatar

Amit Kumar

@infinity__kumar
Learner, Developer, Blogger and DTUite
Stats
15

Influence

697

Total Hits

1

Posts