CronJobs¶
- In this lab, we will learn how to create and manage
CronJobsin Kubernetes. - A
CronJobcreatesJobson a time-based schedule. It is useful for running periodic and recurring tasks, such as backups or report generation.
What will we learn?¶
- What CronJobs are and how they work in Kubernetes
- How to create, monitor, and manage CronJobs
- How to view Job and Pod outputs from scheduled tasks
Prerequisites¶
- A running Kubernetes cluster (
kubectl cluster-infoshould work) kubectlconfigured against the cluster
Introduction¶
- A
CronJobin Kubernetes runs Jobs on a time-based schedule, similar to Linux cron. - Useful for periodic tasks like backups, reports, or cleanup.
Step 01 - Create a CronJob YAML¶
- Create a file named
hello-cronjob.yamlwith the following content:
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
namespace: default
spec:
schedule: "*/1 * * * *" # Every 1 minute
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes CronJob!
restartPolicy: OnFailure
Step 02 - Apply the CronJob¶
Step 03 - Verify CronJob Creation¶
Step 04 - Check CronJob and Jobs¶
- List CronJobs:
- List Jobs created by the CronJob:
- List Pods created by Jobs:
Step 05 - View Job Output¶
- Get the name of a pod created by the CronJob, then view its logs:
Example output:
Cleanup¶
Questions¶
- What happens if the job takes longer than the schedule interval?
- How would you change the schedule to run every 5 minutes?
- How can you limit the number of successful or failed jobs to keep?