I have a cluster of MongoDB instances exposed with a headless service
k get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello 10.0.0.90 <nodes> 8080:32361/TCP 7h
kubernetes 10.0.0.1 <none> 443/TCP 7h
mongo None <none> 27017/TCP 7h
So there are multiple MongoDB endpoints:
k get ep
NAME ENDPOINTS AGE
hello 172.17.0.4:8080 7h
kubernetes 10.0.2.15:8443 7h
mongo 172.17.0.5:27017,172.17.0.6:27017 7h
How can my other service (called hello
) can find 1 IP or 1 DNS entry to talk to ? How would it be possible to discover that list of IPs efficiently?
In the case of Mongo, would it be possible to automatically find the Primary replica (or a writable / readable one) ?
Services should be reachable through their DNS name. In your case "mongo" or "mongo.my-namespace" if you assigned a namespace. You should also check if you have "spec.clusterIP=None" set in your deployment file. If that would be case you are telling kubernetes that you don't want to use the standard way of service discovery and will provide your own.
There are multiple standard ways to expose services:
You can add a fixed internal cluster IP for the service using "spec.clusterIP" field.
Add a "NodePort" type to your service. This allows you to define a port that is available on each node. So when you have a load balancer in front of the service you can forward to this port on each node.
More info can be found at k8s docs