<p>I have deployed ocelot and consul on the kubernetes cluster. Ocelot acts as the api gateway which will distribute request to internal services. And consul is in charge of service discovery and health check. (BTW, I deploy the consul on the kubernetes cluster following the consul's <a href="https://www.consul.io/docs/platform/k8s/run.html" rel="nofollow noreferrer">official document</a>).</p>

<p>And my service (i.e. asp.net core webapi) is also deployed to the kubernetes cluster with 3 replicas. I didn't create a kubernete service object as those pods will only be consumbed by the ocelot which is in the same cluster.</p>

<p>The architecture is something like below:</p>

<pre><code>            ocelot
              |
            consul
              /\
       webapi1 webapi2 ...
        (pod)   (pod)  ...
</code></pre>

<p>Also, IMO, consul can de-register a pod(webapi) when the pod is dead. so I don't see any need to create a kubernete service object</p>

<p>Now My question: <strong>is it right to register each pod(webapi) to the consul when the pod startup? Or should I create a kubernete service object in front of those pods (webapi) and register the service object to the consul?</strong> </p>

Charlie
<ul>
<li><strong><code>Headless Service</code> is the answer</strong></li>
</ul>

<p>Kubernetes environment is more dynamic in nature.</p>

<blockquote>
  <p>deregister a service when the pod is dead</p>
</blockquote>

<p>Yes</p>

<blockquote>
  <p>Kubernetes Pods are mortal. They are born and when they die, they are
  not resurrected. While each Pod gets its own IP address, even those IP
  addresses cannot be relied upon to be stable over time. A Kubernetes
  Service is an abstraction which defines a logical set of Pods and
  provides stable ip</p>
</blockquote>

<p>That's why it is recomended to use <code>headless service</code> which basically fits into this situation. As they mentioned in first line in <a href="https://kubernetes.io/docs/concepts/services-networking/service/#headless-services" rel="nofollow noreferrer">docs</a></p>

<blockquote>
  <p>Sometimes you don’t need or want load-balancing and a single service
  IP. In this case, you can create “headless” services by specifying
  "None" for the cluster IP (.spec.clusterIP)</p>
</blockquote>

<p>headless service doesn't get the <code>ClusterIP</code>. If you do <code>nslookup</code> on the headless servive, it will resolve all IPs of pods that are under headless service. K8s will take care of adding/managing pod IP under the headless service. Please for more details. And I believe, you can register/provide this headless service name in Cosule.</p>

<ul>
<li>Please refer this blog for detailed <a href="https://akomljen.com/stateful-applications-on-kubernetes/" rel="nofollow noreferrer">here</a></li>
</ul>

<p><strong>UPDATE1:</strong></p>

<p>Please refer this <a href="https://www.youtube.com/watch?v=gsf__yuWCF8" rel="nofollow noreferrer">Youtube video</a>. May give you some idea.(Even I have to watch it..!!)</p>
