Logo

dev-resources.site

for different kinds of informations.

Deploying Kubernetes CronJobs with Helm and Google Cloud Build

Published at
12/21/2024
Categories
kubernetes
helm
gcp
Author
mkdev_me
Categories
3 categories in total
kubernetes
open
helm
open
gcp
open
Author
8 person written this
mkdev_me
open
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
Enter fullscreen mode Exit fullscreen mode

We have our code if we execute in our Kubernetes cluster (GKE cluster)

kubectl apply -f cronjob.yaml
Enter fullscreen mode Exit fullscreen mode

job is done, if we do

kubectl get cronjob
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And now a simple values.yaml

schedule: "*/10 * * * *"
image:
  name: busybox
Enter fullscreen mode Exit fullscreen mode

If install the helm chart with

helm install basiccronjob cronjob/ --values cronjob/values.yaml
Enter fullscreen mode Exit fullscreen mode

we will have our cronjob installed. Again if we do

kubectl get cronjob
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

.

gcp Article's
30 articles in total
Favicon
Deploy your Flask API on GCP Cloud Run 🚀
Favicon
Sécuriser l'association entre un projet et son compte de facturation sur Google Cloud Platform
Favicon
A Complete Guide to Database Services in Google Cloud Platform: Features, Capacity, and Popularity
Favicon
Talos on GCP with Spot Instances
Favicon
Deploying Kubernetes CronJobs with Helm and Google Cloud Build
Favicon
Gcp api gateway
Favicon
Síndrome del bolsillo profundo: síntomas y remedios
Favicon
Migrate from Native Google to AWS Redshift: Benefits and Best Practices
Favicon
How to Deploy Google Cloud Functions with GitHub Actions
Favicon
GCP Training in Bangalore | Master Google Cloud Platform with Eduleem
Favicon
Reducing Orchestration Costs Through Cloud Task And Cloud Scheduler
Favicon
Top 45+ GCP Interview Questions in 2025 - Google Cloud Platform Interview Tips
Favicon
Is Google Cloud Anthos Service Mesh a Mess?
Favicon
Cloudnosys Now Available on Google Cloud Marketplace
Favicon
Running Kubernetes on Bare Metal vs. Cloud Providers (AWS, GCP, Azure): A Comprehensive Comparison
Favicon
Top 10+ Google Cloud Platform (GCP) Skills of 2025
Favicon
Your Complete Roadmap to Mastering Google Cloud Platform
Favicon
Mastering Docker in Cloud Environments: AWS, GCP, and Azure Integration
Favicon
Garantir la résilience de votre IaC face à une panne régionale
Favicon
How I cleared GCP - Associate Cloud Engineer Certification
Favicon
Using Google Cloud Functions for Three-Tier Data Processing with Google Composer and Automated Deployments via GitHub Actions
Favicon
Cloud Computing
Favicon
How To Authenticate GCP Cloud Infra using Service Account with IAC Terraform
Favicon
Pushing Python Packages to Artifact Registry Using Cloud Build
Favicon
How to Create a Cloud Build to Allow Docker to Download Python Packages from Artifact Registry
Favicon
Can I use Cloud Armor with Cloud Run?
Favicon
Modernize or Build New Cloud Apps Without the Headaches
Favicon
GCP publish python package in production
Favicon
Implantando Aplicações Serverless no Google Cloud Run
Favicon
GCP Code Build Error(unknown field "logging" in google.devtools.cloudbuild.v1.Build)

Featured ones: