I have a contaner in Docker. It has three replicas. I have also a scheduler (library), which runs every minute and invokes a function in my code. Since I have two more replicas, it invokes three times :(
What I want to achieve is:
When scheduler invokes the code, the first replica should execute the task, the other two should detect that and skip doing the task.
How can I achieve this?
You should break those two out into their own services.
Service 1 would have the app running replicas=3
Service 2 would just run the scheduler with replicas=1
For example, if you were in Laravel, you could have all your code in one image and just change the service command to run in the scheduler service.
If it was in something where the app also starts a scheduler and you can't just start the scheduler by itself, then the above still works, you just wouldn't send any connections to the 2nd service since 1st service is doing that work.