How to use cron with Kubernetes to schedule tasks

How to use cron with Kubernetes to schedule tasks


If you have at any time necessary to schedule a one or recurring process in your Kubernetes clusters, it really is just a cronjob away. Jack Wallen displays you how.

Image: Jack Wallen

Kubernetes is 1 of the most versatile and highly effective container administrators on the marketplace. The further you dive into the engineering, the more you notice it can do. Consider for instance, Kubernetes’ skill to use the Linux cron device for the scheduling of tasks.

Utilizing the mix of Kubernetes and cron make it attainable to not just make recurring duties, but scheduled tasks. With the scheduling of responsibilities, you are in a position to have them run at a time when utilization is very low, as to not lavatory down your cluster with responsibilities that are important but could not be specifically similar to your generation workflow.

So how do you integrate cron into your Kubernetes cluster? You do it on a manifest-by-manifest foundation. I’m likely to demonstrate you how to do just that. I’ll show with a quite basic job, so you can simply understand the thought. 

What you will need to have

In buy to increase cron work, you may need to have a running Kubernetes cluster. If you have still to deploy just one, look at out my article: How to deploy a Kubernetes cluster on Ubuntu server.

SEE: Implementing DevOps: A guide for IT execs (cost-free PDF) (TechRepublic)

How to build the YAML file

The to start with factor we need to do is make a YAML file for the task. Keep in mind, this is a extremely standard undertaking, so our YAML file won’t include way too a great deal information. Let’s crack it down.

The very first area defines the API variation and the variety of career we are generating. That area appears to be like:

apiVersion: batch/v1beta1
variety: CronJob

The following part aspects the metadata for the job. That part seems like:

metadata:
  title: cron-hello there

Up coming, we have the specs for the work. This section includes our parameters for cron and is outlined by way of the plan choice, which will run the job each and every 15 minutes. This section seems like:

spec:
  timetable: ",15,30,45 * * * *"

Now we have the work template part, some of which will keep on being blank (for simplicity). This portion defines the image utilised for the container to be deployed–in this scenario, busybox. It also involves a couple arguments, a single of which is the output to be printed for the job (remember, this is a easy undertaking). The output will be the date and a text information printed just about every 15 minutes, as outlined by the plan choice. 

This part seems like:

jobTemplate:
    spec:
      template:
        spec:
          containers:
          - identify: cron-good day
            graphic: busybox
            args:
            - /bin/sh
            - -c
            - day echo Hi, TechRepublic!
          restartPolicy: OnFailure

Make the new file with the command:

nano cron.yaml

In that file, paste the full contents of the manifest revealed under:

apiVersion: batch/v1beta1
sort: CronJob
metadata:
  name: cron-hi there
spec:
  timetable: ",15,30,45 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - identify: cron-hi
            impression: busybox
            args:
            - /bin/sh
            - -c
            - day echo Hi there, TechRepublic!
          restartPolicy: OnFailure

Conserve and shut the file.

How to deploy the work

Now we’re going to deploy the task that features the scheduled process. To do that, concern the command:

kubectl develop -f cron.yaml

Once the work operates, it’s going to report that cron-hello has been created (Figure A).

Determine A

cronjob-report.jpg

Our scheduled process deployment was thriving.

You can guarantee the cronjob activity is functioning with the command:

kubeclt get cronjobs

This will list out each cronjob job on your cluster (Determine B).

Figure B

cronjob-getjobs.jpg

I have two containers that include things like cronjobs on my Kubernetes cluster.

How to enjoy for the cronjob

Let us say you’ve got deployed a container with an linked cronjob and you want to check out for it. To do that, you’d problem the command:

kubectl get work opportunities --view

The output of that command will involve a running checklist of each deployed career (Determine C).

Figure C

cronjob-running.jpg

I have a quantity of established work opportunities that have operate, many thanks to cronjob.

To finish that check out, use the Ctrl+C combination.

How to delete the cronjob

Deleting a cron-scheduled position is fairly easy. To do this, situation the command:

kubectl delete cronjob Name

The place Identify is the name applied in the metadata segment of the YAML file. 

And which is the gist of applying cronjob to timetable one and recurring responsibilities to your Kubernetes cluster. This easy instrument can go a very long way to make your Kubernetes admin occupation a bit easier. Give it a go and see if you you should not wind up building use of cronjob much more and much more.

Also see



Source url