I have two pods, which run each an instance of ServiceA
. ServiceA
uses a db wich runs on a single seperate pod. With this setup, I'm able to deploy fluently and I'd also have a fall back service.
Now my ServiceA
has a regular task GatheringTask
, where it gathers information from an external service, processes them and stores it to it's db. What I'd like to prevent is that both pods that run ServiceA
do this at the same time.
The first solution that comes to my mind is relativly easy. I create a new table in my db pod where I store the state of GatheringTask
so each instance of ServiceA
checks the state persisted in the db before starting its' GatheringTask
. If on is already running, don't do it anymore. This creates quite a bit of overhead, as I have to update and read the db all the time. Also there is more code.
Now I was wondering, is there a more direct way to share a state between pods? Is there a way, I can share a env property or even map a shared memory between pods, so I do not need the detour over a third pod? My pods are managed with kuberenetes.
A proper way of solving this task is to use a distributed lock manager, ZooKeeper for example. It's available as a docker, Kubernetes friendly image and with an appropriate recipe, for example: https://curator.apache.org/curator-recipes/shared-reentrant-lock.html it could provide a reliable locking mechanism.