RX-M instructor Ron Petty explores the fundamentals of Kubernetes CronJobs, their uses, and how to create them. Kubernetes CronJobs are a type of controller that allows users to define repeatable tasks to be executed within Kubernetes pods. These tasks are designed to be short-lived, terminating once a specific goal is achieved during a scheduled run.
Video Transcript
Hello everybody and welcome to Cloud Native Short Takes from RX-M. My name is Ron Petty and in this Short Take we will look at Kubernetes CronJobs. So let’s run one and we’ll explain what’s going on. So here’s our CronJob and at a high level it defines a Job to run on a schedule. If you’re unfamiliar with Jobs take a look at our Jobs-related Cloud Native Short Take, but for now it launches pods. So in our case our pod is just a simple script printing a random number and then going to sleep. So let’s go ahead and run this. Now let’s list our CronJobs with kubectl get cronjobs.
The pertinent information here really is the schedule. So what is the schedule? The schedule is been defined as part of something called cron which is a system to launch processes that was developed for Unix decades ago. So you can actually see here on our ubuntu system the actual definition of that syntax with man 5 crontab and if we take a look down here on the man page we can see what it’s talking about. So those positional elements, of which there’s five, define which minute, which hour, day of the month, the month, or day of the week. Now we’re not going to go through all the syntax here but just take a look at this man page and you’ll see the different combinations. For example, you can pick particular periods; you can have repeating periods with a shorter syntax. This allows us to repeat what is going on.
So in our case we’re repeating this job submission every minute, so let’s take a look. For our jobs we can see here that it’s been running for a couple minutes. Now since we’ve started this and each one of these launches a pod, now to confirm let’s take a look at the output of one of our pods. And there’s our random number and just to prove that it’s random let’s take a look at one more.
So why would we use a CronJob? A CronJob helps us automate the controlling of Jobs and so it’s really important to understand why we have Jobs in Kubernetes to begin with. So generally speaking we have daemons versus batch type jobs. Another way to think of that is things that run forever like a database or a web server versus activities that don’t run forever; usually some kind of finite duration–something like generating a report, doing some kind of ETL batch job–those kinds of things that you may do a single time or multiple times or with a CronJob on a set schedule. So we know there’s Deployments which are meant to handle things that run forever and in the case of CronJobs it is meant to handle things that run repeatedly on a timed schedule. And those are what CronJobs are for in Kubernetes!