Deploying Kubernetes CronJobs with Helm and Google Cloud Build
Today we are going to learn how to deploy a kubernetes cron job with helm and Google Cloud Build. The idea could be sound complicated but really is simple.
Kubernetes cronjobs is a way to execute jobs in a specific time in a recurrent way. Imagine that for example you want to delete a bucket, get statistics, execute a script, or many other things. The idea is that Kubernetes will execute a job and with this job.
Helm as many of you know is a way to manage your Kubernetes applications defining, installing, and upgrading even the most complex Kubernetes application. Yes, I am reading the official web page. We are going to use helm to deploy our applications and in this case to deploy our cronjob
And the last one is Google Cloud Build. We have already talked about this tool in other articles and is the CI/CD solution that Google offers to be used in our pipelines. It is based on container images executed in a Virtual machine inside a work-pool.
Now to execute our exercise we need first an application. Because as you know at this point we are clever, we are not going to expend time in this point and we are going to copy a cronjob spec from kubernetes official page. Clever people.
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: mkdevcron
spec:
schedule: "*/10 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: mkdevcron
image: busybox
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
We have our code if we execute in our Kubernetes cluster (GKE cluster)
kubectl apply -f cronjob.yaml
job is done, if we do
kubectl get cronjob
our lovely cron is there. Now let's add some variables in our yaml file to be used in our helm chart
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
schedule: {{ .Values.schedule }}
jobTemplate:
spec:
template:
spec:
containers:
- name: {{ .Chart.Name }}
image: {{ .Values.image.name }}
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
And now a simple values.yaml
schedule: "*/10 * * * *"
image:
name: busybox
If install the helm chart with
helm install basiccronjob cronjob/ --values cronjob/values.yaml
we will have our cronjob installed. Again if we do
kubectl get cronjob
our cron is here. Now the last step, cloud build. To use cloud build we need a cloudbuild.yaml file.
steps:
- id: deploy.cronjob
name: 'gcr.io/mkdevyoutube/helm'
env:
- 'CLOUDSDK_COMPUTE_REGION=europe-west1'
- 'CLOUDSDK_CONTAINER_CLUSTER=mkdev'
- 'GCLOUD_PROJECT=mkdev'
args:
- 'install'
- 'basiccronjob'
- 'cronjob/'
- '--values'
- 'cronjob/values.yaml'
As you can see in this cloudbuild file there is an image called helm, but there is a problem, there is no official helm builder in google, crazy but it google, so we will need to create this builder. To do that we do
git clone https://github.com/GoogleCloudPlatform/cloud-builders-community.git
cd cloud-builders-community/helm
GOOGLE_CLOUD_PROJECT=mkdev
GCR_HELM=gcr.io/$GOOGLE_CLOUD_PROJECT/helm
docker build -t $GCR_HELM .
docker push $GCR_HELM
and now the builder is there. Now next step is to create a trigger to be executed every time that we push our code. To do that we need to go to google cloud build and create a trigger.
Name, choose push to a branch as the event, we connect to our GitHub repository, choose our branch, and create. Now as soon as we push our code in our branch trigger will run and the cloud build will be executed. What we are going to have here is a step that we will install our cronjob in our cluster, but because we are clever we understand that this is only a simple example and we understand that we can do many many things here.
Here's the same article in video form for your convenience: