# Configure your kubernetes with helm chart

## What is helm?

Helm is a package manager for Kubernetes. It makes updates and rollback of applications more efficient and improves team collaboration.

Kubernetes objects are challenging to manage Helm automates maintenance of YAML manifests for Kubernetes objects by packaging information into charts and advertises them to a Kubernetes cluster.

Helm keeps track of the versioned history of every chart installation and change. Rolling back to a previous version or upgrading to a newer version is completed with comprehensible commands.

## What is helm chart?

Helm charts are Helm packages consisting of YAML files and templates which convert into Kubernetes manifest files. Charts are reusable by anyone in any environment, which reduces complexity and duplicates.

## Install helm in ubuntu

### Prerequisites:

Before Installing the helm package manager, please make sure you already have a **Kubernetes** cluster running in your system i.e., minikube is already installed and running on your system.

You can follow this link for setting up kubernetes in your local -[https://techblog.akashojha.com/setup-and-run-your-app-on-kubernetes-locally-in-your-system](https://techblog.akashojha.com/setup-and-run-your-app-on-kubernetes-locally-in-your-system)

Let's install helm in your system.

Run these commands in your terminal

```bash
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
```

Now helm package manager should be installed. We can confirm this by running the following command

```bash
helm version
```

You can see this in the output

```bash
version.BuildInfo{Version:"v3.10.3", GitCommit:"835b7334cfe2e5e27870ab3ed4135f136eecc704", GitTreeState:"clean", GoVersion:"go1.18.9"}
```

## Understand the Helm Chart files

Let's create the first helm chart.

By just running a simple command in your system, you should be able to create your first helm chart.

```bash
helm create first-chart
```

Your file structure will look like this.

```bash
|-- Chart.yaml
|-- charts
|-- templates
|   |-- NOTES.txt
|   |-- _helpers.tpl
|   |-- deployment.yaml
|   |-- hpa.yaml
|   |-- ingress.yaml
|   |-- service.yaml
|   |-- serviceaccount.yaml
|   `-- tests
|       `-- test-connection.yaml
`-- values.yaml
```

The files and folders mentioned above have some specific responsibilities to perform

<table><tbody><tr><td colspan="1" rowspan="1" colwidth="404"><p><strong>File/ Folder</strong></p></td><td colspan="1" rowspan="1" colwidth="403"><p><strong>Responsibilities</strong></p></td></tr><tr><td colspan="1" rowspan="1" colwidth="404"><p>templates/</p></td><td colspan="1" rowspan="1" colwidth="403"><p>The <code>templates/</code> directory is for template files. When Helm evaluates a chart, it will send all of the files in the <code>templates/</code> directory through the template rendering engine. It then collects the results of those templates and sends them on to Kubernetes.</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="404"><p>values.yaml</p></td><td colspan="1" rowspan="1" colwidth="403"><p>The <code>values.yaml</code> file is also important to templates. This file contains the <em>default values</em> for a chart. These values may be overridden by users during <code>helm install</code> or <code>helm upgrade</code>.</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="404"><p>Chart.yaml</p></td><td colspan="1" rowspan="1" colwidth="403"><p>The <code>Chart.yaml</code> file contains a description of the chart. You can access it from within a template.</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="404"><p>charts/</p></td><td colspan="1" rowspan="1" colwidth="403"><p>The <code>charts/</code> directory may contain other charts (which we call subcharts).</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="404"><p>_helpers.tpl</p></td><td colspan="1" rowspan="1" colwidth="403"><p>Files whose name begins with an underscore (<code>_</code>) are assumed to not have a manifest inside. These files are not rendered to Kubernetes object definitions but are available everywhere within other chart templates for use. These files are used to store partials and helpers. When we first created <code>first-chart</code>, we saw a file called <code>_helpers.tpl</code>. That file is the default location for template partials.</p></td></tr></tbody></table>

Most files in `templates/` are treated as if they contain Kubernetes manifests except

Let's go over some of the functionalities of helm charts. First we need to delete all the files under `templates/` directory.

Let's create a file called `configmap.yaml` in templates/ directory and add the following content into it.

```yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-{{ include "app.name" .}}-configmap
data:
  myvalue: "Hello World"
  drink: {{ .Values.favoriteDrink }}
```

**Objects:** Objects are passed into a template from the template engine and your code can pass objects around. The `Release` object contains several objects (like `Release.Name, Release.Namespace`) .In the above example, `{{ .`[`Release.Name`](http://Release.Name) `}}` will insert the name of a release into a template.

**Values**: Values are passed into the template from the `values.yaml` file and from user-supplied files.

Let's clear our `values.yaml` file and add the following into it.

```yaml
favoriteDrink: coffee
```

Let's add the following to the `templates/_helpers.tpl` file.

```yaml
{{- define "app.name" -}}
{{- .Release.Namespace | trimSuffix "-" }}
{{- end }}
```

Now if we run the command

```bash
helm install --debug --dry-run first-app-test ./first-chart
```

We can see the following in the output

```bash
# Source: first-chart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: first-app-test-default-configmap
data:
  myvalue: "Hello World"
  drink: coffee
```

So, the `configmap.yaml` file will get the `.Release.Name` from the command line . The `app.name` is defined in `_heplers.tpl` file. The `include` function will include that inside the `configmap.yaml` file.

We can shift the `myvalue: "Hello World"` from config map to `_helpers.tpl` file.

```bash
{{- define "app.name" -}}
{{- .Release.Namespace | trimSuffix "-" }}
{{- end }}

{{- define "app.values" -}}
myvalue: "Hello World"
{{- end }}
```

And Let's modify our `configmap.yaml` file. `nindent 2` will add 2 spaces before including the content.

```yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-{{- include "app.name" .}}-configmap
data:
  {{- include "app.values" . | nindent 2}}
  drink: {{ .Values.favoriteDrink }}
```

Now Lets's run the same command

```bash
helm install --debug --dry-run first-app-test ./first-chart
```

We can see the output will not change.

```bash
# Source: first-chart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: first-app-test-default-configmap
data:
  myvalue: "Hello World"
  drink: coffee
```

## Install a helm chart

* `helm search hub` searches the [Artifact Hub](https://artifacthub.io/), which lists helm charts from dozens of different repositories.
    
* `helm search repo` searches the repositories that you have added to your local helm client (with `helm repo add`). This search is done over local data, and no public network connection is needed.
    

By running the following command

```bash
helm search hub nginx
```

We can get all helm chart repositories for nginx in Artifact Hub

```bash
URL                                               	CHART VERSION	APP VERSION              	DESCRIPTION                                       
https://artifacthub.io/packages/helm/bitnami/nginx	13.2.21      	1.23.3                   	NGINX Open Source is a web server that can be a...
https://artifacthub.io/packages/helm/mirantis/n...	0.1.0        	1.16.0                   	A NGINX Docker Community based Helm chart for K...
https://artifacthub.io/packages/helm/dysnix/nginx 	7.1.8        	1.19.4                   	Chart for the nginx server                        
https://artifacthub.io/packages/helm/test-nginx...	0.1.0        	1.16.0                   	A Helm chart for Kubernetes
```

Let's add bitnami to our local repositories

```bash
helm repo add bitnami https://charts.bitnami.com/bitnami
```

Let's install the bitnami nginx chart in our local

```bash
helm install nginx-release bitnami/nginx
```

You can see the helm chart is installed and running in your kubernetes cluster by running the command

```bash
helm list
```

If you want to see your app in your browser you can get the minikube-ip.

```bash
minikube ip
```

For me the output is

```bash
192.168.58.2
```

And you can get your port from the following command

```bash
kubectl get svc --namespace default -w nginx-release
```

For me, the output is

```bash
nginx-release   LoadBalancer   10.102.32.132   <pending>     80:31600/TCP   24m
```

I can visit the following address in my browser to get the default page of nginx - http://&lt;minikube-ip&gt;:&lt;service-port&gt; (For me, [http://192.168.58.2:31600/](http://192.168.58.2:31600/))

You can uninstall the helm chart by running the command

```bash
helm uninstall nginx-release
```

## Conclusion

I hope you get a basic understanding of helm. And you'll be able to implement helm for your next Kubernetes deployment. The helm community has a very well written doc. You should visit the official documentation of helm and explore more - [https://helm.sh/docs/](https://helm.sh/docs/)

Happy Learning !!
