DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Self-Hosted Inference Doesn’t Have to Be a Nightmare: How to Use GPUStack
  • Smart Deployment Strategies for Modern Applications
  • How We Diagnosed a Hidden Scheduler Failure in a Docker Swarm Cluster Serving 2 Million Users
  • Mastering Kubernetes to Maximize Your Cloud Potential

Trending

  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch
  • Spec-Driven Integration: Turning API Sprawl Into a Governed Capability Fleet for AI
  • The Third Culture: Blending Teams With Different Management Models
  • DevOps Is Dead, Long Live Platform Engineering
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Kubernetes CronJob: An Introduction

Kubernetes CronJob: An Introduction

Sometimes, you need to schedule an application process or other repetitive operations. Here's an overview of CronJob, a useful feature of Kubernetes.

By 
Morvana Bonin user avatar
Morvana Bonin
·
Nov. 17, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
23.8K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes, it’s necessary to schedule an application process, some repetitive operations, like sending emails, alerts, verifications, and so on.
On a server, we usually use a cron, which is easy to set up and maintain. If you aren't familiar with it, you can find here all you need to know.

It is also possible to run a crontab using Docker, but how do you do that using Kubernetes?

Indeed, Kubernetes runs differently, because it is possible to have one or more instances of the same services in a load balance case, a crontab should run just once, regardless of several up instances.

On the other hand, we need a crontab to run once in every process for one or more pods.

Kubernetes has a feature called CronJob to fix that problem.

This article explains how CronJob works, describes a few limitations, and gives some tips to avoid common errors.

The examples below were based on kind.

Example of how to create a Cronjob:

YAML
 




x
28


 
1
apiVersion: batch/v1beta1
2
kind: CronJob
3
metadata:
4
  name: my-cron-job
5
spec:
6
  schedule: "*/1 * * * *"
7
  jobTemplate:
8
    spec:
9
      template:
10
        spec:
11
          containers:
12
          - name: my-cron-job
13
            image: curlimages/curl
14
            resources:
15
              limits:
16
                cpu: "1"
17
                memory: "300Mi"
18
              requests:
19
                cpu: "1"
20
                memory: "300Mi"
21
            args:
22
            - /bin/sh
23
            - -c
24
            - date; echo "Starting an example of CronJob"; resp=$(curl -I --http2 https://www.google.com) ; echo $resp; exit 0
25
          restartPolicy: Never
26
  successfulJobsHistoryLimit: 3
27
  failedJobsHistoryLimit: 3



A Cronjob was created and it runs every minute with a curl image.

You have also set a limit of resources (CPU and memory), the best way to visualize that if you use an AWX, Azure or GCP instance as args will be a simple curl on Google site.

This example never restarts and there’s a limit for successful and failed history jobs, in this case, 3.

spec.successfulJobsHistoryLimit - The number of successfully finished cron-jobs to retain
spec.failedJobsHistoryLimit - The number of failed finished cron-jobs to retain

If you want to know more about the CronJob API, I strongly recommend reading this.

Now, run the following command to apply your CronJob in your Kubernetes.

Shell
 




xxxxxxxxxx
1


 
1
$ kubectl apply -f cronjob.yml


If no error occurs, you are able to see your recently configured cron job using the following command.

Shell
 




xxxxxxxxxx
1


 
1
$ kubectl get cronjob
2

          


I use Lens to visualize all available cron jobs, it’s very useful to track and monitor in Kubernetes.

Checking logs:

To delete an entry, just run the following command

Shell
 




xxxxxxxxxx
1


 
1
$ kubectl delete cronjob my-cron-job


This example runs a simple Cron and only an instance of one.

One limitation I found is the possibility to schedule more than one CronJob for the same process by adding one line in each one. However, CronJob isn’t available in the current version (Kubernetes v1.8 [beta]), you have to replicate the same CronJob using parallelism. For another schedule, you need to create another cron entry. I’m looking forward to the chance to schedule more than one pattern for the same process.

Conclusion: Kubernetes CronJon is very useful and easy to learn. You can read and learn more about API parameters in this link and run some tests to better understand how it works.

Kubernetes

Opinions expressed by DZone contributors are their own.

Related

  • Self-Hosted Inference Doesn’t Have to Be a Nightmare: How to Use GPUStack
  • Smart Deployment Strategies for Modern Applications
  • How We Diagnosed a Hidden Scheduler Failure in a Docker Swarm Cluster Serving 2 Million Users
  • Mastering Kubernetes to Maximize Your Cloud Potential

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook