简体   繁体   English

NestJS - 任务调度 - 防止在 K8s 中的相同服务实例中并行运行相同的作业

[英]NestJS - Task Scheduling - Prevent running the same Job in parallel in identical service instances in K8s

We are running a nestjs -based monorepo containing a couple of services each having at least two instances in the google cloud k8s.我们正在运行一个基于nestjs的 monorepo,其中包含几个服务,每个服务在谷歌云 k8s 中至少有两个实例。 Recently we implemented a couple of scheduled jobs to munch some data stored in a mongo replica set and spit out the results every 10 minutes.最近,我们实施了几个计划作业来处理存储在 mongo 副本集中的一些数据,并每 10 分钟输出一次结果。 What we did not think of is that having multiple instances of the same service will result in running the same job as many times as we have instances at the same exact point in time, against the same exact data set.我们没有想到的是,对于相同的确切数据集,拥有同一服务的多个实例将导致运行相同作业的次数与我们在同一确切时间点的实例一样多。 What is the best practice to handle that and have the scheduled job run in only one instance, only once at the predefined time?处理该问题并使计划作业仅在一个实例中运行的最佳实践是什么,并且仅在预定义时间运行一次?

You need to use the .spec.concurrencyPolicy field in your Cron Job spec and set it to Forbid , to prevent concurrent runs of the same job.您需要使用.spec.concurrencyPolicy场在你的计划作业规范,将其设置为Forbid ,以防止同样的工作,并发运行。

See the Concurrency policy chapter in this k8s doc: https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/请参阅此 k8s 文档中的并发策略章节: https ://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/

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

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