<p>I have implemented leader election using kubernetes/client-go leader election. I have 2 replicas. For the first time both pod is selecting as leader, but same pod is not elected as leader after this. And the leader election get stopped after some time. I tried to delete one pod, then the new pod that is created is selected as leader. Again once the pod stopped leading, no pod is acting as leader. I am using configmap for resource lock. Please help me to solve the issue.</p>

<pre><code>func NewElectorWithCallbacks(namespace, configMapName, identity string, ttl time.Duration, client cli.CoreV1Interface, callbacks *leaderelection.LeaderCallbacks) (*leaderelection.LeaderElector, error) {
hostname, err := os.Hostname()
if err != nil {
  return nil, err
}
broadcaster := record.NewBroadcaster()
broadcaster.StartLogging(log.Printf)
broadcaster.StartRecordingToSink(&amp;cli.EventSinkImpl{Interface: client.Events(namespace)})
recorder := broadcaster.NewRecorder(scheme.Scheme, api.EventSource{Component: identity, Host: hostname})
cmLock := &amp;resourcelock.ConfigMapLock{
  Client: client,
  ConfigMapMeta: meta.ObjectMeta{
    Namespace: namespace,
    Name:      configMapName,
  },
  LockConfig: resourcelock.ResourceLockConfig{
    Identity:      identity,
    EventRecorder: recorder,
  },
}
if callbacks == nil {
  callbacks = NewDefaultCallbacks()
}
config := leaderelection.LeaderElectionConfig{
  Lock:          cmLock,
  LeaseDuration: ttl,
  RenewDeadline: ttl / 2,
  RetryPeriod:   ttl / 4,
  Callbacks:     *callbacks,
}
return leaderelection.NewLeaderElector(config)
}

config, err = rest.InClusterConfig()
v1Client, err := v1.NewForConfig(config)
callbacks := &amp;leaderelection.LeaderCallbacks{
        OnStartedLeading: func(context.Context) {
            // do the work
           fmt.Println("selected as leader")
            // Wait forever
            select {}
        },
        OnStoppedLeading: func() {
            fmt.Println("Pod stopped leading")

        },
    }
elector, err := election.NewElectorWithCallbacks(namespace, electionName, hostname, ttl, v1Client, callbacks)
elector.Run(context.TODO())
</code></pre>

<p><a href="https://i.stack.imgur.com/CZxx5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CZxx5.png" alt="enter image description here"></a></p>

<p>You can deploy the pods as <code>statefullsets</code> &amp; <code>headless service</code>. Please refer the <a href="https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/" rel="nofollow noreferrer">docs</a></p>

<p>Why?</p>

<ul>
<li>Pods will create sequentially. You define the first pod being launched is <code>Master</code> and rest are slaves.  </li>
<li>Pods in a StatefulSet have a unique ordinal index and a stable network identity. For example below,</li>
</ul>

<pre><code>    kubectl get pods -l app=nginx
    NAME      READY     STATUS    RESTARTS   AGE
    web-0     1/1       Running   0          1m
    web-1     1/1       Running   0          1m

</code></pre>

<p>Even if the pod <code>web-0</code> restarts, the name or FQDN for pod never change.</p>

<p><code>web-0.nginx.default.svc.cluster.local</code></p>

<p><code>&lt;pod_name&gt;.&lt;service_name&gt;.&lt;namespace&gt;.svc.cluster.local</code></p>

<p>I have only highlighted few points, please go though the docs completly. </p>
