0

My setup is similar to a active/passive deployment but I'd like to use the kubernetes lease and leader election to manage the active pod.

The leader election is all working as intended and the Lease object is created and maintained (roughly based on this post https://carlosbecker.com/posts/k8s-leader-election/ ), but I'm stuck when I try to use this lease information as a service selector and only send traffic to the active pod.

Fwiw, I have this info in the lease itself:

Spec:
  Acquire Time:            2023-06-06T17:45:49.962378Z
  Holder Identity:         leader-test-585469899d-t84qx
  Lease Duration Seconds:  15
  Lease Transitions:       2
  Renew Time:              2023-06-06T17:55:11.891018Z

Can anyone please give me some pointers how to get this to work?

I did play with the thought of adding logic to the leader-elector sidecar that can set a label on the active pod and use that as service selector, but then i realised this label would probably immediately get reverted by the deployment so I abandoned that idea.

2 Answers2

0

I do not have a straight answer here but a suggestion:

Your service must have some logic that determines what the leader does or does not do, and I assume each pod in the set knows who the leader is as they likely elected it, so what I would suggest is simply to implement a conditional redirect to the leader in each of the pods in the set, so that if a non-leader receives traffic meant for the leader, it redirects it to the leader.

MrE
  • 19,584
  • 12
  • 87
  • 105
0

I have been working on a similar path, and have been working on leader-election as a sidecar as you have, queried via readinessprobe - allowing if the leader matches my host, set my app to active.