Warm tip: This article is reproduced from serverfault.com, please click

How to properly do cron task in GKE

发布于 2020-11-28 09:18:02

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?

Questioner
LearnToday
Viewed
0
user140547 2020-11-29 04:11:24

I am not too familiar with App Engine, but

  • All CronJob schedule: times are based on the timezone of the kube-controller-manager according to the docs
  • You don't need your app image to do a curl call. A curl image will probably be enough
  • If the CronJob is running in the same cluster as the app, you don't need to go over the Loadbalancer, you can as well use the service like http://service-name/tasks/notifications
  • It is probably better to write three Cronjobs than to try to cram all three calls into one.