Home > Blockchain >  How to run quartz task on single pod in kubernetes cluster?
How to run quartz task on single pod in kubernetes cluster?

Time:01-10

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.

Eg:
Task1: admin-3002
Task2: admin-4001

Now we've moved this application into the kubernetes cluster but since hostnames are not static in kubernetes, every task is executed on all available pods.

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.

CodePudding user response:

The easiest way is to create a lock table in your MySQL database with the jobname as unique identifier.

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.

CodePudding user response:

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

  • Related