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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • The Evolution of Scalable and Resilient Container Infrastructure
  • Scaling Microservices With Docker and Kubernetes on Production
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  • Building Reliable LLM-Powered Microservices With Kubernetes on AWS

Trending

  • Apple and Anthropic Partner on AI-Powered Vibe-Coding Tool – Public Release TBD
  • Creating a Web Project: Caching for Performance Optimization
  • The End of “Good Enough Agile”
  • SaaS in an Enterprise - An Implementation Roadmap
  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.5K 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

  • The Evolution of Scalable and Resilient Container Infrastructure
  • Scaling Microservices With Docker and Kubernetes on Production
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  • Building Reliable LLM-Powered Microservices With Kubernetes on AWS

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!