Kubernetes Job as Hook


Kubernetes Job as Hook

In this tutorial, we are going to discuss about Kubernetes Job as Hook. In the earlier tutorial we discussed about how to use the pre-install and post-install Hook. The same way I can have the hooks for the jobs of Kubernetes as well.

Example

Let’s see through an example. Let me get into the same chart that we had created in previous tutorial. Within that, I’m going to get into the templates folder and remove the yaml file that we had created.

ashok@waytoeasylearn:~/hooktest/templates$ rm preinstall-hook.yaml postinstall-hook.yaml 

Now I’m going to create pre-install post-install hook for the jobs. Let me go ahead and create a yaml file and add content to this.

ashok@waytoeasylearn:~/hooktest/templates$ cat preinstalljob-hook-job.yaml 
apiVersion: batch/v1
kind: Job
metadata:
  name: preinstalljob-hook-job
  annotations:
    "helm.sh/hook": "pre-install"
spec:
  template:
    spec:      
      containers:
      - name: pre-install
        image: busybox
        imagePullPolicy: IfNotPresent
        command: ['sh', '-c', 'echo pre-install Job Pod is Running ; sleep 5'] 
      restartPolicy: OnFailure
      terminationGracePeriodSeconds: 0
  backoffLimit: 3
  completions: 1
  parallelism: 1

So here again, I have added the annotation. It’s a hook and added the stage when it should get triggered as pre-install. And I do have the kind as job. So this is going to get executed as a job where it’s going to echo this particular statement and sleep for 5 seconds.

The same way Let me go ahead and create another yaml file postinstalljob-hook and add content to this.

ashok@waytoeasylearn:~/hooktest/templates$ cat postinstalljob-hook-job.yaml 
apiVersion: batch/v1
kind: Job
metadata:
  name: postinstalljob-hook-job
  annotations:
    "helm.sh/hook": "post-install"
spec:
  template:
    spec:      
      containers:
      - name: post-install
        image: busybox
        imagePullPolicy: IfNotPresent
        command: ['sh', '-c', 'echo post-install Pod is Running ; sleep 10']
      restartPolicy: OnFailure
      terminationGracePeriodSeconds: 0
  backoffLimit: 3
  completions: 1
  parallelism: 1

I have mentioned the annotation as it’s a hook and the stage when it should get triggered as post-install.

Helm Installation

So I do have both the yaml file as a part of the template. Let me go ahead and install the chart using the helm install command.

ashok@waytoeasylearn:~$ helm install hooktestjobinstall ./hooktest/
NAME: hooktestjobinstall
LAST DEPLOYED: Sun Jun  6 19:04:22 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. 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=hooktestjobinstall" -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 

Now let me go ahead and check the entities within the Kubernetes.

ashok@waytoeasylearn:~$ kubectl get all
NAME                                      READY   STATUS      RESTARTS   AGE
pod/preinstalljob-hook-job-zrb2j          0/1     Completed   0          3m39s
pod/hooktestjobinstall-5ccb4d9d5c-v72j7   1/1     Running     0          3m34s
pod/postinstalljob-hook-job-lghmc         0/1     Completed   0          3m34s

NAME                         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/kubernetes           ClusterIP   10.152.183.1            443/TCP   16d
service/hooktestjobinstall   ClusterIP   10.152.183.46           80/TCP    3m34s

NAME                                 READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hooktestjobinstall   1/1     1            1           3m34s

NAME                                            DESIRED   CURRENT   READY   AGE
replicaset.apps/hooktestjobinstall-5ccb4d9d5c   1         1         1       3m34s

NAME                                COMPLETIONS   DURATION   AGE
job.batch/preinstalljob-hook-job    1/1           5s         3m39s
job.batch/postinstalljob-hook-job   1/1           8s         3m34s

So if I look at there are 2 pods that got over that is pre-install pod and post-install hook pod which got triggered by 2 jobs and the other pod that is continuing to run, that is the actual pod that we do have within the templates.

So this particular hook is going to get executed as a job and the earlier one that was getting executed as a pod directly. In this case I will be in a position to control how many times it should get triggered as well as I will be in a position to harvest, all the features available within the jobs as well.

So this works very similar to how we had the hook as a part of the pods. Now, let me go ahead and check the details on how this got executed. So Let me get the Pre-install pod name.

ashok@waytoeasylearn:~$ kubectl describe pod/preinstalljob-hook-job-zrb2j | grep -E 'Anno|Started:|Finished:'
Annotations:  cni.projectcalico.org/podIP: 10.1.224.46/32
      Started:      Sun, 06 Jun 2021 19:04:23 +0530
      Finished:     Sun, 06 Jun 2021 19:04:28 +0530

So this got started in 23rd second and got over in 28th second. Now I’m going to get the actual pod.

ashok@waytoeasylearn:~$ kubectl describe pod/hooktestjobinstall-5ccb4d9d5c-v72j7 | grep -E 'Anno|Started:|Finished:'
Annotations:  cni.projectcalico.org/podIP: 10.1.224.54/32
      Started:      Sun, 06 Jun 2021 19:04:31 +0530

So that started in 31st second. Now let me get the post install hook pod.

ashok@waytoeasylearn:~$ kubectl describe pod/postinstalljob-hook-job-lghmc | grep -E 'Anno|Started:|Finished:'
Annotations:  cni.projectcalico.org/podIP: 10.1.224.57/32
      Started:      Sun, 06 Jun 2021 19:04:32 +0530
      Finished:     Sun, 06 Jun 2021 19:04:42 +0530

That started its execution at 32nd second and got over in 42th second. So this is a quick example on how a job can be invoked as a hook using the pre-install and post-install hook.

So in a quick summary we discussed about how to use a job as a hook to get triggered as a part of pre-install and post install stages.

Kubernetes Job as Hook
Scroll to top