Skip to main content

Lab 7.3 - Jobs and CronJobs

Lab Objectives

By the end of this lab, you will be able to:

  • Create and run Jobs for one-time tasks.
  • Understand different types of Jobs (completion, parallel).
  • Create CronJobs for scheduled tasks.
  • Manage Job history.
  • Monitor the execution of Jobs and CronJobs.

Estimated Duration

45-60 minutes

Prerequisites

  • kubectl installed and configured.
  • Functional local Kubernetes cluster (minikube or kind).
  • Knowledge of Jobs and CronJobs (Chapter 7.3).

Part 1: Creating a Simple Job

A Job creates one or more Pods and guarantees that a certain number of them terminate successfully.

Step 1.1: Create a Simple Job

Create a file job-simple.yaml:

# job-simple.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: job-simple
spec:
template:
spec:
containers:
- name: task
image: busybox:1.35
command: ["/bin/sh", "-c"]
args: ["echo 'Task completed successfully!' && date"]
restartPolicy: Never # Jobs use Never or OnFailure

Explanation:

  • restartPolicy: Never: The Pod will not be automatically restarted if it fails. For Jobs, use Never or OnFailure.
  • The Job creates a Pod that executes the command and terminates.

Apply the Job:

kubectl apply -f job-simple.yaml

Step 1.2: Verify the Execution

Check the Job status:

kubectl get jobs

Check the created Pods:

kubectl get pods -l job-name=job-simple

Once the Pod has completed, check the logs:

kubectl logs -l job-name=job-simple

You should see the message "Task completed successfully!" and the date.

Step 1.3: Check the Complete Status

Check the Job details:

kubectl describe job job-simple

You should see that the Job is Complete with 1/1 completed.


Part 2: Job with Multiple Completions

A Job can be configured to run multiple times (in parallel or sequentially).

Step 2.1: Create a Job with Completions

Create a file job-multiple.yaml:

# job-multiple.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: job-multiple
spec:
completions: 5 # The Job must run successfully 5 times
parallelism: 2 # Run 2 Pods in parallel at a time
template:
spec:
containers:
- name: task
image: busybox:1.35
command: ["/bin/sh", "-c"]
args: ["echo 'Task $(date +%s)' && sleep 2"]
restartPolicy: Never

Explanation:

  • completions: 5: The Job must complete 5 successful executions.
  • parallelism: 2: Maximum 2 Pods run in parallel at a time.

Apply the Job:

kubectl apply -f job-multiple.yaml

Step 2.2: Observe the Execution

Monitor the Pods in real time:

kubectl get pods -l job-name=job-multiple -w

You should see 2 Pods running in parallel, then others being created until 5 completions are reached.

Check the Job status:

kubectl get job job-multiple

Once completed, you should see 5/5 completions.


Part 3: Job with Backoff Limit

The backoffLimit defines the number of attempts in case of failure.

Step 3.1: Create a Job that Fails

Create a file job-failure.yaml:

# job-failure.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: job-failure
spec:
backoffLimit: 3 # Maximum 3 attempts in case of failure
template:
spec:
containers:
- name: task
image: busybox:1.35
command: ["/bin/sh", "-c"]
args: ["exit 1"] # Fails intentionally
restartPolicy: Never

Apply the Job:

kubectl apply -f job-failure.yaml

Step 3.2: Observe the Attempts

Monitor the Pods:

kubectl get pods -l job-name=job-failure -w

You should see multiple Pods being created (up to 3 attempts) before the Job is marked as Failed.

Check the Job status:

kubectl get job job-failure
kubectl describe job job-failure

Part 4: Creating a CronJob

A CronJob creates Jobs on a recurring basis according to a schedule (cron format).

Step 4.1: Create a CronJob

Create a file cronjob-example.yaml:

# cronjob-example.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob-example
spec:
schedule: "*/1 * * * *" # Every minute (for testing)
jobTemplate:
spec:
template:
spec:
containers:
- name: task
image: busybox:1.35
command: ["/bin/sh", "-c"]
args: ["echo 'CronJob execution - $(date)'"]
restartPolicy: OnFailure
successfulJobsHistoryLimit: 3 # Keep 3 successful Jobs in history
failedJobsHistoryLimit: 1 # Keep 1 failed Job in history

Explanation:

  • schedule: "*/1 * * * *": Cron format (minute hour day month day-of-week). Here, every minute.
  • successfulJobsHistoryLimit: 3: Keeps the last 3 successful Jobs.
  • failedJobsHistoryLimit: 1: Keeps the last failed Job.

Apply the CronJob:

kubectl apply -f cronjob-example.yaml

Step 4.2: Verify the CronJob

Check the CronJob status:

kubectl get cronjob cronjob-example
# or
kubectl get cj cronjob-example

Wait a minute, then check that Jobs are created:

kubectl get jobs

You should see Jobs named cronjob-example-<timestamp>.

Check the Pods created by the Jobs:

kubectl get pods

Check the logs of one of the Jobs:

JOB_NAME=$(kubectl get jobs -o jsonpath='{.items[0].metadata.name}')
kubectl logs -l job-name=$JOB_NAME

Part 5: Suspending a CronJob

You can temporarily suspend a CronJob without deleting it.

Step 5.1: Suspend the CronJob

Suspend the CronJob:

kubectl patch cronjob cronjob-example -p '{"spec":{"suspend":true}}'

Check that the CronJob is suspended:

kubectl get cronjob cronjob-example

You should see True in the SUSPEND column.

Step 5.2: Resume the CronJob

Resume the CronJob:

kubectl patch cronjob cronjob-example -p '{"spec":{"suspend":false}}'

Part 6: Common Cron Schedule Examples

Here are some common cron schedule examples:

schedule: "0 0 * * *"        # Every day at midnight
schedule: "0 */6 * * *" # Every 6 hours
schedule: "0 9 * * 1" # Every Monday at 9 AM
schedule: "0 0 1 * *" # First day of every month at midnight
schedule: "*/30 * * * *" # Every 30 minutes

Part 7: Cleanup

Delete the created resources:

# Delete CronJobs (also deletes associated Jobs)
kubectl delete cronjob cronjob-example

# Delete Jobs
kubectl delete job job-simple job-multiple job-failure

# Delete remaining Pods (optional, normally deleted automatically)
kubectl delete pods -l job-name

Lab Summary

In this lab, you explored Jobs and CronJobs. You learned how to create Jobs for one-time tasks, configure parallel Jobs, handle failures with backoffLimit, and create CronJobs for recurring scheduled tasks.


Next Steps

The last lab in this module will show you how to use Init Containers and Sidecars for advanced patterns.

Lab 7.4: Init Containers and Sidecars


Lab created: December 2024