Helm Chart Hooks


Helm Chart Hooks

In this tutorial, we are going to discuss about Helm Chart Hooks. Already we discussed about chart hooks where we can add a hook to do the testing which will not get deployed as a component as a part of Kubernetes cluster, but will get executed so that I can utilize that particular execution for testing.

Let’s have a detailed discussion about hooks where hooks can get triggered at various stages of the chart lifecycle. Like pre-install, post-install, pre-delete, pre-upgrade, post-uprade, pre-rollback, Post-rollback.

As the name suggest I can get the hook executed before or after a specific stage of the chart. I can make use of that particular execution for testing purpose to make sure we do have all the prerequisites.

For example, before doing the upgrade, I can make some process to get triggered and even for example, post-rollback I can trigger some process to get invoked so that the other processes or other pods can take care of it.

Example

Let’s see through some example, let me go ahead and create a chart with the name hooktest.

ashok@waytoeasylearn:~$ helm create hooktest
Creating hooktest

As a part of the templates within hooktest, I can get into the templates. Here’s where I’m going to create the hooks. I can mention a particular component as a hook by adding an annotation saying that is a hook and at what state that particular hook should get triggered.

Let me go ahead and create pre-install and post-install hook. For that I will be creating separate yaml files.

Preinstall hook

Let me create yaml file and add the content.

ashok@waytoeasylearn:~/hooktest/templates$ cat preinstall-hook.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: preinstall-hook
  annotations:
    "helm.sh/hook": "pre-install"
spec:
  containers:
name: hook1-container
  image: busybox
  imagePullPolicy: IfNotPresent
  command: ['sh', '-c', 'echo The pre-install hook Pod is running  - preinstall-hook && sleep 10']
  restartPolicy: Never
  terminationGracePeriodSeconds: 0 

Here I do have the annotation with the key helm.sh/hook. It is a hook and at what stage it should get triggered. Basically it is going to start a container and that’s going to sleep for 10 seconds and get stopped.

Postinstall hook

The same way let me go ahead and create another hook called postinstall-hook. Let me add the content for this specific hook.

ashok@waytoeasylearn:~/hooktest/templates$ cat postinstall-hook.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: postinstall-hook
  annotations:
    helm.sh/hook: post-install
spec:
  containers:
    - name: hook2-container
      image: busybox
      imagePullPolicy: IfNotPresent
      command: ['sh', '-c', 'echo post-install hook Pod is running - post-install&& sleep 10']
  restartPolicy: Never
  terminationGracePeriodSeconds: 0

Here I have mentioned the hook as a post install. So after the installation of the helm chart this particular hook will get triggered and within the hook it’s going to echo this particular statement and sleep for 10 seconds.

So I do have 2 hooks. Let me go ahead and check the components as a part of Kubernetes cluster.

ashok@waytoeasylearn:~/hooktest/templates$ kubectl get all
NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.152.183.1           443/TCP   15d
Helm installation

So here, I did not have any pods related to this particular chart. Let me install this hook.

ashok@waytoeasylearn:~$ helm install hooktestinstall hooktest
NAME: hooktestinstall
LAST DEPLOYED: Sat Jun  5 15:11:31 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
Get the application URL by running these commands:
export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=hooktest,app.kubernetes.io/instance=hooktestinstall" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT 

So it’s going to trigger the pre-install hook. And since we gave the sleep command it’s going to sleep for certain duration, then the actual chart will get installed. Then post install hook should get triggered.

Pre-install hook is successful then the pod should get installed. Now the Chart installation is successful and it is deployed. (You can check this via kubectl logs followed by hook name command).

ashok@waytoeasylearn:~$ kubectl get pods
NAME                               READY   STATUS      RESTARTS   AGE
preinstall-hook                    0/1     Completed   0          3m57s
hooktestinstall-84dcdcfff6-j286s   1/1     Running     0          3m44s
postinstall-hook                   0/1     Completed   0          3m44s

ashok@waytoeasylearn:~$ kubectl logs preinstall-hook
The pre-install hook Pod is running - preinstall-hook

ashok@waytoeasylearn:~$ kubectl logs postinstall-hook
post-install hook Pod is running - post-install

So if you look at very carefully, hooktestinstall-84dcdcfff6-j286s is the pod that got deployed as a part of this particular chart hooktestinstall. Here 2 pods that got executed and it got over. It came to the status completed.

Now, let me go ahead and get more description about particular pod that is pre-install hook pod using following command.

ashok@waytoeasylearn:~$ kubectl describe pod/preinstall-hook | grep -E 'Anno|Started:|Finished:'
Annotations:  cni.projectcalico.org/podIP: 10.1.224.60/32
      Started:      Sat, 05 Jun 2021 15:11:32 +0530
      Finished:     Sat, 05 Jun 2021 15:11:42 +0530

Now, let me get the details about hooktestinstall-84dcdcfff6-j286s pod and get the information.

ashok@waytoeasylearn:~$ kubectl describe pod/hooktestinstall-84dcdcfff6-j286s | grep -E 'Anno|Started:|Finished:'
Annotations:  cni.projectcalico.org/podIP: 10.1.224.48/32
      Started:      Sat, 05 Jun 2021 15:11:45 +0530

Here this particular pod is still running. So it started in 45th seconds. So basically after the pre-install hook got over it started its deployment and it successfully started in 45th second.

Now, let me get the details about post-install hook.

ashok@waytoeasylearn:~$ kubectl describe pod/postinstall-hook | grep -E 'Anno|Started:|Finished:'
 Annotations:  cni.projectcalico.org/podIP: 10.1.224.50/32
       Started:      Sat, 05 Jun 2021 15:11:45 +0530
       Finished:     Sat, 05 Jun 2021 15:11:55 +0530

Post-install hook started in 45th second. So what’s going to happen? Pre-install hook will get triggered before the deployment of the pod and post-install hook will get triggered after the deployment of the pod.

And I can make use of this particular staged execution to validate as well as to verify the resources or any pre-check I wanted to do before or after the deployment of the charts.

Summary

So in a quick summary we discussed about the introduction about hooks and how to make use of the pre-install and post-install hook.

The same way I can make the hooks to get triggered for the jobs as well. Within Kubernetes, we do have another component called Jobs. When the jobs getting executed before and after execution I can make the hooks to get triggered. Let’s see that in the next tutorial.

We can set the weightage for the hooks also. In case if I have multiple hooks which hook should get triggered based on the weightage it can decide that example also we will be seeing in the future tutorials.

Helm Chart Hooks
Scroll to top