<p>To perform the leader election, Kubernetes documentation suggests deploying a sidecar in the set of candidate pods.</p>
<p><a href="https://kubernetes.io/blog/2016/01/simple-leader-election-with-kubernetes/" rel="noreferrer">https://kubernetes.io/blog/2016/01/simple-leader-election-with-kubernetes/</a></p>
<p>This sidecar follows the following steps to elect a leader.</p>
<ol>
<li>If a valid leader does not exist, every sidecar tries to update a Kubernetes endpoint object atomically. Only one of the sidecars can successfully update the endpoint object.</li>
<li>The sidecar which updates the endpoint will assume the leader for a specified time duration.</li>
<li>Current leader will update the endpoint again to extend the time duration in order to retain the leadership.</li>
<li>Other sidecars will not try to update the endpoint again when a valid leader exists.</li>
<li>If the current leader does not update the endpoint within the time duration, other sidecars will consider that the leadership is revoked. All sidecars will go to step 1.</li>
</ol>
<p>There are a few issues with this method.</p>
<ol>
<li>It is possible to run 2 leaders simultaneously for a brief period of time.</li>
</ol>
<h4>Example:</h4>
<p>If the current leader hangs and cannot update the endpoint in time, one of the other sidecars will acquire the leadership. But it will take some time for the previous leader to realize that their leadership status is revoked. For this brief period of time, existing 2 leaders can corrupt a shared resource/data.</p>
<p>This is also mentioned in its source code as well.</p>
<pre><code>This implementation does not guarantee that only one client is acting as a leader (a.k.a. fencing).
</code></pre>
<ol start="2">
<li>The source code of this sidecar is retired/archived. So, it is not in active development.
<a href="https://github.com/kubernetes-retired/contrib/tree/master/election" rel="noreferrer">https://github.com/kubernetes-retired/contrib/tree/master/election</a></li>
</ol>
<p>So, what is the proper method to elect a leader with Kubernetes?</p>

SRF
<p>The initial question is quite old but now, <a href="https://kubernetes.io/docs/concepts/architecture/leases/" rel="nofollow noreferrer">Leases</a> should do the trick.</p>
