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 official document).
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.
The architecture is something like below:
ocelot
|
consul
/\
webapi1 webapi2 ...
(pod) (pod) ...
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
Now My question: 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?
Headless Service
is the answerKubernetes environment is more dynamic in nature.
deregister a service when the pod is dead
Yes
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
That's why it is recomended to use headless service
which basically fits into this situation. As they mentioned in first line in docs
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)
headless service doesn't get the ClusterIP
. If you do nslookup
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.
UPDATE1:
Please refer this Youtube video. May give you some idea.(Even I have to watch it..!!)