【问题标题】:How to properly do cron task in GKE如何在 GKE 中正确执行 cron 任务
【发布时间】:2020-12-01 05:48:21
【问题描述】:

使用 App Engine GAE,我们通常会使用 yaml 来执行不同的 cron 任务,如下所示:

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

我搬到了 GKE,我还不知道如何从一个文件中运行上述 cron 任务:

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

那么如何安排 GKE Cron 文件以适应上述所有任务呢? 我必须为每个不同的任务编写不同的(代码)吗?

时间表应为每周一 09:00 时区:澳大利亚/新南威尔士州。 schedule: "*/1 0 0 * * 0" 是正确的表示吗?

我是否必须指定图像,因为网络部署脚本已经指定了图像?

【问题讨论】:

    标签: kubernetes cron google-kubernetes-engine


    【解决方案1】:

    我对 App Engine 不太熟悉,但是

    • 所有CronJob时间表:时间基于docs的kube-controller-manager的时区
    • 您不需要您的应用程序映像来执行 curl 调用。 A curl image will probably be enough
    • 如果CronJob与应用在同一个集群中运行,则不需要经过Loadbalancer,也可以使用http://service-name/tasks/notifications之类的服务
    • 写三个Cronjobs 可能比尝试将所有三个调用塞进一个更好。

    【讨论】:

      【解决方案2】:

      CronJob in kubernetes 使用标准的Cron 语法:

      # ┌───────────── 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>
      

      因此,如果您想每周一 09:00 运行您的工作,它应该如下所示:

       ┌───────────── 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>
      

      如果您的脚本已与图像集成,则无需使用 curl 来执行它。即使它不是映像的一部分,但它在您的节点上本地可用,您也可以考虑将其安装为 Volume,例如hostPath,这是挂载位于 kubernetes 节点上的文件的最简单方法,以便您的 pod 可以使用它。在这种情况下,您只需将脚本的完整路径作为命令:

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

      否则,您可以使用任何包含 curl 的图像作为 user140547 已建议。

      至于:

      写三个 Cronjobs 可能比试图填满所有更好 三通合一。

      我还强烈建议您使用 3 个单独的 CronJobs,因为如果运行任何这些作业出现任何问题,这种方法更简单,也更容易排除故障。

      【讨论】:

        猜你喜欢
        • 2022-06-16
        • 2020-10-22
        • 1970-01-01
        • 2012-02-07
        • 1970-01-01
        • 1970-01-01
        • 2010-10-19
        • 2012-07-19
        • 1970-01-01
        相关资源
        最近更新 更多