Getting Started With Cron Job in Kubernetes

Unais Yousha Siddiqui
Bazaar Engineering
Published in
5 min readMay 13, 2023

What Is Cron Job ?

Cron is a time-based job scheduler in Unix-based operating systems, which enables users to schedule and automate repetitive tasks, such as backups, system maintenance, and data processing. A cron job is a command or script scheduled to run at specified intervals, such as every minute, hour, day, week, or month.

A cron job consists of two parts: a schedule, which defines when the job should run, and a command, which specifies the action to be taken. The cron daemon, which is a background process running on the system, reads the crontab files, which contain the schedule and command for each job, and executes them accordingly.

Scheduling A Job

The Cron Job syntax consists of five fields, each separated by a space:

Job Scheduling

Each field can contain either a specific value or a wildcard character (*).

For example, the following Cron Job configuration will execute the specified command at 1 AM every day:

0 1 * * *

The Cron Job can also be scheduled to run at a specific interval using the / operator. For example, the following Cron Job configuration will execute the specified command every 15 minutes:

*/15 * * * *

For making the right combination of Cron Job configuration we can use this website https://crontab.guru/#*/15_*_*_*_*

How To Implement Cron Job

Cron jobs are commonly used in web applications to perform tasks such as updating database records, sending email notifications, generating reports, and fetching data from external APIs. In this blog, we will discuss how to implement a cron job using Kubernetes, a popular container orchestration platform, and provide a step-by-step guide.

Step 1: Create a Kubernetes Cluster

Before we start implementing the cron job, we need to set up a Kubernetes cluster to run our application. We can use a cloud provider such as Google Cloud, Amazon Web Services, or Microsoft Azure to create a managed Kubernetes cluster, or we can set up a local cluster using tools such as Minikube or Kind.

Step 2: Write the Cron Job Manifest

Once we have a Kubernetes cluster up and running, we can create a manifest file to define the cron job. The manifest file is written in YAML format and contains the metadata and specification for the cron job. In this example, we will use the following manifest file:

Cron Job

Lets first understand the above code:

  • apiVersion specifies the Kubernetes API version to be used for this resource. Here, it is batch/v1 which is the API version for batch jobs.
  • kind specifies the type of the Kubernetes resource. Here, it is CronJob.
  • metadata contains information about the CronJob, such as its name.
  • spec specifies the desired state of the CronJob.
  • schedule is a string in the cron format that specifies when the job should be run. In this example, it is set to run at 6:30 PM (UTC) every day ("30 18 */1 * *").
  • jobTemplate specifies the template for the Job that will be created and run according to the schedule.
  • ttlSecondsAfterFinished is the amount of time in seconds after a Job is finished before it is automatically deleted. Here, it is set to 72000 seconds (20 hours).
  • template specifies the pod template for the Job.
  • containers specifies the container specification for the pod.
  • name is the name of the container.
  • image is the container image to use.
  • command specifies the command to be executed in the container. Here, it is a shell command that makes an HTTP GET request to a URL with a date parameter.
  • restartPolicy specifies the restart policy for the pod. Here, it is set to OnFailure.
  • backoffLimit specifies the number of times to retry the Job in case of failure. Here, it is set to 3.

In summary, this CronJob is configured to run a containerized curl command every day at 6:30 PM (UTC) that makes an HTTP GET request to a specific URL with a date parameter, and retries the job up to three times in case of failure.

Step 3: Deploy the Cron Job

Once we have the manifest file, we can deploy the cron job to the Kubernetes cluster using the “kubectl apply” command:

kubectl apply -f cronjob.yaml

This command creates the cron job and schedules it to run according to the specified schedule.

Step 4: Monitor the Cron Job

We can monitor the status of the cron job using the “kubectl get cronjob” command:

kubectl get cronjob data-pull-cron

This command displays the current status of the cron job, including the last time it ran and the next scheduled run.

We can also view the logs of the container running the cron job using the “kubectl logs” command:

kubectl logs -f data-pull-cron-<job-id>

This command displays the logs in real-time and allows us to monitor the progress of the cron job.

Conclusion

In conclusion, Cron jobs are a powerful tool for automating repetitive tasks and streamlining system maintenance. With the help of Kubernetes, we can easily create and manage Cron jobs to perform various tasks such as database updates, report generation, and data fetching from external APIs. By following the steps outlined in this article, you can quickly implement a Cron job on a Kubernetes cluster and monitor its progress to ensure the job is running as expected. With Cron jobs, you can save time, increase efficiency, and reduce the risk of human error in your system maintenance and data processing workflows.

Thank you very much for taking your valuable time to read my article. Don’t forget to clap if you liked this article 👏🏻

Disclaimer:

Bazaar Technologies believes in sharing knowledge and freedom of expression, and it encourages it’s colleagues and friends to share knowledge, experiences and opinions in written form on it’s medium publication, in a hope that some people across the globe might find the content helpful. However the content shared in this post and other posts on this medium publication mostly describe and highlight the opinions of the authors, which might or might not be the actual and official perspective of Bazaar Technologies.

--

--