Tuesday, November 5, 2024
HomeKubernetesWhat is Helm and How to Create the Package and Push it...

What is Helm and How to Create the Package and Push it to Repo

Helm is a Kubernetes-based package installer. It manages Kubernetes “charts”, which are “preconfigured packages of Kubernetes resources.” Helm enables you to easily install packages, make revisions, and even roll back complex changes.

Helm Installation

Helm now has an installer script that will automatically grab the latest version of the Helm client and install it locally.

You can fetch that script, and then execute it locally. It’s well documented so that you can read through it and understand what it is doing before you run it.

$ curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get > get_helm.sh
$ chmod 700 get_helm.sh
$ ./get_helm.sh
$ helm init

Note that you can also upgrade the Tiller component using:

$ helm init –upgrade 

This post assumes you have install and configured kubernetes already. If not please follow, Below link for setup on ubuntu and centos/redhat

Ubuntu: https://foxutech.com/how-to-setup-kubernetes/

Centos/Redhat: https://foxutech.com/how-to-setup-kubernetes-on-centos-redhat/

Create a package

What is a helm chart? It is basically a set of templates and a file containing variables used to fill these templates. Let’s have a look at an example. I assume that you already have Helm installed and configured at this point.

To start working on a chart, Helm uses a simple command create:

$ helm create firstapp
└── firstapp
    ├── charts
    ├── Chart.yaml
    ├── templates
    │   ├── deployment.yaml
    │   ├── _helpers.tpl
    │   ├── ingress.yaml
    │   ├── NOTES.txt
    │   └── service.yaml
    └── values.yaml

3 directories, 7 files

It has charts directory with chart dependencies. Next comes Chart.yaml containing global variables for the chart such as version and description,

$ cat firstapp/Chart.yaml 
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: firstapp
version: 0.1.0

Then comes templates directory – there you put all the *.yaml files for Kubernetes. Helm uses Go template markup language to customize these files. Helm creates three default file types: deployment, service and ingress. All the files in this directory are ‘skeletons’ which are filled with the variables from values.yaml. when you deploy your Helm chart.

By default helm creates an nginx deployment. Let’s customize it a bit. Add new ConfigMap to the templates directory:

$ cat << HELM > firstapp/templates/cm.yaml
apiVersion: v1
data:
  nginx.conf: |
    events {
      worker_connections  1024;
    }
    http {
      server {
        listen 80;
        location / {
          return 200 "===============================\n\n   My First helm, Happy Helm!!   \n\n===============================\n";
        }
      }
    }
kind: ConfigMap
metadata:
  name: nginx-config
HELM

Point our nginx Deployment to that ConfigMap. Add the following lines to the deployment.yaml:

$ vim firstapp/templates/deployment.yaml (add following line at the end)
volumes:
  - name: config
    configMap:
      name: nginx-config

volumeMounts:
  - name: config
    mountPath: /etc/nginx/nginx.conf
    subPath: nginx.conf

That’s it! Let’s check if we are doing the right thing:

$ helm template firstapp

This will generate all templates with variables and show the output. Now that we know everything is OK, we can deploy the chart:

$ helm install firstapp --name=firstapp-name

Then check that Service and Deploy have been created and curl our Service:

$ kubectl get svc
NAME                          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)             AGE
firstapp-name-firstapp            ClusterIP   10.0.52.41   <none>        80/TCP              44m
$ curl 10.0.52.41
===============================

   My First helm, Happy Helm!!

===============================

Now We have created and deployed our first Helm chart. Additionally, you can create a package:

$ helm package firstapp

This command creates an archive like firstapp-0.1.0.tgz — now you can share your chart with others. For instance, you can upload this file to Helm repository, which we are going to do now.

Create Helm repo and publish your chart

Helm repo is an HTTP server that has file index.yaml and all your chart files. You can use any http-server, but the easiest way to do that is to use GitHub pages.
First, create a GitHub repo, clone it locally and create a branch (note: it should be namedch-pages) for our charts (I will be using the repo called foxutech/helm):

$ git clone https://github.com/foxutech/helm.git
$ cd helm 
$ git checkout -b gh-pages

Now create an empty file and push it to the repo:

$ touch index.yaml
$ git add index.yaml
$ git commit -a -m "add index.yaml"
$ git push --set-upstream origin gh-pages

Then go to github.com to your repo settings and scroll down to “GitHub pages” section. choose gh-pages branch for the source and Copy the link to somewhere. Mine is https://foxutech.github.io/helm/

Now we are going to add our chart to that repo:

$ helm package firstapp
$ helm repo index --url https://foxutech.github.io/helm/ .

The last command generates index.yaml file. Let’s take a look at it:

$ cat index.yaml 
apiVersion: v1
entries:
  firstapp:
  - apiVersion: v1
    appVersion: "1.0"
    created: 2018-08-19T19:40:22.363296112Z
    description: A Helm chart for Kubernetes
    digest: ba308fd186471bed8d0dd8663180f3143d5aefb75b897115c940b15a2e80ecd1
    name: firstapp
    urls:
    - https://foxutech.github.io/helm/firstapp-0.1.0.tgz
    version: 0.1.0
generated: 2018-08-19T19:40:22.36275537Z

Now commit & push the changes:

$ git commit -a -m "change index"
$ git push origin

Now we can add this repo to another Helm installation:

$ helm repo add helm-app https://foxutech.github.io/helm
$ helm repo list
NAME            URL
stable          https://kubernetes-charts.storage.googleapis.com
local           http://127.0.0.1:8879/charts
helm-app        https://foxutech.github.io/helm

Added successfully, Now check it by creating a new deploy from the repo:

$ helm install helm-app/firstapp --name=firstapp-name

And check that everything is running.

Few Other Commands

For check the detailed information about deployed app use status

$ helm status helm-app

To delete,

$ helm delete helm-app

You can list all active revisions using,

$ helm ls

so what if we decide that we’ve changed our mind, and we want to roll back that deletion? Fortunately, Helm is designed for that. We can specify that we want to rollback our application to a specific revision (in this case, 1).

$ helm rollback helm-app 1
RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments