Join our Discord Server
Abraham Dahunsi Web Developer 🌐 | Technical Writer ✍️| DevOps EnthusiastπŸ‘¨β€πŸ’» | Python🐍 |

How to create CronJobs In Kubernetes

2 min read

In Linux, cron jobs are used to automate tasks and schedule them to run at specific times or intervals. They are a valuable tool for system administrators and software engineers to efficiently manage repetitive or time-sensitive tasks, reducing the need for manual intervention.

Kubernetes CronJobs provide an alternative to traditional Linux-based cron jobs. This article explains how to create and manage Kubernetes CronJobs in Kubernetes. You will set up multiple CronJobs and perform different tasks to use the available control policies.

Common Use Cases For CronJobs

CronJobs is a beloved tool because it automates recurring tasks such as data synchronization, batch processing, and maintenance jobs. Some use cases of CronJobs in Kubernetes include:

  • Database maintenance
  • Data synchronization
  • Scheduled reports
  • Batch processing
  • Security scanning
  • Content publishing

Prerequisite

Before you start:

Cronjob Syntax

When setting up a Cronjob, the scheduling details are defined using a specific syntax, as outlined below:


# β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ minute (0 - 59)
# β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ hour (0 - 23)
# β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of the month (1 - 31)
# β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ month (1 - 12)
# β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of the week (0 - 6) (Sun to Sat;
# β”‚ β”‚ β”‚ β”‚ β”‚                      7 is also Sunday on some systems)
# β”‚ β”‚ β”‚ β”‚ β”‚                   OR sun, mon, tue, wed, thu, fri, sat
# β”‚ β”‚ β”‚ β”‚ β”‚
# * * * * *

Creating A Kubernetes CronJob

To put Kubernetes CronJobs into action, create a sample CronJob that regularly executes a container-based activity. The following steps will guide you through the process of setting up a CronJob according to a specified schedule:

  1. Create a new Cron job file test-k8s-jobs.yaml.
$ vim test-k8s-jobs.yaml
  1. Add the following content to the file.
apiVersion: batch/v1
kind: CronJob
metadata:
  name: test-k8s-jobs
spec:
  schedule: "*/2 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: test-k8s-jobs
            image: busybox:latest
            command:
            - /bin/sh
            - -c
            - date; echo 'Testing CronJobs on kubernetes'
          restartPolicy: OnFailure

The provided code defines a CronJob named test-k8s-jobs in Kubernetes using the batch/v1 API. It’s scheduled to run every 2 minutes and executes a command to display the date and print a custom message: ‘Testing CronJobs on kubernetes ‘ within a container.

  1. Create the deployment.
$ kubectl apply -f test-k8s-jobs.yaml

Managing CronJobs In Kubernetes

  1. Verify to check all available CronJobs in your cluster
$ kubectl get cronjobs

Output:

NAME                  SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
test-k8s-jobs       */2 * * * *            False           0                31s                          48s
  1. To check all previous jobs created by CronJob
$ kubectl get jobs

Output:

NAME                                    COMPLETIONS   DURATION   AGE
test-k8s-jobs-10048324         1/1                        6s                  49s
  1. To confirm the ongoing processes, you can examine the logs of the job container.
$ kubectl logs job/test-k8s-jobs

Output:

Testing CronJobs on kubernetes
Fri May 10 18:10:07 UTC 2024

Pausing Kubernetes CronJobs

To pause the execution of a Kubernetes CronJob, set the field spec.suspend to true. Once paused, future CronJobs won’t execute until they are resumed, but other existing jobs won’t be affected.

Here are the steps to pause the resource you created earlier:

  1. Use Kubectl to update the target CronJob by changing the spec.suspend field to true
$ kubectl patch cronjob/test-k8s-jobs -p '{"spec": {"suspend": true}}'
  1. Verify if the SUSPEND value of the CronJob is set to True.
$ kubectl get cronjobs

Output:

NAME              SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
example-test-k8s-jobs   */2 * * * *   True      0        74s             5m30s

Creating CronJob Policies

CronJob policies let you change CronJobs behavior in your cluster. This includes controlling jobs, deadlines, and the restart process.

  1. Create a new file newpolicy.yaml.
vim newpolicy.yaml

Add the following configurations to the file:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: test-cronjob
spec:
  schedule: "*/8 * * * *"
  concurrencyPolicy: Forbid
  startingDeadlineSeconds: 300
  jobTemplate:
    spec:
      backoffLimit: 3
      activeDeadlineSeconds: 1800
      template:
        metadata:
          labels:
            app: <your-app>
        spec:
          containers:
          - name: busybox
            image: busybox:latest
            command:
            - /bin/sh
            - -c
            - date
          restartPolicy: OnFailure
  successfulJobsHistoryLimit: 5
  failedJobsHistoryLimit: 3

The following policies were set in the code above:

  • startingDeadlineSeconds: Specifies a maximum time of 300 seconds for a Job to commence
  • activeDeadlineSeconds: Sets a cap on the duration of an individual Job, not exceeding 1800 seconds
  • successfulJobsHistoryLimit: Preserves a maximum of 5 completed job instances
  • failedJobsHistoryLimit: Configures the system to retain at most three failed Job instances

Conclusion

By following this guide, you have successfully set up and handled Kubernetes CronJobs to run tasks regularly within your cluster. You can also personalize your CronJobs’ behavior and features by setting different CronJob policies. You can learn more about Kubernetes CronJobs in their official documentation.

Have Queries? Join https://launchpass.com/collabnix

Abraham Dahunsi Web Developer 🌐 | Technical Writer ✍️| DevOps EnthusiastπŸ‘¨β€πŸ’» | Python🐍 |
Join our Discord Server
Index