How to configure kubernetes service for random ports?

10/19/2018

The scenarios seems to be a deadlock:

  • server listens on hostname:0 where hostname is configurable. 0 means random port which it's determined after the program is started. The app does not allow to set a static port.
  • clients connect to server using hostname:actual_port

The dilemma is:

If hostname is "localhost" or "0.0.0.0", clients can't connect to say, "0.0.0.0:42187" (the app does not allow to config the address at client side).

If you create a service, say myservice, and set service name as hostname, you need specify the pod port to create the service. Since the port is random, you need wait after the pod is started to get the port. But you need set the hostname before the pod being started! If you create the service in advance with a certain port, say 42131, the pod fails to start with error "failed to bind to myservice/10.100.28.248:0", unless the program happens to allocate the same port of the service.


Conclusion

I think random port for a service does not make any sense. The problem is with the application itself not k8s. Raised an issue for the app on github (https://github.com/spark-jobserver/spark-jobserver/issues/1122).

-- Leon
dns
kubernetes
port

0 Answers