简体   繁体   English

kubernetes cronjob 只运行一次?

[英]kubernetes cronjob only runs once?

I created a k8s CronJob with the following schedule (run every minute):我使用以下时间表创建了一个 k8s CronJob(每分钟运行一次):

schedule: "*/1 * * * *"

I see my CronJob created:我看到我的 CronJob 创建了:

NAMESPACE     NAME                   READY   STATUS    RESTARTS   AGE
job-staging   job-1593017820-tt2sn   2/3     Running   0          10m

My job simply does a Printf to the log, one time, then exits.我的工作只是对日志执行 Printf 一次,然后退出。

When I do a kubernetes get cronjob I see:当我执行kubernetes get cronjob时,我看到:

NAMESPACE     NAME                   READY   STATUS    RESTARTS   AGE
job-staging   job   */1 * * * *      False   1         19m        19m

When I look at the logs, it looks like it only ran once, which was the first run.当我查看日志时,它看起来只运行了一次,这是第一次运行。 Do I need to prevent my program from exiting?我需要阻止我的程序退出吗?

I assumed k8s would restart my program, but maybe that's a wrong assumption.我以为 k8s 会重新启动我的程序,但也许这是一个错误的假设。

Your assumption about the behavior of Kubernetes ("restarting the program") is correct.您对 Kubernetes (“重新启动程序”)行为的假设是正确的。

As you may know, a Job is basically a Kubernetes Pod that executes some process and successfully finishes when it exits with a zero exit code .您可能知道,Job 基本上是一个 Kubernetes Pod,它执行一些进程并在以零退出代码退出时成功完成。 The "Cron" part of CronJob is the most obvious, scheduling the Job to execute in a particular time pattern. CronJob 的“Cron”部分是最明显的,它安排 Job 以特定的时间模式执行。

Most YAML objects for CronJobs include the restartPolicy: OnFailure key that prevents Kubernetes from rescheduling the Job for a non-zero exit code (the hello-world YAML file in Kubernetes documentation uses this flag). Most YAML objects for CronJobs include the restartPolicy: OnFailure key that prevents Kubernetes from rescheduling the Job for a non-zero exit code (the hello-world YAML file in Kubernetes documentation uses this flag).

From what I see in the logs obtained by your kubectl instruction, it looks like your Job is failing - because of the Status 1 .根据我在您的kubectl指令获得的日志中看到的内容,您的 Job 似乎失败了 - 因为Status 1 I would recommend you check the logs of the CronJob using kubectl logs -f -n default job-1593017820-tt2sn for any possible errors in the execution of your script (if your script explicitly exits with an exit-code, check for a possible non-zero code).我建议您使用kubectl logs -f -n default job-1593017820-tt2sn检查 CronJob 的日志,以了解脚本执行中是否存在任何可能的错误(如果您的脚本以退出代码显式退出,请检查可能的非-零代码)。

[UPDATE] [更新]

CronJobs also have limitations: CronJobs 也有限制:

A cron job creates a job object about once per execution time of its schedule. cron 作业在其计划的每个执行时间大约创建一次作业 object。 We say “about” because there are certain circumstances where two jobs might be created, or no job might be created.我们说“约”是因为在某些情况下可能会创造两个工作岗位,或者可能不会创造任何工作岗位。 We attempt to make these rare, but do not completely prevent them.我们试图使这些罕见,但不完全阻止它们。 Therefore, jobs should be idempotent.因此,作业应该是幂等的。

I think these are pretty rare scenarios, but maybe you've found yourself in these rare situations.我认为这些是非常罕见的情况,但也许你已经发现自己处于这些罕见的情况中。 The documentation is here .文档在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM