如何在 GKE 中正确执行 cron 任务
How to properly do cron task in GKE
使用 App Engine GAE,我们通常会有如下不同的 cron 任务的 yaml:
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 时区:Australia/NSW。 schedule: "*/1 0 0 * * 0" 是正确的表示吗?
我是否必须指定图像,因为 Web 部署脚本已经指定了图像?
我对 App Engine 不太熟悉,但是
- 所有
CronJob
时间表:时间根据 docs 基于 kube-controller-manager 的时区
- 您不需要您的应用图像来执行 curl 调用。 A curl image will probably be enough
- 如果
CronJob
与应用在同一个集群运行,则不需要过Loadbalancer,也可以使用类似http://service-name/tasks/notifications
的服务
- 写三个
Cronjobs
可能比尝试将所有三个调用都塞进一个更好。
CronJob
in kubernetes uses standard 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 e.g. hostPath,这是挂载位于 kubernetes 节点上的文件的最简单方法,以便它可供你的 pods。在这种情况下,您只需要将脚本的完整路径作为您的命令:
args:
- /bin/sh
- -c
- /full/path/to/script.sh
否则,您可以使用任何包含 curl 的图像,因为 user140547 已被建议。
至于:
It is probably better to write three Cronjobs than to try to cram all
three calls into one.
我还强烈建议您使用 3 个单独的 CronJobs
,因为如果 运行 这些作业中的任何一个出现问题,这种方法更简单,更容易进行故障排除。
使用 App Engine GAE,我们通常会有如下不同的 cron 任务的 yaml:
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 时区:Australia/NSW。 schedule: "*/1 0 0 * * 0" 是正确的表示吗?
我是否必须指定图像,因为 Web 部署脚本已经指定了图像?
我对 App Engine 不太熟悉,但是
- 所有
CronJob
时间表:时间根据 docs 基于 kube-controller-manager 的时区
- 您不需要您的应用图像来执行 curl 调用。 A curl image will probably be enough
- 如果
CronJob
与应用在同一个集群运行,则不需要过Loadbalancer,也可以使用类似http://service-name/tasks/notifications
的服务 - 写三个
Cronjobs
可能比尝试将所有三个调用都塞进一个更好。
CronJob
in kubernetes uses standard 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 e.g. hostPath,这是挂载位于 kubernetes 节点上的文件的最简单方法,以便它可供你的 pods。在这种情况下,您只需要将脚本的完整路径作为您的命令:
args:
- /bin/sh
- -c
- /full/path/to/script.sh
否则,您可以使用任何包含 curl 的图像,因为 user140547 已被建议。
至于:
It is probably better to write three Cronjobs than to try to cram all three calls into one.
我还强烈建议您使用 3 个单独的 CronJobs
,因为如果 运行 这些作业中的任何一个出现问题,这种方法更简单,更容易进行故障排除。