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 (complete, 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 ensures that a specified number of them successfully complete.
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, useNeverorOnFailure.- The Job creates a Pod that runs the command and completes.
Apply the Job:
kubectl apply -f job-simple.yaml
Step 1.2: Verify Execution
Check the status of the Job:
kubectl get jobs
Check the created Pods:
kubectl get pods -l job-name=job-simple
Once the Pod is 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: Verify Complete Status
Check the details of the Job:
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 runs.parallelism: 2: Maximum 2 Pods run in parallel at a time.
Apply the Job:
kubectl apply -f job-multiple.yaml
Step 2.2: Observe 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 status of the Job:
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 retry attempts in case of failure.
Step 3.1: Create a Failing Job
Create a file job-failure.yaml:
# job-failure.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: job-failure
spec:
backoffLimit: 3 # Maximum 3 retry attempts in case of failure
template:
spec:
containers:
- name: task
image: busybox:1.35
command: ["/bin/sh", "-c"]
args: ["exit 1"] # Intentionally fails
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 status of the Job:
kubectl get job job-failure
kubectl describe job job-failure
Part 4: Creating a CronJob
A CronJob creates Jobs recurrently 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 status of the CronJob:
kubectl get cronjob cronjob-example
# or
kubectl get cj cronjob-example
Wait a minute, then check that Jobs are being 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: Examples of Cron Schedules
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 * *" # The first day of every month at midnight
schedule: "*/30 * * * *" # Every 30 minutes
Part 7: Cleanup
Delete the created resources:
# Delete the CronJobs (also deletes associated Jobs)
kubectl delete cronjob cronjob-example
# Delete the 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, manage failures with backoffLimit, and create CronJobs for recurring scheduled tasks.
Next Steps
The final lab of this module will show you how to use Init Containers and Sidecars for advanced patterns.
Lab 7.4: Init Containers and Sidecars
Lab created on: December 2024