Skip to main content

Command Palette

Search for a command to run...

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

Published
โ€ข3 min read
๐Ÿš€ How to Create a Kubernetes CronJob (Step-by-Step Guide)
B

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

  • kubectl is configured correctly

  • You 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:

RequirementValue
CronJob Namexfusion
Schedule*/6 * * * *
Container Namecron-xfusion
Imagenginx:latest
Commandecho Welcome to xfusioncorp!
Restart PolicyOnFailure

๐Ÿง  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 minutes

  • command: Executes a dummy echo command

  • restartPolicy: 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:latest image

  • Scheduled 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โ€ฆโ€ฆ

More from this blog