Introduction to Helm and Creating your first Helm Chart

Prashant Lakhera
6 min readFeb 4, 2021

--

To read the complete blog

Helm is a package manager for Kubernetes. Similar to yum but for Kubernetes. It bundles all related manifests(such as deployment, service, etc) into a chart. When installing chart, helm creates a release. Benefits of helm is it provide templating, repeatability, reliability, multiple environment and ease of collaboration.

Installing Helm

$ curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 |bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 11213 100 11213 0 0 58630 0 --:--:-- --:--:-- --:--:-- 59015
Helm v3.4.1 is available. Changing from version v3.3.4.
Downloading https://get.helm.sh/helm-v3.4.1-linux-amd64.tar.gz
Verifying checksum... Done.
Preparing to install helm into /usr/local/bin
helm installed into /usr/local/bin/helm
  • Verify helm version
$ helm version
version.BuildInfo{Version:"v3.4.1", GitCommit:"c4e74854886b2efe3321e185578e6db9be0a6e29", GitTreeState:"clean", GoVersion:"go1.14.11"}
  • To check the current state of helm locally
$ helm env
HELM_BIN="helm"
HELM_CACHE_HOME="/home/cloud_user/.cache/helm"
HELM_CONFIG_HOME="/home/cloud_user/.config/helm"
HELM_DATA_HOME="/home/cloud_user/.local/share/helm"
HELM_DEBUG="false"
HELM_KUBEAPISERVER=""
HELM_KUBEASGROUPS=""
HELM_KUBEASUSER=""
HELM_KUBECONTEXT=""
HELM_KUBETOKEN=""
HELM_MAX_HISTORY="10"
HELM_NAMESPACE="default"
HELM_PLUGINS="/home/cloud_user/.local/share/helm/plugins"
HELM_REGISTRY_CONFIG="/home/cloud_user/.config/helm/registry.json"
HELM_REPOSITORY_CACHE="/home/cloud_user/.cache/helm/repository"
HELM_REPOSITORY_CONFIG="/home/cloud_user/.config/helm/repositories.yaml"
helm use the Kubernetes cluster/host via the config file
~/.kube/configHELM_DATA_HOME="/home/cloud_user/.local/share/helm"
HELM_DEBUG="false"
HELM_KUBEAPISERVER=""
HELM_KUBEASGROUPS=""
HELM_KUBEASUSER=""
HELM_KUBECONTEXT=""
HELM_KUBETOKEN=""
HELM_MAX_HISTORY="10"
HELM_NAMESPACE="default"
HELM_PLUGINS="/home/cloud_user/.local/share/helm/plugins"
HELM_REGISTRY_CONFIG="/home/cloud_user/.config/helm/registry.json"
HELM_REPOSITORY_CACHE="/home/cloud_user/.cache/helm/repository"
HELM_REPOSITORY_CONFIG="/home/cloud_user/.config/helm/repositories.yaml"
  • helm use the Kubernetes cluster/host via the config file
~/.kube/config

Advantage of using Helm

  • Traditional deployments in Kubernetes is done with Kubectl across files into separately managed items. Helm deploys units called charts as managed releases.
  • We can search for charts https://helm.sh/ . Charts can be pulled(downloaded) and optionally unpacked(untar).

Chart Repo

  • Location where Helm charts can be stored and shared
  • Respond to REST API GET requests to get packages
  • Many storage options available — cloud buckets, local storage, GH Pages, etc or we can use Chartmuseum(open source helm chart repository)
  • To show all the repos
$ helm repo list
NAME URL
istio.io https://storage.googleapis.com/istio-release/releases/1.4.2/charts/
  • To add a repo
helm repo add <repo> url
helm repo add stable https://charts.helm.sh/stable
"stable" has been added to your repositories
  • Search a repo
$ helm search repo mysql
NAME CHART VERSION APP VERSION DESCRIPTION
stable/mysql 1.6.7 5.7.30 Fast, reliable, scalable, and easy to use open-...
stable/mysqldump 2.6.2 2.4.1 DEPRECATED! - A Helm chart to help backup MySQL...
stable/prometheus-mysql-exporter 0.7.1 v0.11.0 DEPRECATED A Helm chart for prometheus mysql ex...
stable/percona 1.2.1 5.7.26 free, fully compatible, enhanced, open source d...
stable/percona-xtradb-cluster 1.0.6 5.7.19 free, fully compatible, enhanced, open source d...
stable/phpmyadmin 4.3.5 5.0.1 DEPRECATED phpMyAdmin is an mysql administratio...
stable/gcloud-sqlproxy 0.6.1 1.11 DEPRECATED Google Cloud SQL Proxy
stable/mariadb 7.3.14 10.3.22 DEPRECATED Fast, reliable, scalable, and easy t...

Search for hub

