Six tools to Simplify Kubernetes Journey — Day 4 — Helm Package Manager for Kubernetes
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. The helm’s benefits it provide templating, repeatability, reliability, multiple environments, and ease of collaboration.
Installing Helm
Helm now has an installer script that will automatically grab the latest version of Helm and install it locally.
Reference: https://helm.sh/docs/intro/install/
$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
$ chmod 700 get_helm.sh
$ ./get_helm.sh
- Verify helm version
helm versionversion.BuildInfo{Version:"v3.7.1", GitCommit:"1d11fcb5d3f3bf00dbe6fe31b8412839a96b3dc4", GitTreeState:"clean", GoVersion:"go1.17.2"}
- To check the current state of helm locally
helm envHELM_BIN="helm"HELM_CACHE_HOME="/Users/plakhera/Library/Caches/helm"HELM_CONFIG_HOME="/Users/plakhera/Library/Preferences/helm"HELM_DATA_HOME="/Users/plakhera/Library/helm"HELM_DEBUG="false"HELM_KUBEAPISERVER=""HELM_KUBEASGROUPS=""HELM_KUBEASUSER=""HELM_KUBECAFILE=""HELM_KUBECONTEXT=""HELM_KUBETOKEN=""HELM_MAX_HISTORY="10"HELM_NAMESPACE="default"HELM_PLUGINS="/Users/plakhera/Library/helm/plugins"HELM_REGISTRY_CONFIG="/Users/plakhera/Library/Preferences/helm/registry.json"HELM_REPOSITORY_CACHE="/Users/plakhera/Library/Caches/helm/repository"HELM_REPOSITORY_CONFIG="/Users/plakhera/Library/Preferences/helm/repositories.yaml"
How Helm Works
The way helm works are instead of using individual Kubernetes command for each Kubernetes object, it embeds the Kubernetes object definition in a package called a chart. This chart is then passed to the helm, and the helm connects to the Kubernetes API to create the Kubernetes objects. The way helm connects to the Kubernetes cluster/host via the config file
~/.kube/config
Under the hood, the helm library uses the Kubernetes client to communicate with the Kubernetes API.
Helm chart is the definition of your application, and the release is an instance of that chart. Helm stores these release manifests inside your Kubernetes cluster in the form of secrets.
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
- The 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 lisNAME URL gremlin https://helm.gremlin.comt
- 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 in 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 creates a new chart named myhelmchart.
helm create myhelmchartCreating 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
tree myhelmcharmyhelmchart├── Chart.yaml├── charts├── templates│ ├── NOTES.txt│ ├── _helpers.tpl│ ├── deployment.yaml│ ├── hpa.yaml│ ├── ingress.yaml│ ├── service.yaml│ ├── serviceaccount.yaml│ └── tests│ └── test-connection.yaml└── values.yaml
3 directories, 10 filest
templates
Templates are one of the most important directories, and this is where helm looks for your Kubernetes object yaml definitions(Deployment, Services,etc.).
values.yaml
If templates hold the resource definition, then values provide 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 gets printed out when the chart is successfully deployed. Usually, this contains the next step for using the chart.
Chart.yaml
This file contains the metadata such as chart version, application version, or constraints like a 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 files, it’s time to create your first chart. I will start with a simple Nginx application, and later on, we will parameterize it using values.yaml.
Step1: Cleanup all the files inside the templates directory so that we will start from scratch.
rm -rf myhelmchart/templates/zsh: sure you want to delete all 8 files in /Users/plakhera/Documents/kind/myhelmchart/templates [yn]? y*
Step2: Create deployment file
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml >>myhelmchart/templates/nginx.yaml
Step3: Expose your deployment
kubectl expose deploy nginx --port 80 --type NodePort --dry-run=client -o yaml >> myhelmchart/templates/service.yamError from server (NotFound): deployments.apps "nginx" not foundl
Now the catch is that if you try to expose the deployment without creating it, you will encounter this error, fix it, and create deployment temporarily(as we want to deploy it using helm)
kubectl create deployment nginx --image=ngindeployment.apps/nginx createdx
Try to expose your deployment again
kubectl expose deploy nginx --port 80 --type NodePort --dry-run=client -o yaml >> myhelmchart/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 the 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
- Don’t forget to cleanup the deployment
kubectl delete deployment ngindeployment.apps "nginx" deletedx
Deploying your first chart
With all the config in place, it’s time to deploy the first helm chart. We can also set the release name so that we can refer it back at the later stages.
- It is always a good idea to run linter before deploying your chart to ensure there is no syntax error or follow all the best practices.
helm lint ./myhelmchar
==> Linting ./myhelmchart[INFO] Chart.yaml: icon is recommended[INFO] values.yaml: file does not exist
1 chart(s) linted, 0 chart(s) failedt
- Let’s now start with a dry-run to make sure everything looks good
helm install demochart ./myhelmchart --dry-ruNAME: demochartLAST DEPLOYED: Thu Oct 21 17:49:56 2021NAMESPACE: defaultSTATUS: pending-installREVISION: 1TEST SUITE: NoneHOOKS:MANIFEST:---# Source: myhelmchart/templates/service.yamlapiVersion: v1kind: Servicemetadata: creationTimestamp: null labels: app: nginx name: nginxspec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: nginx type: NodePortstatus: loadBalancer: {}---# Source: myhelmchart/templates/nginx.yamlapiVersion: apps/v1kind: Deploymentmetadata: creationTimestamp: null labels: app: nginx name: nginxspec: 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 applicationn
- If things good deploy your helm chart without dry-run option
helm install demochart ./myhelmcharNAME: demochartLAST DEPLOYED: Thu Oct 21 17:50:13 2021NAMESPACE: defaultSTATUS: deployedREVISION: 1TEST SUITE: NoneNOTES:This is first helm chart and it will deploy nginx applicationt
- To verify it
helm lisNAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSIONdemochart default 1 2021-10-21 17:50:13.253624 -0700 PDT deployed myhelmchart-0.1.0t
- We have created and deployed our first helm chart; it’s time to parameterize these values using values.yaml. Let target the deployment.yaml and parameterize the image
apiVersion: apps/v1kind: Deploymentmetadata: creationTimestamp: null labels: app: nginx name: nginxspec: replicas: 1 selector: matchLabels: app: nginx strategy: {} template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx name: nginx resources: {}status: {}
- But before that how to uninstall your application using helm
helm uninstall democharrelease "demochart" uninstalledt
- Verify it
helm listNAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
- Create values.yaml, and add key as imagename and its value as nginx
cat values.yamimagename: nginxl
- Now to refer this value, you can specify it via Values and key so it should be like (.Values.imagename)
cat myhelmchart/templates/nginx.yamapiVersion: apps/v1kind: Deploymentmetadata: creationTimestamp: null labels: app: nginx name: nginxspec: replicas: 1 selector: matchLabels: app: nginx strategy: {} template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: {{ .Values.imagename }} name: nginx resources: {}status: {}l
- We can also bump up the version of helm chart
cat myhelmchart/Chart.yaml
apiVersion: v2
name: myhelmchart
description: A Helm chart for Kubernetes
version: 0.2.0
- Deploy it again
helm install demochart ./myhelmchartNAME: demochartLAST DEPLOYED: Thu Oct 21 18:02:55 2021NAMESPACE: defaultSTATUS: deployedREVISION: 1TEST SUITE: NoneNOTES:This is first helm chart and it will deploy nginx application
Packaging Chart
You have your chart ready at this stage, but if you want to share these charts with your team or the rest of the community. To do that, use the helm package command.
helm package ./myhelmchart
Successfully packaged chart and saved it to: /home/prashant/Documents/helm/myhelmchart-0.2.0.tgz
- If your teammate need to install this chart, he can do it via helm install
helm install mychart myhelmchart-0.2.0.tgz
Wrapping Up
Helm is a powerful tool, and it greatly simplifies how you package your Kubernetes manifests. I barely scratched the surface in this blog, but if there is any helm feature you liked and want to share with the rest of the community, please mention that in the comment section.