- In Kubernetes, CronJobs are used to run tasks on a schedule — just like Linux cron jobs.
- They are great for automating repetitive tasks like backups, reports, or sending notifications.
🔹 Step 1: What is a CronJob?
- A CronJob creates a Job on a schedule.
- Jobs then create Pods that run to completion.
- Schedule is defined using cron syntax (e.g., */1 * * * * = every 1 minute).
- Useful for:
- Database backups
- Log rotation
- Report generation
- Cleanup tasks
🔹 Step 2: Example CronJob Manifest (cron-job.yaml)
apiVersion: batch/v1
kind: CronJob
metadata:
name: cron-job-demo
spec:
schedule: "*/1 * * * *" # Runs every 1 minute
concurrencyPolicy: Allow # Multiple jobs can run at the same time
startingDeadlineSeconds: 100
suspend: false # If true, suspends job execution
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo "Hello, World!"
restartPolicy: OnFailure
👉 Here, every minute, a new Pod runs and prints:
Hello, World!
🔹 Step 3: Deploy and Verify
# Deploy CronJob
kubectl apply -f cron-job.yaml
# List CronJobs
kubectl get cronjob
kubectl get cj # shorthand
# Describe CronJob
kubectl describe cronjob cron-job-demo
📌 Observation:
You’ll see events showing the CronJob is scheduled.
Every minute, a new Pod is created and runs to completion.
# List Pods
kubectl get pods
kubectl get pods --watch
Sample Output (Pods created each minute):
NAME READY STATUS AGE
cron-job-demo-28017695-vkh 0/1 Completed 40s
cron-job-demo-28017700-tnc 0/1 Completed 20s
🔹 Step 4: Clean-Up
When you’re done:
kubectl delete cronjob cron-job-demo
🎯 Final Thoughts
CronJobs are perfect for automated, scheduled tasks in Kubernetes.
Always tune:
- concurrencyPolicy → Avoid overlapping jobs if not needed.
- successfulJobsHistoryLimit & failedJobsHistoryLimit → Keep logs clean.
- suspend → Useful for pausing without deleting.
🌟 Thanks for reading! If this post added value, a like ❤️, follow, or share would encourage me to keep creating more content.
— Latchu | Senior DevOps & Cloud Engineer
☁️ AWS | GCP | ☸️ Kubernetes | 🔐 Security | ⚡ Automation
📌 Sharing hands-on guides, best practices & real-world cloud solutions
Top comments (0)