How to properly do cron task in GKE

11/28/2020

With App Engine GAE, we will usually have yaml with different cron tasks as follows:

cron:
# Notifications Job
- description: "Remove Notifications Cron Weekly run"
  url: /tasks/notifications
  schedule: every monday 09:00
  timezone: Australia/NSW
# Jobs job
- description: "Remove Deleted Jobs / completed"
  url: /tasks/jobs/deleted_completed_drafts
  schedule: every monday 09:00
  timezone: Australia/NSW
# Marketplace job
- description: "Remove Deleted Products / soldout"
  url: /tasks/products/deleted_soldout_drafts
  schedule: every monday 09:00
  timezone: Australia/NSW

I moved to GKE, I can't figure out yet exactly how to run the above cron tasks from one file:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: Cron Task
spec:
  schedule: "*/1 0 0 * * 0" #"*/1 * * * *"
  startingDeadlineSeconds: 104444
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: callout
            image: gcr.io/my-site/mysite-kubernetes:v0.0.16
            args:
            - /bin/sh
            - -ec
            - curl https://www.ksite.com/tasks/notifications
          restartPolicy: Never

So how do I arrange the GKE Cron file to accommodate all the above tasks? Do I have to write different(codes) for each different task?

The schedule should be every Monday 09:00 timezone: Australia/NSW. Is schedule: "/1 0 0 * 0" a correct representation of that?

Do I have to specify an image, cause the web-deployment script already has the image specified?

-- LearnToday
cron
google-kubernetes-engine
kubernetes

2 Answers

11/28/2020

I am not too familiar with App Engine, but

-- user140547
Source: StackOverflow

11/30/2020

CronJob in kubernetes uses standard Cron syntax:

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                   7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>

So if you want to run your job every Monday at 09:00 it should look as follows:

 ┌───────────── minute (0 - 59)
 │ ┌───────────── hour (0 - 23)
 │ │ ┌───────────── day of the month (1 - 31)
 │ │ │ ┌───────────── month (1 - 12)
 │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
 │ │ │ │ │                                   7 is also Sunday on some systems)
 │ │ │ │ │
 │ │ │ │ │
 0 9 * * 1 <command to execute>

If your script was integrated with the image, you wouldn't need to use curl to execute it. Even if it's not part of the image but it is available locally on your node, you can think about mounting it as a Volume e.g. hostPath, which is the simplest way of mounting a file located on your kubernetes node so it becomes available to your pods. In such case you simply need to put as your command the full path to the script:

args:
- /bin/sh
- -c
- /full/path/to/script.sh

Otherwise you can use any image containing curl as user140547 already suggeasted.

As to:

It is probably better to write three Cronjobs than to try to cram all three calls into one.

I would also strongly recommend you to use 3 separate CronJobs as such approach is much simpler and easier to troubleshoot if anything goes wrong with running any of those jobs.

-- mario
Source: StackOverflow