Kubernetes CronJobs: The Basics and a Quick Tutorial

What Are Kubernetes Jobs and CronJobs?

Kubernetes jobs are tasks configured to run pods that terminate successfully once their specific workloads complete. They are suited for batch-oriented processing, where each job execution is intended for a finite task and ceases upon task completion. Kubernetes CronJobs are jobs that run on a predetermined schedule.

This design ensures that tasks meant to run only until successful completion are managed efficiently without requiring manual intervention after execution. While regular pods in Kubernetes are designed to run continuously, Kubernetes jobs enable scenarios where non-ongoing, bounded tasks need executing. 

This setup is particularly advantageous for data processing tasks, cleanup operations, or batch processing where an explicit start and end define the workload’s lifecycle. The system automatically handles job completion and failure, ensuring that resources are liberated for other processes.

This is part of a series of articles about Kubernetes architecture.

In this article:

Kubernetes Jobs vs. CronJobs: What Are the Differences?

Kubernetes Jobs are designed to execute a specified workload once and then terminate, whereas CronJobs are meant for tasks that need to recur at scheduled intervals. CronJobs utilize the same principles as jobs but are built to run automatically at predefined times, dates, or intervals. This model is suitable for regular maintenance tasks, such as nightly backups or hourly data syncs.

The main differences are in their use cases and configuration settings. Jobs are a one-time operation setup, reflecting a model of ‘run-to-completion,’ whereas CronJobs are scheduled to repeat according to a cron schedule. This makes CronJobs more suited for ongoing background tasks that require regular execution, leveraging Kubernetes’s scheduler to handle repetition and execution logistics.

The Role of the Job Scheduler in Kubernetes 

The job scheduler in Kubernetes manages the execution of jobs within the cluster. It oversees the deployment of pods associated with a job, ensuring they run on suitable nodes with adequate resources. This includes balancing loads and managing dependencies, enhancing overall system efficiency and performance. 

By scheduling jobs smartly, the scheduler maintains cluster health and optimizes resource utilization. It also monitors job progress and status, retrying failed jobs according to policy settings. 

For example, if a job’s pod fails due to node failure or insufficient resources, the scheduler can automatically restart or reschedule the job on a different node. This fault-tolerant mechanism helps in maintaining the reliability and continuous operation, necessary for batch processing and similar asynchronous tasks.

What Are the Benefits of Kubernetes CronJobs? 

CronJobs in Kubernetes offers several advantages.

Scheduled Task Automation

CronJobs automate repetitive tasks, removing the need for manual initiation. This scheduling capability ensures that tasks such as backups, email notifications, or database cleanups are performed at consistent times, reducing the likelihood of human error and the labor involved in recurrent initiation. 

The system’s ability to integrate with diverse workloads and configurations allows for extensive flexibility in how tasks are defined and managed. Users can specify exact timing, resource allocation, and dependency chains, ensuring that automated tasks are executed in a precise, controlled manner that aligns with broader system operations and needs.

Resource Efficiency

By strategically scheduling jobs during off-peak hours, CronJobs optimize resource usage, allowing more resource-intensive tasks to run when system demand is lower. This distribution of the workload helps in maintaining system performance and minimizing operational costs.

Additionally, the ability to scale resources based on the specific needs of a job at any scheduled time helps in avoiding over-provisioning and underutilization. Kubernetes dynamically allocates and deallocates resources, which is particularly important in environments with variable workloads and performance demands, ensuring that resources are utilized efficiently.

Reliability and Fault Tolerance

Kubernetes provides built-in fault tolerance for CronJobs, ensuring high reliability by retrying failed tasks automatically. This resilience is crucial for critical tasks that must be reliably completed, increasing the usefulness of automated operations. Logging and monitoring integrations help in promptly identifying issues, allowing for quick mitigation and continuously reliable operations.

The platform’s ability to handle node failures, software crashes, and other common issues without human intervention provides an advantage in hosting and managing periodic tasks. CronJobs can be configured to manage failures gracefully, for example, by specifying backoff limits and deadlines. This further increases the reliability of scheduled executions in a Kubernetes environment.

Tutorial: Running Automated Tasks with a CronJob 

Ensure you have a Kubernetes cluster operational, and that the kubectl command-line tool is configured to communicate with your cluster. If you don’t already have a cluster, consider setting one up using Minikube, or use online platforms like Killercoda or Play with Kubernetes for a hands-on approach without local setup.

Create a CronJob

CronJobs in Kubernetes require a specific configuration file, known as a manifest, to define the job. Here’s an example manifest for a CronJob named welcome that executes a task every minute:

apiVersion: batch/v1
kind: CronJob
metadata:
name: welcome
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: welcome
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Welcome to the Kubernetes cluster
restartPolicy: OnFailure

This YAML file outlines a job that runs every minute, using the busybox image to execute a simple script that prints the date and a greeting message. Please store the above content in a file called cronjob.yaml on your local drive.

Run the CronJob

Deploy the CronJob using the following kubectl command:

kubectl create -f cronjob.yaml

Upon successful creation, you’ll see a confirmation:

cronjob.batch/welcome created

Monitor the CronJob

To check the status of the CronJob, use:

kubectl get cronjob welcome

This command returns details about the CronJob’s schedule and execution status, like the following:

Watch for the job creation by using:

kubectl get jobs --watch

You should see an output like:

This indicates the job has completed. To stop watching, press Ctrl+C.

View the Job Output

After the job has executed, find the pods created by the job and view the logs to see the output:

# Replace "welcome-4111706356" with the job name in the system
pods=$(kubectl get pods --selector=job-name=welcome-4111706356
--output=jsonpath={.items[*].metadata.name})
kubectl logs $pods

The expected output should display the date and a greeting:

Automating Kubernetes Infrastructure with Spot by NetApp

Spot Ocean from Spot by NetApp frees DevOps teams from the tedious management of their cluster’s worker nodes while helping reduce cost by up to 90%. Spot Ocean’s automated optimization delivers the following benefits:

  • Container-driven autoscaling for the fastest matching of pods with appropriate nodes
  • Easy management of workloads with different resource requirements in a single cluster
  • Intelligent bin-packing for highly utilized nodes and greater cost-efficiency
  • Cost allocation by namespaces, resources, annotation and labels
  • Reliable usage of the optimal blend of spot, reserved and on-demand compute pricing models
  • Automated infrastructure headroom ensuring high availability
  • Right-sizing based on actual pod resource consumption  

Related content: Read our guide to kubernetes labels.

Learn more about Spot Ocean today!