Helm Chart: a Beginner’s Take
Few important points:
- I have used Docker for Desktop on personal laptop(windows) and enable Kubernetes on it. You can refer to article1 and article2 for the same. I strongly recommend you to enable it as it will help you a lot for development related work.
- I have used helm v3.7.1 for the sample code used in this article. ou can refer this link to install Helm CLI.
- The sample code used in this article can be found here.
- The sample app uses ingress and to install it with Docker Desktop you can run the below command. If you want to know in more detail please follow this article.
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.40.2/deploy/static/provider/cloud/deploy.yaml
What is Helm?
As per the Helm’s official website, Helm is the Package Manager for Kubernetes. It is the best way to find, share and use software built for Kubernetes.
Now what does that mean in simple words. Let’s understand. While developing a distributed application, you might need to setup sql database or install and configure Prometheus for observability or ELK stack for logging. In all these scenarios, you need to create a lot many resources like services, configmaps, secrets, users etc. You will need to write a lot many yaml files.
Don't you think that all of this is a common steps and someone might have already done this. Or after setting it up you only can share with other teams/people who can reuse it, something like a nuget in dotnet, npm in node. In case of Kubernetes the answer is HELM: the Package Manager for Kubernetes.
For most of your use cases, ready to use package of yamls as helm charts can be find here. For example, to deploy Kafka on your Kubernetes cluster you just need to run these 2 commands:
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install my-release bitnami/kafka
Just two commands and your Kafka instance will be up and running. You can add parameters to specify how you want to configure your Kafka instance. You can find more details about Kafka helm chart here. You can refer this link to install Helm CLI.
Sample Helm Chart
Now what if you want to create your own set of yaml file package, your own Helm chart. The answer is yes you can do. If you are working with microservices then you will come across this scenario very often.
While creating an application(specifically microservice in distributed application) which has to be deployed to Kubernetes, you need to configure/create many different resources. As a minimal setup, following will be required:
- deployment
- replicaset
- service
- configmap
- secret
- ingress
And make a note, all of this will be required for each one of the microservices. And if you have multiple environments, then you need it for all the environments(dev, stage, prod etc.). Now what solutions you have to create all these yaml files.
- Write each of these yaml files one by one separately.
- Use Kustomize to create base yaml and then override it for different environments.
- Use helm to create basic template file(s) and then use values.yaml to provide desired values for different microservices and different environments.
In the below section I have explained the third approach: the Helm way. I have created a sample python app to demonstrate how Helm can be used. You can find the code here. Find below the directory structure.
To create a new Helm chart you can use the command helm create:
helm create sample-app-v1
This will create a folder structure similar to what you have seen in the picture above except for python-docker. This folder I have added separately to demonstrate how charts can be used for microservices.
- Chart.yaml :A YAML file containing information about the chart.
- templates/ : A directory of templates that, when combined with values(values.yaml) will generate valid Kubernetes manifest files. By default Helm creates a standard set of Kubernetes resources’ yaml files. I have deleted that and created my own set of resources’ yaml files.
- charts/ :A directory containing any helm charts upon which this current helm chart depends.
- values.yaml :The default configuration values for this chart.
The yaml files in templates are self-explanatory(if you are aware of Kubernetes) It consists of a namespace, a deployment, a config, a service and an ingress. You can define your own set of base resources which are common for microservices. Then later define different set of values.yaml(e.g. values.dev.yaml, values.prod.yaml etc) which can be used to generate different set of Kubernetes manifest files. If you want to verify your complete set of manifest yamls, you can use below command to locally render it.
#helm template <chart-name> <chart-folder-path> --values <values-yaml>
helm template sample-app-v1 .
helm template sample-app-v1 . --values values.dev.yaml
If values are not provided, the default values provided in chart folder(values.yaml) will override the values for desired parameter specified in template yamls. For example you can see the template generated without providing values in below diagram.
If you specify the values in command it will create manifest based on provided values.yaml. For example if you use values.dev.yaml, the result will look something like below:
Install helm chart
To install a helm chart you can use helm install command. If you have already install a version and want to upgrade/override to a newer version or provide new values, you can use helm upgrade install.
#helm install <chart-name> <chart-foder-path> --values <values-yaml>
helm install sample-app-v1 .
helm upgrade --install sample-app-v1 . --values values.dev.yaml
helm upgrade --install sample-app-v1 . --values values.prod.yaml
You can see in below 3 pictures how Kubernetes resources are getting created and python app is displaying different messages based on different values.yaml provided.
Every time you install/upgrade a helm chart you get a new version deployed. You can use helm list command to list all installed chart and related details. To uninstall you can use helm uninstall sample-app-v1.
For further learning about Helm, Please refer to official helm docs.
Hope this article has helped you getting some insight on how to kickstart your journey with Helm.