How to Run, Apply, Get Kubernetes CronJob with Kubectl|yaml
Posted April 24, 2024

In this guide, you will learn how to use commands such as kubectl run cronjob and kubectl get cronjob to run, deploy, apply, and get Kubernetes CronJob.
Kubernetes CronJobs will schedule and automate Kubernetes cluster tasks. Along this example tutorial, you’ll learn:
- How to create Kubernetes cronjob yaml manifest.
- Using Kubectl apply to run and deploy a Kubernetes cronjob.
- How to use kubectl get cronjobs to watch your Kubernetes CronJobs.
- Using Kubectl to describe and Understand your CronJobs.
Ready? Now dive in and learn How to Run, Apply, Get Kubernetes CronJob with Kubectl|yaml
Requirements
Before going through this Steps to Run, Apply, and Get Kubernetes CronJobs with kubectl, ensure:
- You have the basics of Working with Kubernetes
- Have Kubectl ready on you computer.
Creating a CronJob YAML Manifest:
A YAML manifest file will use CronJob as your Kubernetes kind. From here Kubernetes will be able to use the CronJob objects to run your tasks.
Here, you will add the schedule, job template, and other configurations for your CronJob. Go ahead and create a cronjob.yaml
file as follows with the Kubernetes job scheduler:
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob-container
spec:
schedule: "*/30 * * * *" # Change schedule to every 30 minutes
jobTemplate:
spec:
template:
spec:
containers:
- name: test-container
image: busybox:latest
command: ["/bin/sh", "-c"]
args:
- date; echo "Hello from my CronJob"
restartPolicy: OnFailure
The special lines of code you must Understand are:
- Kind is set to CronJob
- The schedule determines the time needed to run the Kubernetes in every given duration.
- You will then run your Kubernetes container inside the jobTemplate
The schedule can be minutes, seconds, hours, Months, days, etc. For example, Run every 5 minutes, every 3 hours, every 5 days, you will use schedule: "*/5 */3 */5 * *"
where:
- */5 as minute field running every 5 minutes.
- */3 is an hour field to run the CronJob every 3 hours.
- */5 acts as a month field representing running every 5 days.
Run every 5 hours with schedule: "0 */5 * * *"
:
- 0 in the minute field ensures the job runs at the start of the hour.
- */5 in the hour field represents running every 5 hours.
-
- in the other fields the job runs every day of the month, every month, and every day of the week.
To run CronJob every 5 minutes, your scheduler format will be schedule: “*/5 * * * *”:
- */5 in the minute field represents running every 5 minutes.
-
- fields means the job runs every hour, every day of the month, every month, and every day of the week.
How to Apply and Run Kubernetes CronJob with Kubectl
It is time to run and apply the CronJobs. Use the following Kubectl apply command within your cronjob.yaml
file path;
kubectl apply -f cronjob.yaml
# cronjob.batch/cronjob-container created
Applying Changes to a CronJob
Once the CronJob is created you can also add more Kubernetes CronJob objects such as:
startingDeadlineSeconds: 300
to wait 5 minutes for the job to startsuccessfulJobsHistoryLimit: 3
for retaining up to 3 successful job historyfailedJobsHistoryLimit: 2
to retain up to 2 failed job history
You will edit your cronjob.yaml
file and add them in the spec section as follows:
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob-container
spec:
schedule: "*/30 * * * *" # Change schedule to every 30 minutes
startingDeadlineSeconds: 300
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 2
### The Rest of your manifest
All you need to rerun the apply command and the changes will get updated:
kubectl apply -f cronjob.yaml
# cronjob.batch/cronjob-container configured
To ensure the CronJob is working as you expected, use kubectl get cronjob command as follows:
kubectl describe cronjobs
You can use specific CronJob and use the watch command to watch the status of CronJobs in Kubernetes with kubectl get cronjobs:
kubectl get cronjobs cronjob-container --watch
This command will start watching the status of CronJobs in the current namespace, and you’ll see updates as they occur.
Exit the watch mode by pressing Ctrl + C.
To even get deeper details kubectl describes command detailed information about CronJob resource.
kubectl describe cronjob output detailed information about the CronJob with schedule, last execution time, concurrency policy, and any associated events or errors. In this case, you will run the command against your created CronJob, for example:
kubectl describe cronjob cronjob-container
Conclusion
You have learned the Steps you need to Run, Apply, and Get Kubernetes CronJobs with kubectl. I hope you can now:
- Create Kubernetes cronjob yaml manifest.
- Use Kubectl apply to run and deploy a Kubernetes cronjob.
- Apply kubectl to get cronjobs to watch your Kubernetes CronJobs.
- Use Kubectl to descriobe and Understand your CronJobs.