How can I do port discovery with Kubernetes service discovery?

5/21/2018

I have an HPC cluster application where I am looking to replace MPI and our internal cluster management software with a combination of Kubernetes and some middleware, most likely ZMQ or RabbitMQ.

I'm trying to design how best to do peer discovery on this system using Kubernetes' service discovery.

I know Kubernetes can provide a DNS name for a given service, and that's great, but is there a way to also dynamically discover ports?

For example, assuming I replaced the MPI middleware with ZeroMQ, I would need a way for ranks (processes on the cluster) to find each other. I know I could simply have the ranks issue service creation messages to the Kubernetes discovery mechanism and get a hostname like myapp_mypid_rank_42 fairly easily, but how would I handle the port?

If possible, it would be great if I could just do:

zmqSocket.connect("tcp://myapp_mypid_rank_42");

but I don't think that would work since I have no port number information from DNS.

How can I have Kubernetes service discovery also provide a port in as simple a manner as possible to allow ranks in the cluster to discover each other?

Note: The registering process knows its port and can register it with the K8s service discovery daemon. The problem is a quick and easy way to get that port number back for the processes that want it. The question I'm asking is whether or not there is a mechanism as simple as a DNS host name, or will I need to explicitly query both hostname and port number from the k8s daemon rather than simply building a hostname based on some agreed upon rule (like building a string from myapp_mypid_myrank)?

-- stix
kubernetes

2 Answers

5/21/2018

I think in the most usual case you should know the port number to access your services.

But if it is useful, Kubernetes add some environment variables to every pod to ease autodiscovery of all services. For example {SVCNAME}_SERVICE_HOST and {SVCNAME}_SERVICE_PORT. Docs here

-- Ignacio Millán
Source: StackOverflow

6/18/2018

Turns out the best way to do this is with a DNS SRV record:

https://kubernetes.io/docs/concepts/services-networking/service/#discovering-services https://en.wikipedia.org/wiki/SRV_record

A DNS SRV record provides both a hostname/IP and a port for a given request.

Luckily, Kubernetes service discovery supports SRV records and provides them on the cluster's DNS.

-- stix
Source: StackOverflow