Kubernets Helm Chart
Published: 2024-02-13 04:50:08

### Definition:

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.

Helm uses a packaging format called Charts. A Helm Chart in Kubernetes is a collection of files that describe a set of Kubernetes resources. The Helm Charts can be sent to a Helm Chart Repository. The details specified in the Helm Chart are used to enable a more consistent Kubernetes deployment.

### Components of a Helm Chart!

- Chart: A Helm chart is a collection of files that define a specific application deployment on Kubernetes. It includes templates, which are parameterized YAML files that generate Kubernetes manifests. Charts also contain a Chart.yaml file that describes the chart, and a values.yaml file that holds configurable parameters.
- Release: It is a specific instance of a chart which has been deployed to the Kubernetes cluster using Helm.
- Repository:Helm repositories are collections of charts hosted on a server or a remote location. Charts can be published to a public repository like the official Helm Hub or to private repositories.

- Helm Client: The Helm client is the command-line interface (CLI) tool used to interact with Helm. It allows you to create, package, and manage charts, as well as install and upgrade releases

### Installing Helm

```
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 |bash
```

| Type | #Verify helm version | #To check the current state of helm locally | #helm use the Kubernetes cluster/host via the config file | 
| :---: | :---: | :---: | :---: | 
| Comments | helm version | helm env | ~/.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:

| #To show all the repos | #To add a repo | #Search a repo | #Search for hub | 
| :---: | :---: | :---: | :---: | 
| helm repo list | helm repo add <repo> url | helm search repo mysql | helm search hub |head |

### 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 firsthelmchart

```helm create myhelmchart```

If you see the firsthelmchart directory you will see the structure like below

firsthelmchart
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

Chart.yaml Example
```
apiVersion: v2
name: my-web-app
description: A Helm chart for a simple web application
version: 1.0.0
```

values.yaml

```
replicaCount: 3
image:
  repository: my-web-app
  tag: latest
  pullPolicy: IfNotPresent
service:
  name: my-web-app
  type: ClusterIP
  port: 80
ingress:
  enabled: false
```

Create a new directory called templates, and create a file called deployment.yaml inside it with the following contents:

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Chart.Name }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ .Chart.Name }}
  template:
    metadata:
      labels:
        app: {{ .Chart.Name }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 80
```

Create another file called service.yaml inside the templates directory with the following contents:

```
apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.service.name }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
  selector:
    app: {{ .Chart.Name }}
```

Finally, package your chart by running the following command:

```
helm package .
```

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

create deployment temporarily(as we want deploy it using helm).

```
kubectl create deployment nginx --image=nginx
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 ./firsthelmchart
```

Let’s now start with a dry-run to make sure everything looks good

```
helm install demochart ./firsthelmchart --dry-run
```

If things good deploy your helm chart without dry-run option

```
helm install demochart ./firsthelmchart
```

To verify it

```
helm list
```

Or you can access your application via hitting any of the kubernetes node

curl http://175.12.0.1:32543/