The CronJob specification in Kubernetes allows you to configure various options to control the behavior of your scheduled tasks. Here are the commonly used options with examples:

1. Schedule:

This field specifies the cron schedule for running the Job. For example, "*/1 * * * *" means that the Job will run every minute.

YAML
schedule: "*/1 * * * *"

2. Concurrency Policy:

This field specifies how to handle concurrent execution of Jobs created by the CronJob. The available options are:

  • Allow (default): This option allows concurrent Job executions.
  • Forbid: This option skips the new Job if a previous Job is still running.
  • Replace: This option replaces the currently running Job with a new one.
YAML
concurrencyPolicy: Forbid

3. Suspend:

This field is a boolean that specifies whether the CronJob should be suspended or not. When set to true, the CronJob will not create any new Jobs until it is resumed. Example:

YAML
suspend: true

4. Successful Job History Limit:

This field specifies the number of successful Job objects to retain in the cluster. The default value is 3, and setting it to 0 will retain all successful Jobs.

YAML
successfulJobsHistoryLimit: 5

5. Failed Job History Limit:

This field specifies the number of failed Job objects to retain in the cluster. The default value is 1, and setting it to 0 will retain all failed Jobs.

YAML
failedJobsHistoryLimit: 3

6. Start Deadline Seconds:

This field specifies the deadline (in seconds) for starting the Job if it misses its scheduled time for any reason. After this deadline, the Job will be skipped, and a new Job will be created at the next scheduled time.
Example:

YAML
startingDeadlineSeconds: 300

7. Job Template:

This field specifies the template for creating Jobs. It includes the Pod template spec, which defines the containers and other configurations for the Pods that will be created by the Job.
Example:

YAML
jobTemplate:
  spec:
    template:
      spec:
        containers:
        - name: my-container
          image: my-image
          args:
          - /bin/sh
          - -c
          - echo "Hello, Kubernetes!"
        restartPolicy: OnFailure

8. Job Parallelism:

This field specifies the maximum number of Pods that should run in parallel for a single Job created by the CronJob. If not specified, it defaults to 1.

YAML
jobTemplate:
  spec:
    parallelism: 3

These options allow you to fine-tune the behavior of your CronJobs, such as handling concurrency, managing Job history, specifying deadlines, and configuring the Job and Pod specifications.

CronJob example in Kubernetes:

Here’s an example of a complex CronJob that uses various specification options:

YAML
apiVersion: batch/v1
kind: CronJob
metadata:
  name: complex-cronjob
spec:
  schedule: "0 2 * * 1-5"  # Run at 2 AM every weekday
  concurrencyPolicy: Forbid  # Do not allow concurrent Jobs
  suspend: false  # CronJob is active
  successfulJobsHistoryLimit: 10  # Keep the last 10 successful Job records
  failedJobsHistoryLimit: 5  # Keep the last 5 failed Job records
  startingDeadlineSeconds: 300  # Jobs must start within 5 minutes of scheduled time
  jobTemplate:
    spec:
      parallelism: 2  # Run 2 Pods in parallel for each Job
      backoffLimit: 3  # Try 3 times before marking the Job as failed
      template:
        spec:
          containers:
          - name: data-processor
            image: data-processor:v1.0
            args:
            - /app/process_data.sh
            - --source=/data/input
            - --destination=/data/output
            volumeMounts:
            - name: data
              mountPath: /data
          volumes:
          - name: data
            emptyDir: {}
          restartPolicy: OnFailure

Explanation:

Let’s break down the different options used in this CronJob:

  1. Schedule: The schedule is set to "0 2 * * 1-5", which means the CronJob will run at 2 AM every weekday (Monday to Friday).
  2. Concurrency Policy: The concurrencyPolicy is set to Forbid, which means that if a new Job is scheduled while a previous Job is still running, the new Job will be skipped.
  3. Suspend: The suspend field is set to false, which means the CronJob is active and will create new Jobs based on the schedule.
  4. Successful Job History Limit: The successfulJobsHistoryLimit is set to 10, which means that Kubernetes will keep the records of the last 10 successful Jobs created by this CronJob.
  5. Failed Job History Limit: The failedJobsHistoryLimit is set to 5, which means that Kubernetes will keep the records of the last 5 failed Jobs created by this CronJob.
  6. Start Deadline Seconds: The startingDeadlineSeconds is set to 300 (5 minutes), which means that if a Job created by this CronJob fails to start within 5 minutes of its scheduled time, it will be skipped, and a new Job will be created at the next scheduled time.
  7. Job Template: The jobTemplate section defines the Job specification, including the Pod template and other Job-level configurations.
    • parallelism is set to 2, which means that each Job created by this CronJob will run 2 Pods in parallel.
    • backoffLimit is set to 3, which means that if a Pod fails to start or run successfully, Kubernetes will try to restart it up to 3 times before marking the Job as failed.
    • The Pod template includes a single container named data-processor that runs the data-processor:v1.0 image and executes the /app/process_data.sh script with arguments to specify the input and output data directories.
    • The container mounts an emptyDir volume named data to provide a shared directory for input and output data.
    • The restartPolicy for the Pods is set to OnFailure, which means that Kubernetes will restart the Pod if it fails or exits with a non-zero exit code.
By |Last Updated: May 7th, 2024|Categories: Kubernetes|