Getty Images

Tip

An IT ops guide to Kubernetes Job vs. CronJob

Jobs and CronJobs both execute tasks in Kubernetes. Their key difference comes down to frequency, and matching a task with the wrong Job type can lead to unsuccessful execution.

There are two main approaches to tackling tasks in Kubernetes: Jobs and CronJobs. Understanding their differences and characteristics is crucial for proper task management.

Kubernetes is a standard to automate and orchestrate container-based application environments. Management of these environments often involves Kubernetes Jobs: the execution of tasks. Many tasks are one-off or ad-hoc. In other cases, teams must schedule tasks by organizing groups or sets of related tasks for recurring execution. As such, Jobs and CronJobs each cater to specific task frequency: Jobs are for one-time, as-needed tasks. CronJobs are recurring.

Although Kubernetes Jobs and CronJobs can both execute important tasks, the decision of whether to schedule the tasks depends on their type.

Jobs in Kubernetes

Kubernetes Jobs are for one-time or on-demand tasks. A Job creates one or more pods of containers to execute a short-term or sequential batch task. The noteworthy consideration with Jobs is its short-term nature. Where most pods run on an extended basis, the pod involved in a Job will only execute until a certain number of completions occur, at which point the Job finishes. Jobs can support parallel tasks that occur on multiple pods, automatically retry on failure and keep track of completions. IT staff can delete or suspend Jobs manually and programmatically.

The specification of a Kubernetes Job can involve several key elements:

  • Configured using a YAML file and executed through a Kubernetes command line.
  • Job templates, which can create pods that perform specified tasks. Templates enable IT staff to create standardized and reusable tasks to handle a wide range of operations.
  • A restart policy that can determine pod behavior after a failure or completion.
  • A specified number of successful completions needed to consider the Job complete, which does not need to be 100%.
  • Parallelism defined by the number of pods -- and the maximum number of pods -- that can run concurrently to operate on the required task.

Teams can check Jobs in Kubernetes using the Kubernetes command kubectl get all to retrieve a list.

CronJobs in Kubernetes

A CronJob is a Job that runs on a recurring or specified schedule. The schedule relies on a declarative Cron format and parameters, where Cron is a command-line job scheduler utility on Unix-type operating systems. CronJob functionality is built into the Kubernetes REST API.

Tasks are specified like any typical Job in Kubernetes, but the recurring intent of CronJobs means the underlying Jobs are often suited for repetitious operations. Examples include backups, data synchronization and report generation.

Kubernetes CronJobs require several considerations:

  • Job templates can create pods that perform specified tasks. As with ordinary individual Jobs, templates support standardized and reusable tasks with CronJobs.
  • The CronJob must specify the schedule, including the time and frequency of CronJob execution.
  • Concurrency, either intended or accidental, is when a CronJob starts while a previous CronJob is still running. The CronJob should specify how it handles these types of overlapping schedules.
  • The CronJob should stipulate the maximum time allowed, or a deadline, for the task to start before the task fails.

There are three noteworthy limitations to CronJobs:

  • IT staff can add time zones to the CronJob schedule, but the schedule may not support some time zone formats. If this is the case, warnings will occur during execution.
  • A CronJob only applies to new Jobs. When an existing CronJob is modified, the changes only apply to new Jobs that start after the changes are made. This means CronJobs will not update Jobs that are already running.
  • It is possible that no Jobs may start during a cycle, or Jobs may overlap, as in accidental or unexpected concurrency. This is due to the way the Kubernetes scheduler works. Scheduled start times are not precise.

Kubernetes Jobs vs. CronJobs examples

Jobs, best-suited to one-time or as-needed tasks, befit image processing, periodic data replication or restoration, data transformation and normalization, as well as other data processing tasks and analytics. For example, a data restoration can be critical for a business, but it's only needed on those rare occasions when a data loss occurs.

CronJobs, best-suited for scheduled or recurring tasks, are a match for data backups, data synchronization or aggregation, log cleanup and other system maintenance activities and time-based resource scaling. For example, IT operations might require log cleanups on a regular basis, so scheduling such tasks through CronJobs means no one has to remember to conduct the cleanup.

Stephen J. Bigelow is a senior technology editor in TechTarget's Data Center and Virtualization Media Group.

Next Steps

Configure Kubernetes garbage collection to get rid of waste

Dig Deeper on Containers and virtualization

Software Quality
App Architecture
Cloud Computing
SearchAWS
TheServerSide.com
Data Center
Close