$ helm search hub |head
URL CHART VERSION APP VERSION DESCRIPTION
https://hub.helm.sh/charts/gabibbo97/389ds 0.1.0 fedora-32 389 Directory Server
https://hub.helm.sh/charts/arhatdev/abbot 0.1.0 latest Network Manager Living at Edge
https://hub.helm.sh/charts/restorecommerce/acce... 0.1.2 0.1.6 A Helm chart for restorecommerce access-control...
https://hub.helm.sh/charts/ckotzbauer/access-ma... 0.4.1 0.4.1 Kubernetes-Operator to simplify RBAC configurat...
https://hub.helm.sh/charts/a10-prometheus-expor... 0.1.0 1.0 A Helm chart of A10 acos prometheus exporter fo...
https://hub.helm.sh/charts/k8s-at-home/adguard-... 2.2.0 v0.102.0 DNS proxy as ad-blocker for local network
https://hub.helm.sh/charts/mogaal/adminer 0.1.0 4.7.3 A Helm chart for Adminer
https://hub.helm.sh/charts/cetic/adminer 0.1.6 4.7.7 Adminer is a full-featured database management ...
https://hub.helm.sh/charts/graviteeio/ae 1.1.11 1.2.13 Official Gravitee.io Helm chart for Alert Engine

Creating your first chart

The easiest way to get started with chart is by using helm create command. The below command create a new chart named myhelmchart

helm create myhelmchart
Creating myhelmchart
  • If you drill down inside the myhelmchart directory you will see the structure like this. Let take a look at each of these files/directory one by one
myhelmchart
├── charts
├── Chart.yaml
├── templates
│ ├── deployment.yaml
│ ├── _helpers.tpl
│ ├── hpa.yaml
│ ├── ingress.yaml
│ ├── NOTES.txt
│ ├── serviceaccount.yaml
│ ├── service.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml

templates

Templates is one of the most important directory and this is where helm looks for your kubernetes object yaml definitions(Deployment,Services,etc).

values.yaml

If templates holds the resource definition, then values provides the way to parameterize it.

_helpers.tpl

Helm allows the use of Go templating in the resources files for kubernetes. The _helpers.tpl is used to define Go template helpers.

NOTES.txt

This is the plain text file that get printed out when the chart is successfully deployed. Usually this contains the next step for using the chart.

Chart.yaml

This file contain the metadata such as chart version, application version or constraints like minimum version of kubernetes/Helm which is required to manage this chart. Some required fields in Chart.yaml

Now you understand all the chart structure and all the corresponding file, it’s time to create your first chart. For the purpose of that I will start with a simple nginx application and later on we will parameterize it using values.yaml.

Step1: Cleanup all the files inside templates directory so that we will start from scratch

rm -rf templates/*

Step2: Create deployment file

kubectl create deployment nginx --image=nginx --dry-run=client -o yaml >> templates/deployment.yaml

Step3: Expose your deployment

kubectl expose deploy nginx --port 80 --type NodePort --dry-run=client -o yaml > /tmp/service.yaml
Error from server (NotFound): deployments.apps "nginx" not found

Now the catch is if you try to expose the deployment without creating it, you will encounter this error, to fix it, create deployment temporarily(as we want deploy it using helm).

kubectl create deployment nginx --image=nginx                                                      
deployment.apps/nginx created
kubectl expose deploy nginx --port 80 --type NodePort --dry-run=client -o yaml > templates/service.yaml
  • As this my first chart, I want to keep it bare bone and define only the required values inside Chart.yaml
cat Chart.yaml               
apiVersion: v2
name: myhelmchart
description: A Helm chart for Kubernetes
version: 0.1.0
  • One more additional file, I need to create is NOTES.txt inside templates directory
echo "This is first helm chart and it will deploy nginx application" >>templates/NOTES.txt
  • I also cleanup rest of the files and directory to draw the clean slate
rm -rf values.yaml charts

Deploying your first chart

With all the config in place it’s time to deploy first helm chart. We can also set the name of the release so that we can refer it back at the later stages.

  • It always a good idea to run linter before deploying your chart to make sure there is no syntax error or your are following all the best practices
helm lint ./myhelmchart 
==> Linting ./myhelmchart
[INFO] Chart.yaml: icon is recommended
[INFO] values.yaml: file does not exist
1 chart(s) linted, 0 chart(s) failed
  • Let’s now start with a dry-run to make sure everything looks good
helm install demochart ./myhelmchart --dry-run
NAME: demochart
LAST DEPLOYED: Thu Feb 4 09:46:19 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: myhelmchart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: nginx
name: nginx
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
type: NodePort
status:
loadBalancer: {}
---
# Source: myhelmchart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: nginx
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
NOTES:
This is first helm chart and it will deploy nginx application
  • If things good deploy your helm chart without dry-run option
helm install demochart ./myhelmchart          
NAME: demochart
LAST DEPLOYED: Thu Feb 4 09:47:56 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
This is first helm chart and it will deploy nginx application
  • To verify it
helm list                           
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
demochart default 1 2021-02-04 09:47:56.949383215 -0800 PST deployed myhelmchart-0.1.0
  • Or you can access your application via hitting any of the kubernetes node
curl http://172.19.0.2:32544/                  
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

--

--

Prashant Lakhera
Prashant Lakhera

Written by Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP