In Linux, cron jobs are used to automate tasks and schedule them to run at specific times or intervals. They are a valuable tool for system administrators and software engineers to efficiently manage repetitive or time-sensitive tasks, reducing the need for manual intervention.
Kubernetes CronJobs provide an alternative to traditional Linux-based cron jobs. This article explains how to create and manage Kubernetes CronJobs in Kubernetes. You will set up multiple CronJobs and perform different tasks to use the available control policies.
Common Use Cases For CronJobs
CronJobs is a beloved tool because it automates recurring tasks such as data synchronization, batch processing, and maintenance jobs. Some use cases of CronJobs in Kubernetes include:
- Database maintenance
- Data synchronization
- Scheduled reports
- Batch processing
- Security scanning
- Content publishing
Prerequisite
Before you start:
Cronjob Syntax
When setting up a Cronjob, the scheduling details are defined using a specific syntax, as outlined below:
# ββββββββββββββ minute (0 - 59)
# β ββββββββββββββ hour (0 - 23)
# β β ββββββββββββββ day of the month (1 - 31)
# β β β ββββββββββββββ month (1 - 12)
# β β β β ββββββββββββββ day of the week (0 - 6) (Sun to Sat;
# β β β β β 7 is also Sunday on some systems)
# β β β β β OR sun, mon, tue, wed, thu, fri, sat
# β β β β β
# * * * * *
Creating A Kubernetes CronJob
To put Kubernetes CronJobs into action, create a sample CronJob that regularly executes a container-based activity. The following steps will guide you through the process of setting up a CronJob according to a specified schedule:
- Create a new Cron job file
test-k8s-jobs.yaml
.
$ vim test-k8s-jobs.yaml
- Add the following content to the file.
apiVersion: batch/v1
kind: CronJob
metadata:
name: test-k8s-jobs
spec:
schedule: "*/2 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: test-k8s-jobs
image: busybox:latest
command:
- /bin/sh
- -c
- date; echo 'Testing CronJobs on kubernetes'
restartPolicy: OnFailure
The provided code defines a CronJob named test-k8s-jobs
in Kubernetes using the batch/v1
API. It’s scheduled to run every 2
minutes and executes a command to display the date and print a custom message: ‘Testing CronJobs on kubernetes ‘ within a container.
- Create the deployment.
$ kubectl apply -f test-k8s-jobs.yaml
Managing CronJobs In Kubernetes
- Verify to check all available CronJobs in your cluster
$ kubectl get cronjobs
Output:
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
test-k8s-jobs */2 * * * * False 0 31s 48s
- To check all previous jobs created by CronJob
$ kubectl get jobs
Output:
NAME COMPLETIONS DURATION AGE
test-k8s-jobs-10048324 1/1 6s 49s
- To confirm the ongoing processes, you can examine the logs of the job container.
$ kubectl logs job/test-k8s-jobs
Output:
Testing CronJobs on kubernetes
Fri May 10 18:10:07 UTC 2024
Pausing Kubernetes CronJobs
To pause the execution of a Kubernetes CronJob, set the field spec.suspend
to true
. Once paused, future CronJobs won’t execute until they are resumed, but other existing jobs won’t be affected.
Here are the steps to pause the resource you created earlier:
- Use Kubectl to update the target CronJob by changing the
spec.suspend
field totrue
$ kubectl patch cronjob/test-k8s-jobs -p '{"spec": {"suspend": true}}'
- Verify if the
SUSPEND
value of the CronJob is set toTrue
.
$ kubectl get cronjobs
Output:
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
example-test-k8s-jobs */2 * * * * True 0 74s 5m30s
Creating CronJob Policies
CronJob policies let you change CronJobs behavior in your cluster. This includes controlling jobs, deadlines, and the restart process.
- Create a new file
newpolicy.yaml
.
vim newpolicy.yaml
Add the following configurations to the file:
apiVersion: batch/v1
kind: CronJob
metadata:
name: test-cronjob
spec:
schedule: "*/8 * * * *"
concurrencyPolicy: Forbid
startingDeadlineSeconds: 300
jobTemplate:
spec:
backoffLimit: 3
activeDeadlineSeconds: 1800
template:
metadata:
labels:
app: <your-app>
spec:
containers:
- name: busybox
image: busybox:latest
command:
- /bin/sh
- -c
- date
restartPolicy: OnFailure
successfulJobsHistoryLimit: 5
failedJobsHistoryLimit: 3
The following policies were set in the code above:
startingDeadlineSeconds
: Specifies a maximum time of 300 seconds for a Job to commenceactiveDeadlineSeconds
: Sets a cap on the duration of an individual Job, not exceeding 1800 secondssuccessfulJobsHistoryLimit
: Preserves a maximum of 5 completed job instancesfailedJobsHistoryLimit
: Configures the system to retain at most three failed Job instances
Conclusion
By following this guide, you have successfully set up and handled Kubernetes CronJobs to run tasks regularly within your cluster. You can also personalize your CronJobs’ behavior and features by setting different CronJob policies. You can learn more about Kubernetes CronJobs in their official documentation.