How to avoid multiple scheduled tasks running in parallel

10/7/2019

I'm building a microservices application using Kubernetes as a containers orchestrator. The app is up and running now but i have an other issue. That is in my services, i have a scheduled task running daily and when service get deployed, multiple service instances will run (by set replica number), that created multiple tasks running at the same time. What i expect is only ONE instance of service task will be running rather than multiple. Is there any technique to handle this case?

Technologies Stack

  1. Kubernetes
  2. Asp.net core to build Micro service
  3. Bedrock implementation for CI/CD
  4. Fabrikate
  5. Traefik as a load balancer

Please help me with this, Thanks!

-- Yen Nguyen
docker
kubernetes
microservices
scheduled-tasks

1 Answer

10/7/2019

You have 2 ways of solving this problem:

1) Use task coordination in your application. Like there is a lock and only application owning a lock can run task. Take a look towards ZooKeeper for a distributed lock logic. This is the preferred solution.

2) Use Kubernetes CronJob which runs daily.

-- Vasily Angapov
Source: StackOverflow