How to automate your tasks with Kubernetes CronJob

Kubernetes actively monitors the implementation of CronJobs and executes user-defined actions if errors occur. Configuring Kubernetes CronJobs is straightforward with YAML files.

What is a CronJob?

A CronJob is a method for automating tasks, a bit like an alarm clock that rings at a certain time. You can also configure a CronJob so that it automatically executes defined tasks at set times. A CronJob can automate various types of tasks, such as updating databases, backing up files, executing scripts or sending emails at regular intervals. By integrating CronJobs into Kubernetes, tasks can be monitored and managed in isolation in containers.

Tip

Managed Kubernetes from IONOS offers you a high-performance infrastructure in which you can configure worker nodes according to your individual requirements. The comprehensive IONOS cloud management solution ensures smooth operation.

How to set up CronJobs in Kubernetes

Creating a CronJob is very similar to creating a regular job in Kubernetes. You have to create a YAML manifest file to do this. This is a structured description that contains all the relevant details for the CronJob. You define important parameters in this file, such as the schedule for executing the job, the containers and the exact commands to be executed in these containers.

Creating a YAML file

Open a text editor of your choice to create the YAML configuration file for the CronJob. Make sure to define the schedule of the CronJob in Cron format. Add the job definition, including the task to be executed. Then, save the file with the extension .yaml.

apiVersion: batch/v1 
kind: CronJob 
metadata: 
    name: new_cronjob 
spec: 
    schedule: "0     ****    " 
    jobTemplate: 
        spec: 
            template: 
                spec: 
                    containers: 
                    - name: container 
                        image: image:latest 
                        command:    
                     - /bin/sh
yaml

Add resource requirements and other settings to the Pod specification if necessary:

spec: 
    containers: 
    - name: container 
        resources: 
            requests: 
                memory: "64Mi" 
                cpu: "250m" 
            limits: 
                memory: "128Mi" 
                cpu: "500m"
yaml

Activating CronJob

Enter the following command in a terminal to create the CronJob in the Kubernetes cluster:

kubectl apply -f [filename].yaml
shell

Monitoring CronJob

If you execute the following command, you will receive a list of the existing Kubernetes CronJobs and Watch mode will ensure that the output is automatically updated when the status of CronJobs changes.

kubectl get cronjob --watch
shell

CronJob schedule syntax

The Schedule syntax for Kubernetes CronJobs is based on the classic cron format with five fields in the following order: minute, hour, day of the month, month and day of the week. Here is a brief overview:

  • Minute (0–59)
  • Hour (0–23)
  • Day of the month (1–31)
  • Month (1–12 or Jan–Dec)
  • Day of the week (0–7 or Sun–Sat)

Special characters:

  • *: Each valid value for the field
  • ,: Specification of multiple values
  • /: Specification of steps

Examples:

0 ****: Every hour 15 2 ***: Daily at 2:15 0 0 1 **: On the first day of each month at midnight 0 0 *3*: Daily at midnight in March 0 2 ** 1: Every Monday at 2 a.m.

CronJob concurrency policy

The concurrency policy can be specified in the configuration of a CronJob and affects how parallel jobs within the same Kubernetes CronJob are handled.

  • Allow (default): If the concurrency policy is set to __Allow__, a new job is started even if the previous job has not yet been completed. This means that several instances of the same job can run simultaneously.
  • Forbid: With this setting, a new job is not started if a previous job has not yet been completed. This ensures that only one instance of the job is executed at any given time.
  • Replace: Unfinished jobs are canceled so that new jobs can continue. No instances of the same job may run at the same time.

Set a deadline for the execution run

The startingDeadlineSeconds field in a Kubernetes CronJob specifies the maximum number of seconds after the scheduled execution time that a job may be started. If the job does not run within this time limit, it is considered failed.

apiVersion: batch/v1 
kind: CronJob 
metadata: 
    name: new_cronjob 
spec: 
    schedule: "0     ****    " 
    startingDeadlineSeconds: 300    
    jobTemplate: 
        spec: 
            template: 
                spec: 
                    containers: 
                    - name: container 
                        image: image:latest 
                        command: 
                     - /bin/sh
yaml

In this example, the job defined by the CronJob must be started within 300 seconds (5 minutes) of the scheduled time, otherwise it will be deemed as failed.

Limit job history

In Kubernetes CronJobs, the settings spec.successfulJobsHistoryLimit and spec.failedJobsHistoryLimit allow you to limit the number of jobs kept in the history of the CronJob. spec.successfulJobsHistoryLimit is an optional field that specifies how many successfully completed jobs should be saved in the history. This is useful to control resource utilization and ensure that the history is not cluttered with an excessive number of successfully completed jobs. Similarly, spec.failedJobsHistoryLimit allows control over the number of failed jobs.

apiVersion: batch/v1beta1 
kind: CronJob 
metadata: 
    name: new_cronjob 
spec: 
    schedule: "0     ****    " 
    successfulJobsHistoryLimit: 3    # Keep only the last 3 successfully completed jobs in history. 
    failedJobsHistoryLimit: 1                    # Keep only the last failed job in history. 
    jobTemplate: 
        spec: 
            template: 
                spec: 
                    containers: 
                    - name: container 
                        image: image:latest 
                        command: 
                     - /bin/sh
yaml

Here, we specify keeping only the last three successfully completed jobs and the last failed job in the course of the Kubernetes CronJob new_cronjob.

Kubernetes Cronjob errors and troubleshooting

Various errors can occur in Kubernetes CronJobs, so you should know some effective troubleshooting techniques. Here are a few common error sources and troubleshooting tips:

Job will not start

If a CronJob does not start in Kubernetes, this can have various causes including an unavailable or incorrect container image and missing authorizations for the pod’s service account. To diagnose the problem, examine the container logs with kubectl logs <pod-name>, the events of the CronJob with kubectl describe cronjob <cronjob-name>, and the events of the pod with kubectl describe pod <pod-name>. Make sure that the service account has the necessary authorizations by checking the role and role binding.

Incorrect CronJob configuration

An incorrect CronJob configuration may be due to syntax errors in the YAML file or invalid settings for Schedule and RestartPolicy. To identify these problems, check the YAML file for correct syntax and settings. You can use kubectl describe cronjob <cronjob-name> to obtain detailed information about the configuration and identify any errors or inconsistencies.

Resource limits exceeded

Exceeding resource limits can cause CronJobs to not run properly. To resolve the problem, check the resource limits in the CronJob and the associated pods with kubectl describe cronjob <cronjob-name> and kubectl describe pod <pod-name>. In addition, monitoring cluster resource utilization with kubectl top nodes and kubectl top pods can provide valuable insights. If necessary, adjust the resource limits in the YAML file.

To get started with cluster management in Kubernetes, we recommend the Kubernetes tutorial from our Digital Guide.

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top