[英]How to run quartz task on single pod in kubernetes cluster?
I've a Spring MVC project in which quartz tasks are currently bound to virtual machines in mysql table and based on these mapping, we execute the task on that respective server.我有一个 Spring MVC 项目,其中石英任务当前绑定到 mysql 表中的虚拟机,并且基于这些映射,我们在相应的服务器上执行任务。
Eg:例如:
Task1: admin-3002任务 1:admin-3002
Task2: admin-4001任务 2:admin-4001
Now we've moved this application into the kube.netes cluster but since hostnames are not static in kube.netes, every task is executed on all available pods.现在我们已将此应用程序移至 kube.netes 集群中,但由于 kube.netes 中的主机名不是 static,因此每个任务都在所有可用的 pod 上执行。
Can anyone suggest an optimal solution to this problem.任何人都可以建议这个问题的最佳解决方案。 Would consistent hashing be a good solution to achieve this?
一致性哈希是实现这一目标的好解决方案吗?
We're currently running a single pod to avoid tasks running on all pods but need to find a better way to resolve this issue.我们目前正在运行单个 pod 以避免在所有 pod 上运行任务,但需要找到更好的方法来解决此问题。
The easiest way is to create a lock table in your MySQL database with the jobname as unique identifier.最简单的方法是在 MySQL 数据库中创建一个锁表,并将作业名称作为唯一标识符。
When a job start try to write a record to that table.当作业开始时,尝试向该表写入一条记录。
When there is already a record for your job just stop processing your job.当你的工作已经有记录时,就停止处理你的工作。
When there is no lock create and commit the lock, process the job and at the end remove the lock.当没有锁创建并提交锁时,处理作业并在最后删除锁。
A nice solution to this would be leader election, potentially using kube api. Less clean but potentially as successfull would be to use StatefulSet and execute certain logic only on the pod with a -0
suffix as sts pods are numbered sequentially, always from 0一个很好的解决方案是领导者选举,可能使用 kube api。不太干净但可能同样成功的是使用 StatefulSet 并仅在带有
-0
后缀的 pod 上执行某些逻辑,因为 sts pod 按顺序编号,始终从 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.