Skip to main content

Chapter 7.4 - CronJobs

Learning Objectives​

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

  • Understand what a CronJob is
  • Create and manage CronJobs
  • Configure schedules with cron syntax
  • Handle multiple jobs and timeouts
  • Understand time zones
  • Implement scheduled tasks

Introduction​

A CronJob creates Jobs on a recurring basis according to a schedule (like cron). It is ideal for scheduled tasks: backups, reports, cleanup, etc.


What is a CronJob?​

A CronJob creates Jobs on a recurring basis according to a schedule defined with cron syntax.

Characteristics​

  • Schedule: Standard cron syntax
  • Job Template: Template for creating Jobs
  • Concurrency Policy: Handle multiple jobs
  • Suspend: Pause the CronJob

Cron Syntax​

Format​

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday)
│ │ │ │ │
* * * * *

Examples​

ScheduleDescription
0 * * * *Every hour (at minute 0)
0 2 * * *Every day at 2:00 AM
*/15 * * * *Every 15 minutes
0 0 * * 0Every Sunday at midnight
0 9-17 * * 1-5Monday to Friday, 9 AM to 5 PM
0 0 1 * *First of every month at midnight

Basic Example​

Simple CronJob​

apiVersion: batch/v1
kind: CronJob
metadata:
name: hello-cronjob
spec:
schedule: "*/1 * * * *" # Every minute
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox:1.35
command:
- /bin/sh
- -c
- date; echo "Hello from CronJob"
restartPolicy: OnFailure
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1

Example: Daily Backup​

Backup CronJob​

apiVersion: batch/v1
kind: CronJob
metadata:
name: daily-backup
spec:
schedule: "0 2 * * *" # Every day at 2:00 AM
timeZone: "America/New_York" # Timezone
jobTemplate:
spec:
template:
spec:
containers:
- name: backup
image: postgres:14
command:
- /bin/bash
- -c
- |
echo "Starting backup at $(date)"
pg_dump -h postgres-service -U postgres mydb > /backup/backup-$(date +%Y%m%d).sql
gzip /backup/backup-$(date +%Y%m%d).sql
aws s3 cp /backup/backup-$(date +%Y%m%d).sql.gz s3://backups/
echo "Backup completed at $(date)"
env:
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: postgres-secret
key: password
volumeMounts:
- name: backup-storage
mountPath: /backup
volumes:
- name: backup-storage
emptyDir: {}
restartPolicy: OnFailure
successfulJobsHistoryLimit: 7 # Keep 7 successful backups
failedJobsHistoryLimit: 3 # Keep 3 failed backups

Concurrency Policy​

Controls how to handle multiple jobs:

Allow (Default)​

Allows multiple jobs in parallel:

apiVersion: batch/v1
kind: CronJob
metadata:
name: allow-concurrent
spec:
schedule: "*/5 * * * *"
concurrencyPolicy: Allow
jobTemplate:
spec:
template:
spec:
containers:
- name: task
image: my-app:1.0
restartPolicy: OnFailure

Forbid​

Prevents new jobs if a previous job is still running:

spec:
concurrencyPolicy: Forbid

Behavior: If a job is still running, the new job is skipped.

Replace​

Replaces the previous job with a new one:

spec:
concurrencyPolicy: Replace

Behavior: The previous job is deleted and a new one is created.


Starting Deadline Seconds​

Timeout for starting a job:

apiVersion: batch/v1
kind: CronJob
metadata:
name: deadline-job
spec:
schedule: "0 * * * *"
startingDeadlineSeconds: 300 # 5 minutes
jobTemplate:
spec:
template:
spec:
containers:
- name: task
image: my-app:1.0
restartPolicy: OnFailure

Behavior: If a job cannot start within 5 minutes, it is considered missed.


Suspend​

Pause a CronJob:

apiVersion: batch/v1
kind: CronJob
metadata:
name: suspended-job
spec:
schedule: "0 * * * *"
suspend: true # CronJob suspended
jobTemplate:
spec:
template:
spec:
containers:
- name: task
image: my-app:1.0
restartPolicy: OnFailure

Via kubectl:

# Suspend
kubectl patch cronjob daily-backup -p '{"spec":{"suspend":true}}'

# Resume
kubectl patch cronjob daily-backup -p '{"spec":{"suspend":false}}'

Job History​

Successful Jobs History Limit​

Number of successful jobs to retain:

spec:
successfulJobsHistoryLimit: 3 # Keep 3 successful jobs

Failed Jobs History Limit​

Number of failed jobs to retain:

spec:
failedJobsHistoryLimit: 1 # Keep 1 failed job

Advantages:

  • View logs of previous jobs
  • Debug failures
  • Audit and history

Example: Periodic Cleanup​

Cleanup CronJob​

apiVersion: batch/v1
kind: CronJob
metadata:
name: cleanup-old-files
spec:
schedule: "0 3 * * *" # Every day at 3:00 AM
jobTemplate:
spec:
template:
spec:
containers:
- name: cleanup
image: busybox:1.35
command:
- /bin/sh
- -c
- |
echo "Cleaning up files older than 30 days"
find /data -type f -mtime +30 -delete
find /data -type d -empty -delete
echo "Cleanup completed"
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: data-pvc
restartPolicy: OnFailure
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1

Useful Commands​

Create and Manage​

# Create a CronJob
kubectl apply -f cronjob.yaml

# View CronJobs
kubectl get cronjob
kubectl get cj

# Details
kubectl describe cronjob daily-backup

# View created Jobs
kubectl get jobs -l app=backup

# View Pods
kubectl get pods -l job-name=daily-backup-1234567890

Suspend/Resume​

# Suspend
kubectl patch cronjob daily-backup -p '{"spec":{"suspend":true}}'

# Resume
kubectl patch cronjob daily-backup -p '{"spec":{"suspend":false}}'

Delete​

# Delete a CronJob
kubectl delete cronjob daily-backup

# Delete with Jobs
kubectl delete cronjob daily-backup --cascade=foreground

Best Practices​

1. Appropriate Schedule​

Choose a schedule that does not overload the cluster.

2. Concurrency Policy​

Use Forbid to avoid conflicts if necessary.

3. Timeouts​

Define startingDeadlineSeconds and activeDeadlineSeconds in the Job.

4. History Limits​

Configure limits to prevent accumulation of Jobs.

5. Monitoring​

Monitor CronJobs to detect failures.

6. Timezone​

Specify the timezone if necessary with timeZone.


Summary​

In this chapter, you learned:

CronJob: Creates Jobs on a recurring basis according to a schedule
Schedule: Standard cron syntax (minute, hour, day, month, weekday)
Concurrency Policy: Allow, Forbid, Replace
Starting Deadline: Timeout for starting a job
Suspend: Pause a CronJob
Job History: Limits for successful/failed jobs
Use cases: Backups, cleanup, reports, periodic tasks
Best practices: Appropriate schedule, timeouts, monitoring


Next Steps​

Module 8: Ingress and Load Balancing
Lab 7.4: Auto-scaling with HPA


Chapter created: December 2024