๐ How to Create a Kubernetes CronJob (Step-by-Step Guide)

I am Bittu Sharma, a DevOps & AI Engineer with a keen interest in building intelligent, automated systems. My goal is to bridge the gap between software engineering and data science, ensuring scalable deployments and efficient model operations in production.! ๐๐ฒ๐'๐ ๐๐ผ๐ป๐ป๐ฒ๐ฐ๐ I would love the opportunity to connect and contribute. Feel free to DM me on LinkedIn itself or reach out to me at bittush9534@gmail.com. I look forward to connecting and networking with people in this exciting Tech World.
In real-world DevOps environments, teams often need to run tasks on a fixed scheduleโsuch as backups, cleanup jobs, reports, or monitoring scripts.
In Kubernetes, this requirement is fulfilled using CronJobs.
In this blog, we will create a Kubernetes CronJob with a dummy command as part of the Nautilus DevOps team setup.
๐ Prerequisites
Before starting, make sure:
You have access to a Kubernetes cluster
kubectlis configured correctlyYou are logged into the jump_host
(In KodeKloud labs, this is already configured)
๐ Task Requirements
We need to create a CronJob with the following specifications:
| Requirement | Value |
| CronJob Name | xfusion |
| Schedule | */6 * * * * |
| Container Name | cron-xfusion |
| Image | nginx:latest |
| Command | echo Welcome to xfusioncorp! |
| Restart Policy | OnFailure |
๐ง What is a CronJob in Kubernetes?
A CronJob runs jobs on a time-based schedule, similar to Linux cron.
Examples:
Every 5 minutes
Every day at midnight
Every Sunday
๐งพ Step 1: Create the CronJob YAML File
Create a file named xfusion-cronjob.yaml:
vi xfusion-cronjob.yaml
Paste the following content:
apiVersion: batch/v1
kind: CronJob
metadata:
name: xfusion
spec:
schedule: "*/6 * * * *"
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: cron-xfusion
image: nginx:latest
command:
- /bin/sh
- -c
- echo Welcome to xfusioncorp!
๐ก Explanation:
schedule: Runs every 6 minutescommand: Executes a dummy echo commandrestartPolicy: OnFailure: Pod restarts only if the job fails
๐ Step 2: Apply the CronJob
Run the following command:
kubectl apply -f xfusion-cronjob.yaml
Expected output:
cronjob.batch/xfusion created
๐ Step 3: Verify the CronJob
Check if the CronJob is created successfully:
kubectl get cronjob xfusion
Sample output:
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
xfusion */6 * * * * False 0 <none> 5s
โ This confirms that the CronJob is running correctly.
๐ Step 4: (Optional) Check Jobs & Pods
After some time (6 minutes), Kubernetes will create a Job.
kubectl get jobs
kubectl get pods
To check logs:
kubectl logs <pod-name>
Expected log:
Welcome to xfusioncorp!
โ ๏ธ Common Mistakes to Avoid
โ Wrong indentation in YAML
โ restartPolicy outside template.spec
โ Incorrect container name
โ Missing quotes around cron schedule
๐ก KodeKloud Tip:
Name, schedule, image, and restartPolicy are strictly validated.
โ Final Summary
We successfully:
Created a Kubernetes CronJob
Used
nginx:latestimageScheduled it every 6 minutes
Executed a dummy command
Ensured correct restart policy
This setup is commonly used as a base template in production DevOps workflows.
Follow me on LinkedIn
Follow me on GitHub
Keep Learningโฆโฆ




