title: How to configure kubernetes service for random ports?
body: <p>The scenarios seems to be a deadlock:</p>
  
  <ul>
  <li>server listens on <code>hostname:0</code> 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.</li>
  <li>clients connect to server using <code>hostname:actual_port</code> </li>
  </ul>
  
  <p>The dilemma is: </p>
  
  <p>If <code>hostname</code> 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). </p>
  
  <p>If you create a service, say <code>myservice</code>, and set service name as <code>hostname</code>, 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.</p>
  
  <hr>
  
  <h3>Conclusion</h3>
  
  <p>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 (<a href="https://github.com/spark-jobserver/spark-jobserver/issues/1122" rel="nofollow noreferrer">https://github.com/spark-jobserver/spark-jobserver/issues/1122</a>).</p>