Kubernetes
Cron Jobs
Pods
Automation
Cloud Computing

Kubernetes Cron Jobs - Run multiple pods for a cron job

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction to Kubernetes Cron Jobs

Kubernetes, an open-source platform for automating deployment, scaling, and operations of application containers, provides various features to manage job executions, such as Cron Jobs. A Kubernetes Cron Job creates Jobs on a repeating schedule, similar to UNIX cron jobs. It is particularly useful for tasks that must run at specific intervals, such as database backups, report generation, or regular system clean-ups.

Kubernetes Cron Jobs run on top of the Kubernetes Jobs API, allowing you to define a job that should run periodically at defined times. In this article, we'll dive deep into Kubernetes Cron Jobs, especially focusing on executing multiple pods for a single cron job instance.

Kubernetes Cron Job Overview

A CronJob in Kubernetes has a spec that details the schedule, job template, and concurrency policy, among other configurations. The critical aspect of a CronJob is its schedule, which determines when the job is executed. The schedule is defined using the conventional cron syntax:

  • `* * * * *` where each asterisk represents, in order, a minute, hour, day of month, month, and day of the week.

For example, a CronJob with a schedule of `0 0 * * *` would execute at midnight every day.

Executing Multiple Pods for a Cron Job

While a single pod might suffice for many job scenarios, there are cases where multiple pods are necessary. You may require multiple pods to:

  • Parallelize Workloads: Distribute workload across multiple pods to increase throughput.
  • Ensure High Availability: Use multiple pods so that if one fails, others continue to work.
  • Resource Optimization: Allocate tasks to different pods based on resource requirements.

Here's how you can configure multiple pods for a cron job:

Job Spec with Parallelism and Completions

In the job template of your CronJob, you can define `parallelism` and `completions`:

  • Parallelism specifies the maximum number of pods that can run simultaneously.
  • Completions indicates the number of times the job should complete in total.

For example, if both values are set to `3`, the job will run a total of 3 pods, with no more than 3 running at a time.

  • name: example
    • /bin/sh
    • -c
    • "date; echo Hello from the Kubernetes cluster"
  • Allow: Allows CronJobs to run concurrently.
  • Forbid: Forbids concurrent runs; skips the next if the previous hasn't finished.
  • Replace: Cancels the running job and starts a new one.

Course illustration
Course illustration

All Rights Reserved.