<p>In Kubernetes, is it possible to have 2 services for a single deployment, one which is &quot;standard&quot; and proxies in front of all ready pods, and a second service which sends traffic only the elected leader? If so how? I am using <a href="https://pkg.go.dev/k8s.io/client-go/tools/leaderelection?tab=doc" rel="nofollow noreferrer">client-go</a> for leader election. These are layer 4 services.</p>
<p>I know that a service can use labels for a selector, but client-go uses an annotation to mark the leader. Using a <a href="https://kubernetes.io/docs/concepts/services-networking/service/#services-without-selectors" rel="nofollow noreferrer">service without selectors</a> and creating/removing an endpoint in the <a href="https://pkg.go.dev/k8s.io/client-go/tools/leaderelection?tab=doc#LeaderCallbacks" rel="nofollow noreferrer">leader callbacks</a> seems hacky/buggy. Thank you.</p>

user2133814
<blockquote>
<p>In Kubernetes, is it possible to have 2 services for a single deployment, one which is &quot;standard&quot; and proxies in front of all ready pods, and a second service that sends traffic only the elected leader?</p>
</blockquote>
<p>Yes, but it seems a bit hacky. The way services work is like this:</p>
<p>Service -&gt; Service labels select endpoints -&gt; Endpoint -&gt; Endpoint labels select pods -&gt; Pods (PodIP)</p>
<ol>
<li><p>So you could have your regular &quot;Service&quot; that points to all the pods on your Deployment or StatefulSet which automatically provisions all the Endpoints.</p>
</li>
<li><p>You could also have another set of <a href="https://kubernetes.io/docs/concepts/services-networking/service/#headless-services" rel="nofollow noreferrer">&quot;Headless Service&quot;</a> + <a href="https://theithollow.com/2019/02/04/kubernetes-endpoints/" rel="nofollow noreferrer">&quot;Endpoint&quot;</a> each manually created individually that you make their labels match with each other and then have that Endpoint manually match with the label of the pod of your choice.</p>
</li>
</ol>
<p>Now with respect to <a href="https://pkg.go.dev/k8s.io/client-go/tools/leaderelection?tab=doc" rel="nofollow noreferrer">client-go/leaderelection</a>. It seems like it works using an <a href="https://github.com/kubernetes/client-go/blob/v11.0.0/tools/leaderelection/resourcelock/endpointslock.go" rel="nofollow noreferrer">Endpoint</a> or <a href="https://github.com/kubernetes/client-go/blob/v11.0.0/tools/leaderelection/resourcelock/configmaplock.go" rel="nofollow noreferrer">ConfigMap</a> lock for the leader (The example shows a <a href="https://github.com/kubernetes/client-go/blob/v11.0.0/tools/leaderelection/example/main.go#L68" rel="nofollow noreferrer">ConfigMap</a> lock). But, looks like you want to use the Endpoint lock. So this package doesn't work with services or labels, it looks like it just works on Endpoint resources. So essentially if you have 3 nodes and want to find the leader you would have to use 3 manually created Endpoint resources. The one that is the leader will always have the annotation.</p>
<p>Now how do you tie it to 2) above? As your client elects or selects the leader then you also have to change the endpoint labels so that they match your manually created headless service. (It could be done in your code too)</p>
<p>You could also just elect to just use Endpoints instead 2) above (No headless service) and have client-go/leaderelection talk directly to the endpoints.</p>
<p>Another option is to take advantage of <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">StatefulSets</a> and its required <a href="https://kubernetes.io/docs/concepts/services-networking/service/#headless-services" rel="nofollow noreferrer">headless service</a>. So that service will resolve to the IP addresses of all the replicas in your quorum-based cluster. The leader election would be up to the client package (client-go doesn't seem to support this) which is pretty the case for most quorum based applications (K8s, Zookeeper, Kafka, Etcd, etc, etc); the client is the one that finds who the leader is.</p>
<p>✌️</p>
