Run Scheduled Cron method in Spring from single pod if multiple pods exist on Kubernetes

10/23/2019

I am moving from single pod(docker image) to multiple pods for my Spring Application on Kubernetes for load handling. But I am facing an issue because I have a cron scheduler method in my application which runs daily at a particular time. If I run multiple pods, they all run simultaneously and as a result multiple entries get saved into my DB, but I want only a single pod to execute that function.

I have thought of generating java uuid and saving it in the DB as the function starts execution on each pod. Then in the same function, putting a sleep timer of, let's say, 5 seconds and comparing the uuid from the DB in each pod. The pod which updated the value in the database latest will match that value and will execute the method ahead.

Is this a good approach? If not, please give suggestions that can be done to solve this issue.

-- Manasvi Batra
java
kubernetes
quartz-scheduler
spring
spring-boot

1 Answer

10/24/2019

Also, you can move the workload to an isolated process, this will be helpful and cleaner, check this out https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/ this will give you an idea.

-- cperez08
Source: StackOverflow