Create CronJob in Kubernetes

((KODEKLOUD TASK))

Rajesh
2 min readAug 25, 2023

What are CronJob?

A CronJob creates jobs on a repeating schedule. CronJob is meant for performing regular scheduled actions such as backups, report generation, and so on.

Syntax of CronJob:

In Kubernetes CronJob, we define the schedule using .spec.schedule field in the manifest file.

KODEKLOUD TASK

Manifest File for above task

apiVersion: batch/v1
kind: CronJob
metadata:
name: xfusion
spec:
schedule: "*/7 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: cron-xfusion
image: nginx:latest
command:
- echo "Welcome to xfusioncorp!"
restartPolicy: OnFailure
  • For CronJob , apiversion is batch/v1 and kind is CronJob. CronJob name is set in metadata section.
  • Mentioned earlier, schedule was defined in .spec.schedule field.
  • To define the job for cronjob, we have to use .spec.jobTemplate field.
  • To define what the container to do when cronjob is scheduled. we can use can built-in image for to do that task or we can define commands to run in the container by using command in the container section.

After defining the manifest file. We created a cronjob using kubectl command and list the cronjobs created.

We can also get detailed info about cronjob by using the command

kubectl describe cronjob xfusion

Thank You for seeing this mini article. !!!

--

--